你能帮我解决这个代码吗,我试图在标签中显示当前日期,但我使用了3个不同的类,即主类(AppStart),我在广告中创建标签的类将在框架中显示标签,即 (Swing1),然后是它本身的日期类 (DateLabel)。代码如下所示:
public DateLabel()
{
Date today = new Date();
//Date format
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
String strDate = df.format(today);
setText( strDate);
}
}
import java.awt.*;//used for Gui Developement
import java.util.*;
import java.text.*;
import javax.swing.*;//Used for GUI development
public class Swing1 extends JFrame
{
JLabel lblWelcome;
DateLabel myDate;
Swing1()
{
JFrame myJF = new JFrame();
myJF.setTitle("CBT Tutorial");
//JLabel Stuff
myDate = new DateLabel();
Container c = getContentPane();
c.add(myDate, BorderLayout.NORTH);
myJF.setSize(300,300);
myJF.show();
}
}
public class AppStart {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
new Swing1();
}
}
最佳答案
对于使用 netbeans 可视化生成器的任何人,我找到了一种更快的方法来执行此操作。
public classname{
Initcomponents();
String date= new Date.toString();
Yourlabelname.setText(date)}
}
希望它能帮助陷入困境的人!:-)
关于java - 想要在标签中显示日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7892381/