setVisible(true)不起作用

时间:2014-11-06 20:56:27

标签: java jframe

好吧,我正在编写一个计时器(或至少尝试),我的代码工作得非常好。 我的代码中有3个类,主类,设置定时器的帧,以及定时器关闭的另一个帧。现在,我的代码工作得很好,直到我在第二帧(倒计时)中编写System.out.println(“working”),然后从第一帧停止显示,甚至我删除了System.out.println。 有人能帮助我吗?

主要课程:

package timer;

public class Main {
    static Timer T = new Timer();
    public static void main (String[] args){
        Topen();
    }

    public static void Tclose(){
        T.setVisible(false);
    }

    public static void Topen(){
        T.setVisible(true);
    }
}

其他类有点长,但它们都是JFrames

编辑:这是其他类(第二帧尚未完成):

第一帧(计时器):

public class Timer extends JFrame {
    private static final long serialVersionUID = 1L;

    private static final String title = "Timer";
    private static final int WIDTH = 600;
    private static final int HEIGHT = 400;
    int H2;
    int min2;
    int sec2;

    static ActualTimer AT = new ActualTimer();

    JPanel setTimerPanel = new JPanel();
    JPanel buttonsPanels = new JPanel();

    JButton start = new JButton("start timer");
    JButton exit = new JButton("exit");

    JLabel points = new JLabel(":");
    JLabel points2 = new JLabel(":");
    JTextField H = new JTextField("00");
    JTextField min = new JTextField("00");
    JTextField sec = new JTextField("00");

    public Timer() {
        super(title);
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                H2 = Integer.parseInt(H.getText());
                min2 = Integer.parseInt(min.getText());
                sec2 = Integer.parseInt(sec.getText());
                AT.H2 = H2;
                AT.min2 = min2;
                AT.sec2 = sec2;
                H2 = 0;
                min2 = 0;
                sec2 = 0;
                H.setText(""+H2);
                min.setText(""+min2);
                sec.setText(""+sec2);

                AT.H.setText(""+AT.H2);
                AT.min.setText(""+AT.min2);
                AT.sec.setText(""+AT.sec2);

                Main.Tclose();
                openAT();
                AT.nanosec = System.nanoTime();
                AT.start = true;
            }
        });

        buttonsPanels.add(start);
        buttonsPanels.add(exit);
        setTimerPanel.add(H);
        setTimerPanel.add(points);
        setTimerPanel.add(min);
        setTimerPanel.add(points2);
        setTimerPanel.add(sec);
        add(setTimerPanel, BorderLayout.NORTH);
        add(buttonsPanels, BorderLayout.SOUTH);
    }

    public static void openAT(){
        AT.setVisible(true);
    }

    public static void closeAT(){
        AT.setVisible(false);
    }

}

第二帧(ActualTimer(不,我觉得没有名字 - 创意)):

public class ActualTimer extends JFrame implements Runnable{
    private static final long serialVersionUID = 1L;

    private static final String title = "Timer";
    private static final int WIDTH = 600;
    private static final int HEIGHT = 400;

    Thread thread = new Thread(this);

    long nanosec;
    long remaining = 0;

    public static boolean start = false;

    int H2;
    int min2;
    int sec2;

    JPanel p = new JPanel();

    JButton restart = new JButton("restart timer");
    JButton exit = new JButton("exit");

    JLabel points = new JLabel(":");
    JLabel points2 = new JLabel(":");
    JTextField H = new JTextField("00");
    JTextField min = new JTextField("00");
    JTextField sec = new JTextField("00");

    public ActualTimer() {
        super(title);
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        restart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Timer.closeAT();
                Main.Topen();
                start = false;

            }
        });

        p.add(H);
        p.add(points);
        p.add(min);
        p.add(points2);
        p.add(sec);
        add(p);

        while(true){
            if(start){
                thread.start();
                continue;
            }
        }
    }

    @Override
    public void run() {
        int go = 0;
        if(System.nanoTime() - nanosec + remaining > 1000000000){
            go++;
            remaining = System.nanoTime() - nanosec;
            nanosec = System.nanoTime();

        }
        while(go>0){
            sec2--;
            if(Integer.parseInt(sec.getText()) > -1)
                sec.setText(""+sec2);
            else{
                sec2+=60;
                min2--;
                sec.setText(""+sec2);
                if(Integer.parseInt(min.getText()) > -1)
                     min.setText(""+min2);
                else
                    min2+=60;
                    H2--;
                    min.setText(""+sec2);
                    if(Integer.parseInt(H.getText()) > -1)
                        H.setText(""+H2);
                    else
                        JOptionPane.showMessageDialog(null, "the timer has stopped");
                        thread.stop();
        }
        }
    }
}

我很清楚第二帧中run方法中的问题,当问题开始时我正要改变它。

0 个答案:

没有答案