为什么我的GUI卡住了? (试图在Java Swing中制作闹钟)

时间:2017-07-20 18:35:11

标签: java swing java-threads

你好我做了一个闹钟...它的工作原理但是当我启动应用程序时,GUI被卡住了...任何想法如何修改代码以便它可以工作?想法是,如果我不把while循环的小时分钟和秒变量不更新......

package alarm;

 import java.awt.BorderLayout;
 import java.awt.Font;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextArea;
 import javax.swing.JTextField;
 import javax.swing.SwingConstants;
 import javax.swing.Timer;
 import org.joda.time.LocalTime;

 public class Alarm {

static String jtfh;
static String jtfm;
static String jtfs;

static int h;
static int m;
static int s;




public static void main(String[] args) {

        JFrame jf = new JFrame();
        jf.setVisible(true);
        jf.setSize(400, 400);
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel jp = new JPanel();
        jf.add(jp);
        jp.setSize(400, 400);
        jp.setLayout(new BorderLayout());

      while(true) { 
        LocalTime lt = new LocalTime();
        h = lt.getHourOfDay();
        m = lt.getMinuteOfHour();
        s = lt.getSecondOfMinute();



        String h1 = Integer.toString(h);
        String m1 = Integer.toString(m);
        String s1 = Integer.toString(s);

        String hour = h1+":"+m1+":"+s1;

        System.out.println(h);

        JLabel jl = new JLabel(hour);
        jp.add(jl);
        jl.setVisible(true);
        jl.setBounds(200, 200, 100, 500);
        Font labelFont = jl.getFont();
        jl.setFont(new Font(labelFont.getName(), Font.PLAIN, 30));

        Thread clock = new Thread() {
            public void run() {
                for(;;) {
                    LocalTime lt = new LocalTime();
        int h = lt.getHourOfDay();
        int m = lt.getMinuteOfHour();
        int s = lt.getSecondOfMinute();



        String h1 = Integer.toString(h);
        String m1 = Integer.toString(m);
        String s1 = Integer.toString(s);

        String hour = h1+":"+m1+":"+s1;

        jl.setText(hour);
                }
            }
        };

        clock.start();

        JTextField jtf1 = new JTextField();
        jp.add(jtf1);
        jtf1.setVisible(true);
        jtf1.setBounds(50, 100, 50, 50);

        JTextField jtf2 = new JTextField();
        jp.add(jtf2);
        jtf2.setVisible(true);
        jtf2.setBounds(150, 100, 50, 50);

        JTextField jtf3 = new JTextField();
        jp.add(jtf3);
        jtf3.setVisible(true);
        jtf3.setBounds(250, 100, 50, 50);

        JButton jb = new JButton("Set alarm");
        jp.add(jb);
        jb.setVisible(true);
        jb.setBounds(80, 200, 200, 100);


        jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                jtfh = jtf1.getText();
                jtfm = jtf2.getText();
                jtfs = jtf3.getText();

                System.out.println(jtfh);

                if(jtfh.equals(h1)&&jtfm.equals(m1)) {
           System.out.println("alarm");
       }
            }
        });


      }






}

}

0 个答案:

没有答案