2014-05-14 130 views
0

我有一個程序,以一個空面板開始,然後當我按下一個按鈕,面板將被填充一些其他面板,其中包含的元素。如何清除面板?

但是,如果我再次按下按鈕,它會一遍又一遍地重疊和堆疊,變得非常難看。

如何每次按下按鈕時清除面板,使其不會重疊。 請注意,我已經使用removeAll()UpdateUI()而且沒有任何工作。在這裏,我的代碼:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.ComponentOrientation; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.BorderFactory; 
import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextField; 

public class Calculator extends JFrame { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    JPanel NumberPanel = new JPanel(); 
    String[] ButtonString = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "+/-"}; 
    JButton ButtonArray[] = new JButton[ButtonString.length]; 
    JPanel OperationPanel = new JPanel(); 
    String[] OperationString = {"Erase", "Ac", "*", "/", "+", "-", "Ans", "="}; 
    JButton[] OperationArray = new JButton[OperationString.length]; 
    JTextField WritingZone = new JTextField(27); 
    JButton b1 = new JButton("Ok"); 
    JButton b2 = new JButton("Exit"); 
    double first = 0; 
    double second = 0; 
    double result = 0; 
    char operation = ' '; 

    Calculator() { 
     super("Project Wolverine"); 
     setLayout(new BorderLayout(5, 5)); 
     JPanel radio1 = new JPanel(); 
     final JRadioButton r1 = new JRadioButton("Simple Calculator"); 
     final JRadioButton r2 = new JRadioButton("Advanced Calculator"); 
     ButtonGroup g1 = new ButtonGroup(); 
     g1.add(r1); 
     g1.add(r2); 
     radio1.setLayout(new GridLayout(3, 1, 5, 5)); 
     radio1.add(r1); 
     radio1.add(r2); 
     radio1.setBorder(BorderFactory.createTitledBorder("Choose a calculator")); 
     JPanel panel2 = new JPanel(); 
     panel2.setLayout(new GridLayout(1, 2, 5, 5)); 
     panel2.add(b1); 
     panel2.add(b2); 
     radio1.add(panel2); 
     add(BorderLayout.WEST, radio1); 
     final JPanel emptyPanel = new JPanel(); 
     emptyPanel.setVisible(false); 
     emptyPanel.setLayout(new FlowLayout()); 
     add(BorderLayout.EAST, emptyPanel); 

     b1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       emptyPanel.removeAll(); 
       if (e.getSource() == b1) { 

        if (r1.isSelected()) { 
         for (int a = 0; a < ButtonArray.length; a++) { 
          ButtonArray[a] = new JButton(ButtonString[a]); 
         } 
         NumberPanel.setLayout(new GridLayout(4, 3, 5, 5)); 
         for (int a = 0; a < ButtonArray.length; a++) { 
          NumberPanel.add(ButtonArray[a]); 
         } 

         for (int a = 0; a < OperationArray.length; a++) { 
          OperationArray[a] = new JButton(OperationString[a]); 
         } 
         OperationPanel.setLayout(new GridLayout(4, 2, 5, 5)); 
         for (int a = 0; a < OperationArray.length; a++) { 
          OperationPanel.add(OperationArray[a]); 
         } 

         JPanel Finalpanel = new JPanel(); 
         Finalpanel.setLayout(new FlowLayout()); 
         Finalpanel.add(NumberPanel); 
         Finalpanel.add(OperationPanel); 

         WritingZone.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
         WritingZone.setBorder(BorderFactory.createLoweredSoftBevelBorder()); 
         WritingZone.setEditable(false); 
         WritingZone.setBackground(Color.white); 
         JPanel TextPanel = new JPanel(); 
         TextPanel.add(WritingZone); 

         for (int a = 0; a < ButtonArray.length; a++) { 
          ButtonArray[a].addActionListener(new ActionListener() { 
           public void actionPerformed(ActionEvent e) { 
            for (int a = 0; a < ButtonArray.length - 1; a++) { 
             if (e.getSource() == ButtonArray[a]) { 
              String temp = WritingZone.getText(); 
              WritingZone.setText(temp + ButtonArray[a].getText()); 
             } 
            } 
           } 
          }); 
         } 
         ButtonArray[11].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           if (e.getSource() == ButtonArray[11]) { 
            StringBuilder temp = new StringBuilder(); 
            temp.append(WritingZone.getText()); 
            WritingZone.setText(""); 
            for (int a = 0; a < temp.length(); a++) { 
             if (temp.charAt(a) == '-') { 
              temp.deleteCharAt(a); 
              WritingZone.setText(temp.toString()); 
             } else { 
              WritingZone.setText("-" + temp.toString()); 
             } 
            } 
           } 
          } 
         }); 

         OperationArray[0].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           if (e.getSource() == OperationArray[0]) { 
            String temp = WritingZone.getText(); 
            String temp2 = ""; 
            WritingZone.setText(""); 
            for (int x = 0; x < temp.length() - 1; x++) { 
             temp2 += (Character.toString(temp.charAt(x))); 
            } 
            WritingZone.setText(temp2); 
           } 
          } 
         }); 

         OperationArray[1].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           if (e.getSource() == OperationArray[1]) { 
            WritingZone.setText(""); 
            first = 0; 
            second = 0; 
           } 
          } 
         }); 

         OperationArray[2].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation = OperationArray[2].getText().charAt(0); 
          } 
         }); 

         OperationArray[3].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation = OperationArray[3].getText().charAt(0); 
          } 
         }); 

         OperationArray[4].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation = OperationArray[4].getText().charAt(0); 
          } 
         }); 

         OperationArray[5].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation = OperationArray[5].getText().charAt(0); 
          } 
         }); 

         OperationArray[6].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           String temp = String.valueOf(result); 
           String temp2 = WritingZone.getText(); 
           WritingZone.setText(""); 
           WritingZone.setText(temp2 + "" + temp); 
          } 
         }); 

         OperationArray[7].addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           if (operation == '*') { 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            result = (first * second); 
            String temp2 = String.valueOf(result); 
            WritingZone.setText(temp2); 
           } else if (operation == '/') { 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            if (second < 0) { 
             result = (first/second); 
             String temp2 = String.valueOf(result); 
             WritingZone.setText(temp2); 

            } else if (second > 0) { 
             result = (first/second); 
             String temp2 = String.valueOf(result); 
             WritingZone.setText(temp2); 

            } else if (second == 0) { 
             WritingZone.setText("Error! Can't divide by 0"); 
            } 
           } else if (operation == '+') { 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            result = (first + second); 
            String temp2 = String.valueOf(result); 
            WritingZone.setText(temp2); 
           } else if (operation == '-') { 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            result = (first - second); 
            String temp2 = String.valueOf(result); 
            WritingZone.setText(temp2); 
           } 
          } 
         }); 

         final JPanel AllPanel = new JPanel(); 
         AllPanel.setLayout(new BorderLayout(5, 5)); 
         AllPanel.add(BorderLayout.NORTH, TextPanel); 
         AllPanel.add(BorderLayout.CENTER, Finalpanel); 
         AllPanel.setBorder(BorderFactory.createTitledBorder("Simple Calculator")); 
         emptyPanel.add(AllPanel); 
         emptyPanel.setVisible(true); 
         pack(); 

        } 
       } 
      } 

     }); 

     b2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource() == b2) { 
        System.exit(0); 
       } 
      } 
     }); 
    } 

    public static void main(String[] arg) { 

     JFrame frame = new Calculator(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.pack(); 
     frame.setResizable(false); 
    } 
} 

回答

1

開始採取看看一些所涉及的板...

NumberPanelOperationPanelWritingZone這是實例變量和emptyPane這是一個final局部變量。

這些都需要「清除」或重新創建。

一個簡單的解決辦法是有emptyPane作爲唯一的實例變量和簡單的調用emptyPane.removeAll()每到ActionListener被觸發,但因爲它的立場,你將不得不調用removeAll在每個前面提到的面板,例如...

public void actionPerformed(ActionEvent e) { 
    emptyPanel.removeAll(); 
    NumberPanel.removeAll(); 
    OperationPanel.removeAll(); 
    WritingZone.setText(null); 

現在,就個人而言,我將通過創建一個BasicCalculatorPaneAdvancedCalculatorPane開始。每個包含所需的用戶界面元素和邏輯,使他們工作。然後,當用戶在它們之間進行選擇時,刪除屏幕上的計算器窗格,並創建所需計算器的新實例並添加它。同樣,你可以保持達到的一個實例,但提供了一個「重置」選項,這將讓你重新設置界面,但將節省您不必創建這些面板的多個實例

+0

它工作。如你所說,得到了我期待的結果。 – Javabeginner

+0

很高興幫助... – MadProgrammer

1

試試這個

yourPanel.getContentPane().removeAll();//clean main screen Frame 
yourPanel.add(newContent);//add the desire Panel 
yourPanel.revalidate(); 
+0

考慮到OP調用'pack','validate'是無用的,除此之外,通常'revalidate'更有用;) – MadProgrammer

0

請試試這個

public class Calculator extends JFrame implements ActionListener{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    JPanel NumberPanel = new JPanel(); 
    String [] ButtonString = {"7","8","9","4","5","6","1","2","3","0",".","+/-"}; 
    JButton ButtonArray [] = new JButton[ButtonString.length]; 
    JPanel OperationPanel = new JPanel(); 
    String [] OperationString = {"Erase","Ac","*","/","+","-","Ans","="}; 
    JButton [] OperationArray = new JButton [OperationString.length]; 
    JTextField WritingZone = new JTextField(27); 
    JButton b1 = new JButton("Ok"); 
    JButton b2 = new JButton("Exit"); 
    double first = 0; 
    double second = 0; 
    double result = 0; 
    char operation = ' '; 
    Calculator(){ 
     super("Project Wolverine"); 
     setLayout(new BorderLayout(5,5)); 
     JPanel radio1 = new JPanel(); 
     final JRadioButton r1 = new JRadioButton("Simple Calculator"); 
     final JRadioButton r2 = new JRadioButton("Advanced Calculator"); 
     ButtonGroup g1 = new ButtonGroup(); 
     g1.add(r1); 
     g1.add(r2); 
     radio1.setLayout(new GridLayout (3,1,5,5)); 
     radio1.add(r1); 
     radio1.add(r2); 
     radio1.setBorder(BorderFactory.createTitledBorder("Choose a calculator")); 
     JPanel panel2 = new JPanel(); 
     panel2.setLayout(new GridLayout(1,2,5,5)); 
     panel2.add(b1); 
     panel2.add(b2); 
     radio1.add(panel2); 
     add(BorderLayout.WEST, radio1); 
     final JPanel emptyPanel = new JPanel(); 
     emptyPanel.setVisible(false); 
     emptyPanel.setLayout(new FlowLayout()); 
     add(BorderLayout.EAST, emptyPanel); 


     b1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 

       if (e.getSource()==b1){ 

       if (r1.isSelected()){ 
        for (int a = 0 ; a < ButtonArray.length ; a++){ 
         ButtonArray[a]= new JButton (ButtonString[a]); 
        } 
        NumberPanel.removeAll(); 
        NumberPanel.setLayout(new GridLayout(4,3,5,5)); 
        for (int a = 0 ; a < ButtonArray.length ; a++){ 
         NumberPanel.add(ButtonArray[a]); 
        } 

        for (int a = 0 ; a < OperationArray.length ; a++){ 
         OperationArray[a]= new JButton(OperationString[a]); 
        } 
        OperationPanel.removeAll(); 
        OperationPanel.setLayout(new GridLayout(4,2,5,5)); 
        for (int a = 0 ; a < OperationArray.length ; a++){ 
         OperationPanel.add(OperationArray[a]); 
        } 

        JPanel Finalpanel = new JPanel(); 
        Finalpanel.setLayout(new FlowLayout()); 
        Finalpanel.add(NumberPanel);Finalpanel.add(OperationPanel); 


        WritingZone.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
        WritingZone.setBorder(BorderFactory.createLoweredSoftBevelBorder()); 
        WritingZone.setEditable(false); 
        WritingZone.setBackground(Color.white); 
        JPanel TextPanel = new JPanel(); 
        TextPanel.add(WritingZone); 


        for(int a = 0 ; a < ButtonArray.length; a++){ 
         ButtonArray[a].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           for (int a = 0; a < ButtonArray.length-1; a++){ 
            if(e.getSource()==ButtonArray[a]){ 
             String temp = WritingZone.getText(); 
             WritingZone.setText(temp+ButtonArray[a].getText()); 
            } 
           } 
          } 
         }); 
        } 
        ButtonArray[11].addActionListener(new ActionListener(){ 
         public void actionPerformed(ActionEvent e){ 
          if(e.getSource()==ButtonArray[11]){ 
           StringBuilder temp = new StringBuilder(); 
           temp.append(WritingZone.getText()); 
           WritingZone.setText(""); 
           for (int a = 0; a < temp.length(); a++){ 
            if (temp.charAt(a)=='-'){ 
             temp.deleteCharAt(a); 
             WritingZone.setText(temp.toString()); 
            } 
            else{ 
             WritingZone.setText("-"+temp.toString()); 
            } 
           } 
          } 
         } 
        }); 


         OperationArray[0].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           if(e.getSource()==OperationArray[0]){ 
            String temp = WritingZone.getText(); 
            String temp2 = ""; 
            WritingZone.setText(""); 
            for (int x = 0 ; x < temp.length()-1 ; x++){ 
             temp2+=(Character.toString(temp.charAt(x))); 
            } 
            WritingZone.setText(temp2); 
           } 
          } 
         }); 

         OperationArray[1].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           if (e.getSource()==OperationArray[1]){ 
            WritingZone.setText(""); 
            first = 0; 
            second = 0; 
           } 
          } 
         }); 

         OperationArray[2].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation=OperationArray[2].getText().charAt(0); 
          } 
         }); 

         OperationArray[3].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation=OperationArray[3].getText().charAt(0); 
          } 
         }); 

         OperationArray[4].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation=OperationArray[4].getText().charAt(0); 
          } 
         }); 

         OperationArray[5].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           first = Double.valueOf(WritingZone.getText()); 
           WritingZone.setText(""); 
           operation=OperationArray[5].getText().charAt(0); 
          } 
         }); 

         OperationArray[6].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           String temp = String.valueOf(result); 
           String temp2 = WritingZone.getText(); 
           WritingZone.setText(""); 
           WritingZone.setText(temp2+""+temp); 
          } 
         }); 


         OperationArray[7].addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent e){ 
           if (operation=='*'){ 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            result=(first*second); 
            String temp2 = String.valueOf(result); 
            WritingZone.setText(temp2); 
           } 
           else if(operation=='/'){ 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            if(second<0){ 
             result=(first/second); 
             String temp2 = String.valueOf(result); 
             WritingZone.setText(temp2); 

            } 
            else if (second>0){ 
             result=(first/second); 
             String temp2 = String.valueOf(result); 
             WritingZone.setText(temp2); 

            } 
            else if (second==0) 
             WritingZone.setText("Error! Can't divide by 0"); 
           } 
           else if (operation=='+'){ 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            result=(first+second); 
            String temp2 = String.valueOf(result); 
            WritingZone.setText(temp2); 
           } 
           else if (operation=='-'){ 
            String temp = WritingZone.getText(); 
            WritingZone.setText(""); 
            second = Double.valueOf(temp); 
            result=(first-second); 
            String temp2 = String.valueOf(result); 
            WritingZone.setText(temp2); 
           } 
          } 
         }); 




        final JPanel AllPanel = new JPanel(); 
        AllPanel.setLayout(new BorderLayout(5,5)); 
        AllPanel.add(BorderLayout.NORTH, TextPanel); 
        AllPanel.add(BorderLayout.CENTER, Finalpanel); 
        AllPanel.setBorder(BorderFactory.createTitledBorder("Simple  Calculator")); 
        emptyPanel.removeAll(); 
        emptyPanel.add(AllPanel); 
        emptyPanel.setVisible(true); 
        pack(); 


      }} 
     } 



     }); 

     b2.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       if(e.getSource()==b2) 
        System.exit(0); 
      } 
     });} 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 
    public static void main (String [] arg){ 






    JFrame frame = new Calculator(); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
    frame.pack(); 
    frame.setResizable(false);}} 

剛剛從emptyPanel,NumberPanel刪除所有組件,OperationPanel

emptyPanel.removeAll(); 
NumberPanel.removeAll(); 
OperationPanel.removeAll();