我有一個程序,以一個空面板開始,然後當我按下一個按鈕,面板將被填充一些其他面板,其中包含的元素。如何清除面板?
但是,如果我再次按下按鈕,它會一遍又一遍地重疊和堆疊,變得非常難看。
如何每次按下按鈕時清除面板,使其不會重疊。 請注意,我已經使用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);
}
}
它工作。如你所說,得到了我期待的結果。 – Javabeginner
很高興幫助... – MadProgrammer