如何清除面板?

时间:2014-05-14 06:34:33

标签: java swing

我有一个以空面板开头的程序然后当我按下一个按钮时,面板将被其他包含元素的面板填充。

但是,如果我再次按下按钮,它会重叠并叠加成为非常难看的东西。

每次按下按钮时,如何清除面板,使其不会重叠。 请注意,我使用了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);
    }
}

3 个答案:

答案 0 :(得分:1)

首先来看一下所涉及的一些面板......

NumberPanelOperationPanelWritingZone是实例变量,emptyPanefinal本地变量。

其中每一项都需要&#34;清除&#34;或重建。

一个简单的解决方案是将emptyPane作为唯一的实例变量,并触发每个emptyPane.removeAll()的简单调用ActionListener,但就目前情况而言,您将继续必须在每个前面提到的面板上调用removeAll,例如......

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

现在,就个人而言,我首先要创建BasicCalculatorPaneAdvancedCalculatorPane。每个都包含所需的UI元素和逻辑,使它们工作。然后,当用户在它们之间进行选择时,删除屏幕上的计算器窗格并创建所需计算器的新实例并添加它。同样,您可以维持一个覆盖范围的实例,但提供&#34;重置&#34;选项,它允许您重置界面,但可以使您不必创建这些面板的更多实例

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分: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();