我有可能微不足道的問題,但我花了幾個小時尋找答案。爪哇鞦韆 - 操縱的GUI
我想創建一個按鈕(ENTER按鈕),一旦點擊,刪除GUI上的某些組件(如numpad)。問題在於,定義一旦點擊按鈕後執行的指令的類沒有看到組件。我試圖給這個類添加implements ATM
,但是然後控制檯返回了非常奇怪的錯誤(執行時)。有沒有「乾淨」的方式來做到這一點?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ATM extends JFrame{
// Container
int state = 0; // PIN screen
// ELEMENTS
JPanel container = new JPanel();
JTextArea display = new JTextArea("Please enter your PIN", 10, 50);
JTextField inputArea = new JTextField("");
JPanel buttons = new JPanel();
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton clear = new JButton("Clear");
JButton enter = new JButton("Enter");
JButton quit = new JButton("Quit");
// EVENTS
ButtonPresser buttonPress = new ButtonPresser(inputArea, display);
EnterPresser enterPress = new EnterPresser(inputArea, display, state, buttons);
ATM(){
super("ATM Cash Machine");
buildGUI();
pack();
setVisible(true);
}
private void buildGUI(){
// EVENT BINDINGS
one.addActionListener(buttonPress);
two.addActionListener(buttonPress);
three.addActionListener(buttonPress);
four.addActionListener(buttonPress);
five.addActionListener(buttonPress);
six.addActionListener(buttonPress);
seven.addActionListener(buttonPress);
eight.addActionListener(buttonPress);
nine.addActionListener(buttonPress);
zero.addActionListener(buttonPress);
clear.addActionListener(buttonPress);
quit.addActionListener(buttonPress);
enter.addActionListener(enterPress);
// ELEMENT SETTINGS
inputArea.setEditable(false);
display.setEditable(false);
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(display);
container.add(inputArea);
// Numeric pad
buttons.setLayout(new GridLayout(5,3));
buttons.add(one);
buttons.add(two);
buttons.add(three);
buttons.add(four);
buttons.add(five);
buttons.add(six);
buttons.add(seven);
buttons.add(eight);
buttons.add(nine);
buttons.add(clear);
buttons.add(zero);
buttons.add(enter);
buttons.add(quit);
container.add(buttons);
add(container, BorderLayout.NORTH);
}
// Main method
public static void main(String[] args){
ATM atm = new ATM();
}
}
class ButtonPresser implements ActionListener{
private JTextField iField;
private JTextArea oArea;
ButtonPresser(JTextField in, JTextArea out){
iField = in;
oArea = out;
}
public void actionPerformed(ActionEvent e){
switch(e.getActionCommand()){
case "Quit":
System.exit(0);
break;
case "Clear":
iField.setText("");
break;
default:
String fieldText = iField.getText();
if(fieldText.length() < 4){
iField.setText(fieldText+e.getActionCommand());
}
break;
}
}
}
class EnterPresser implements ActionListener{
private JTextField iField;
private JTextArea oArea;
private int state;
private JPanel buttons;
private final String PIN = "1234";
EnterPresser(JTextField in, JTextArea out, int st, JPanel but){
iField = in;
oArea = out;
state = st;
buttons = but;
}
public void actionPerformed(ActionEvent e){
if(state == 0){
String fieldText = iField.getText();
if(fieldText.equals(PIN)){
iField.setText("");
state = 1;
uiState0To1();
}
}
}
public void uiState0To1(){
buttons.remove(one);
}
}
我一直有理解靜態和非靜態的東西的問題。無論如何,它工作,謝謝! – Jojko
已經足夠壞了,你建議將字段設置爲'public',但是'static',這只是顯而易見的錯誤和令人難以置信的糟糕的建議,這兩個問題 – MadProgrammer
我不知道它是怎麼回事。如果他想操縱其他班級的組件,那麼這是做這件事的方法之一......我的答案沒有錯。 – znick