[英]Issue with text not showing up on Java GUI calculator
When i run the program the GUI shows up.当我运行程序时,图形用户界面就会出现。 When i click on the buttons nothing populates in the screen.
当我单击按钮时,屏幕中没有任何内容。 In my action performed method, screen does not resolve because i have not declared it in the method.
在我执行的操作方法中,屏幕无法解析,因为我没有在方法中声明它。 screen is declared in my init method at the beginning.
screen 在我的 init 方法中一开始就声明了。
I don't understand how my code in the second method will be applied to the first method (the GUI).我不明白我在第二种方法中的代码将如何应用于第一种方法(GUI)。
any tips?有小费吗?
import acm.program.*;
import javax.swing.*;
import java.awt.*; // for graphics
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import acm.gui.*;
public class Calculator extends DialogProgram implements ActionListener{
public void init() {
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bm,bd,ba,bs,bdec,bc;
DoubleField screen;
//create a panel
JPanel P= new JPanel(); // main panel
JPanel P1= new JPanel(); // panel for buttons
JPanel P2= new JPanel(); // panel for screen
// adding panel to dialog box
add(P); // main panel
P.add(P2); // adding screen
P.add(P1); // adding panel of buttons
// set panel color
P.setBackground(Color.yellow);
// grid layout for buttons
P1.setLayout(new GridLayout(4,4,4,4)); // layout for button panel
P.setLayout(new GridLayout(2,1)); // layout for main panel
// create screen
screen = new DoubleField(); // creating screen
screen.setPreferredSize(new Dimension(200,40)); // setting screen size
screen.setEditable(false); // screen can not be edited
// integer buttons
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
// operator buttons
bm = new JButton("*");
bd= new JButton("/");
ba = new JButton("+");
ba.addActionListener(this);
bs = new JButton("-");
// decimal button
bdec = new JButton(".");
// clear button
bc = new JButton("C");
// adding buttons to panel
P2.add(screen);
P1.add(b7);
P1.add(b8);
P1.add(b9);
P1.add(bm);
P1.add(b4);
P1.add(b5);
P1.add(b6);
P1.add(bd);
P1.add(b1);
P1.add(b2);
P1.add(b3);
P1.add(ba);
P1.add(bc);
P1.add(b0);
P1.add(bdec);
P1.add(bs);
// adding action listeners
bm.addActionListener(this);
bd.addActionListener(this);
bs.addActionListener(this);
b9.addActionListener(this);
b8.addActionListener(this);
b7.addActionListener(this);
b6.addActionListener(this);
b5.addActionListener(this);
b4.addActionListener(this);
b3.addActionListener(this);
b2.addActionListener(this);
b1.addActionListener(this);
b0.addActionListener(this);
bdec.addActionListener(this);
bc.addActionListener(this);
addActionListeners();
} // close
public void actionPerformed (ActionEvent e) {
String clicked =e.getActionCommand();
if (clicked.equals("ba")) {
screen.setText("+");
}
}
} /*Class*/
Move the declarations of the variables for all your GUI components out of the init()
method, but still within the Calculator
class, so they they will be visible to other methods (like actionPerformed
) that might need to access them:将所有 GUI 组件的变量声明移出
init()
方法,但仍在Calculator
class 中,因此它们将对可能需要访问它们的其他方法(如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("+");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.