0

我在将时间和按钮合并在一起时遇到问题...

我无法在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;
    }
}
4

2 回答 2

3

这里看看这个修改后的代码:

import java.awt.Color;
import java.awt.FlowLayout;
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;
import javax.swing.SwingUtilities;

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");

    private DateFormat dateFormat2 = 
                new SimpleDateFormat("h:mm:ss a");
    private JButton button1 = new JButton("USELESS");           
    private java.util.Date date2;
    private JLabel label;
    private JPanel panel;

    public Date_Time(){        

        //this.setResizable(false);
        getContentPane().add(show_Time_date());
    }    

    public JPanel show_Time_date(){

        getShow_date_time().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        /*
         * Always set this Opaque property of the 
         * said JComponent to true, before
         * you change the Background Colour
         * of the said component in question.
         * In some Look And Feels it's set to
         * false by default, so you won't
         * see any colour, in such situations.
         */
        getShow_date_time().setOpaque(true);
        getShow_date_time().setBackground(Color.ORANGE);        

        Font f;
        f = new Font("SansSerif", Font.PLAIN, 15);

        getTime().setFont(f);
        getShow_date_time().add(getTime());
        setShow_time(new JLabel(""));

        updateDateTime();

        getShow_time().setFont(f);
        getShow_date_time().add(getShow_time());
        getShow_date_time().add(button1);

        return getShow_date_time();
    }

    public static void main(String[] args) {

        /*
         * Calls like pack()/setSize()/setVisible()
         * must be done from inside the Event Dispatch Thread
         * or the EDT in short. And not from your
         * Main Thread. Please read Concurrency in Swing
         * in detail from this link : 
         * http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html
         */
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                Date_Time main_win=new Date_Time();
                main_win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                main_win.setSize(250, 75);
                main_win.setVisible(true);
            }
        });        
    }

    public void updateDateTime()
    {
        Thread th = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                while(true)
                {
                    date2 = new java.util.Date();
                    final String dateTime = dateFormat2.format(date2);
                    /*
                     * Any updates to the GUI, must also be done
                     * on the EDT - Event Dispatch Thread.
                     */
                    SwingUtilities.invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            getShow_time().setText(dateTime);
                        }
                    });                    
                }
            }
        });
        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;
    }
}
于 2012-07-08T09:09:08.527 回答
3

正如@nIcE cOw 所指出的,您的线程未正确同步,并且invokeLater()是正确的解决方案。不幸的是,您的线程也会连续运行,从而影响性能。您应该考虑以下替代方案:

  • 差:让处理器在循环中:

    Thread.yield(); // prevent thread from starving
    
  • invokeLater()更好:在catch之后添加适当的延迟InterruptedException

    try {
        Thread.sleep(500);
    } catch (InterruptedException ex) {
        Logger.getLogger(Date_Time.class.getName()).log(Level.SEVERE, null, ex);
    }
    
  • 最佳:使用,如此javax.swing.Timer所示。

于 2012-07-08T15:12:16.810 回答