不同的GUI组件在运行时不断消失

时间:2014-03-10 15:40:43

标签: java swing user-interface

GUI构建还是一个新手。我正在尝试为闹钟制作一个简单的GUI。在制作GUI时,我一直发现不同的组件在运行时不断消失。

    int hour=0;
     int mins=0;
     JLabel timer;
     JLabel timer2;


    public Gui(){

        JFrame jframe = new JFrame("test");
        jframe.setVisible(true);
        jframe.setSize(420,580);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Panel p =new Panel();
        p.setLayout(new GridBagLayout());
        p.setBackground(Color.black);
        jframe.add(p);

        GridBagConstraints c = new GridBagConstraints();


        timer = new JLabel(); timer2= new JLabel();

        timer.setForeground(Color.red); timer2.setForeground(Color.red);
        timer.setText("0" + hour); timer2.setText("0" + mins);
        c.gridx=0;c.gridy=1;c.insets = new Insets(10,10,0,0);p.add(timer,c);
        c.gridx=1;c.gridy=1;c.insets = new Insets(10,10,0,0);p.add(timer2,c);

        JButton button1 = new JButton("^");
        c.gridx=0;
        c.gridy=0;
        c.insets = new Insets(10,10,0,0);
        p.add(button1,c);  
        button1.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
                 String hr ="";
                  if(hour==23){
                     hour=0;
                  }
                  else{
                      hour++;
                  }
                  if(hour <10){ 
                    hr = "0" + hour;
                 }
                  else { 
                    hr = Integer.toString(hour);
                  }
                  timer.setText(hr);
             }  
            });



        JButton button2 = new JButton("^");
        c.gridx=1;
        c.gridy=0;
        c.insets = new Insets(10,10,0,0);
        p.add(button2,c);  
        button2.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent ea){
                 String min ="";

                  if(mins==59){
                     mins=0;
                  }
                  else{
                      mins++;
                  }
                  if(mins <10){ 
                    min = "0" + mins;
                 }
                  else { 
                    min = Integer.toString(mins);
                  }
                  timer2.setText(min);
             }

            });




        JButton button3 = new JButton("v");
        c.gridx=0;
        c.gridy=2;
        p.add(button3,c);  
        button3.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent eb){
                 String hr ="";

                  if(hour==0){
                     hour=23;
                  }
                  else{
                      hour--;
                  }

                  if(hour <10){ 
                    hr = "0" + hour;
                 }
                  else { 
                    hr = Integer.toString(hour);
                  }
                  timer.setText(hr);
             }

            });



        JButton button4 = new JButton("v");
        c.gridx=1;
        c.gridy=2;
        c.insets = new Insets(10,10,0,0);
        p.add(button4,c);  

        button4.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent ea){
                 String min ="";

                  if(mins==0){
                     mins=59;
                  }
                  else{
                      mins--;
                  }
                  if(mins <10){ 
                    min = "0" + mins;
                 }
                  else { 
                    min = Integer.toString(mins);
                  }
                  timer2.setText(min);
             }

            });




        JButton button5 = new JButton("cancel");
        c.gridx=1;
        c.gridy=3;
        c.insets = new Insets(10,10,0,0);
        p.add(button5,c);  

        JButton button6 = new JButton("ok");
        c.gridx=0;
        c.gridy=3;
        c.insets = new Insets(10,10,0,0);
        p.add(button6,c);  



    }
    public static void main(String[] args) {
       Gui app=new Gui();
    }




}

任何帮助将不胜感激。对此仍然很新。非常感谢

0 个答案:

没有答案