我在将时间和按钮合并在一起时遇到问题...
我无法在JPanel
.
这是我的代码:
import java.awt.Color;
import java.awt.Font;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public final class Date_Time extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel show_date_time = new JPanel();
private JLabel time = new JLabel("Time:");
private JLabel show_time = new JLabel("Show Time");
DateFormat dateFormat2 = new SimpleDateFormat("h:mm:ss a");
java.util.Date date2;
private JLabel label;
private JPanel panel;
public Date_Time(){
this.setSize(300, 300);
this.setResizable(false);
getContentPane().add(Show_Time_date());
}
private JButton button1 = new JButton();
private JFrame frame1 = new JFrame();
public JPanel Show_Time_date(){
frame1.add(show_date_time);
show_date_time.setBackground(Color.ORANGE);
frame1.add(button1);
getShow_date_time().setLayout(null);
Font f;
f = new Font("SansSerif", Font.PLAIN, 15);
getTime().setBounds(0,250,400,30);
getTime().setFont(f);
getShow_date_time().add(getTime());
setShow_time(new JLabel(""));
updateDateTime();
getShow_time().setBounds(37,250,400,30);
getShow_time().setFont(f);
getShow_date_time().add(getShow_time());
return getShow_date_time();
}
public static void main(String[] args) {
Date_Time Main_win=new Date_Time();
Main_win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main_win.setVisible(true);
}
public void updateDateTime()
{
Thread th = new Thread(new Runnable()
{
@Override
public void run()
{
while(true)
{
date2 = new java.util.Date();
String dateTime = dateFormat2.format(date2);
getShow_time().setText(dateTime);
getShow_time().updateUI();
}
}
});
th.start();
}
/**
* @return the show_time
*/
public JLabel getShow_time() {
return show_time;
}
/**
* @param show_time the show_time to set
*/
public void setShow_time(JLabel show_time) {
this.show_time = show_time;
}
/**
* @return the time
*/
public JLabel getTime() {
return time;
}
/**
* @param time the time to set
*/
public void setTime(JLabel time) {
this.time = time;
}
/**
* @return the show_date_time
*/
public JPanel getShow_date_time() {
return show_date_time;
}
/**
* @param show_date_time the show_date_time to set
*/
public void setShow_date_time(JPanel show_date_time) {
this.show_date_time = show_date_time;
}
/**
* @return the label1
*/
/**
* @param label1 the label1 to set
*/
/**
* @return the label
*/
public JLabel getLabel() {
return label;
}
/**
* @param label the label to set
*/
public void setLabel(JLabel label) {
this.label = label;
}
/**
* @return the panel
*/
public JPanel getPanel() {
return panel;
}
/**
* @param panel the panel to set
*/
public void setPanel(JPanel panel) {
this.panel = panel;
}
}