1 回答

TA貢獻1797條經(jīng)驗 獲得超6個贊
將所有 GUI 組件的變量聲明移出init()方法,但仍在Calculator類內(nèi),這樣它們對于actionPerformed可能需要訪問它們的其他方法(例如 )是可見的:
public class Calculator extends DialogProgram implements ActionListener{
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bm,bd,ba,bs,bdec,bc;
DoubleField screen;
JPanel P, P1, P2;
public void init() {
P= new JPanel(); // main panel
P1= new JPanel(); // panel for buttons
P2= new JPanel(); // panel for screen
// continue setting up the rest of the GUI...
}
public void actionPerformed (ActionEvent e) {
String clicked = e.getActionCommand();
if (clicked.equals("ba")) {
screen.setText("+");
}
}
}
添加回答
舉報