当我运行该程序时,GUI 就会出现。当我单击按钮时,屏幕上没有任何内容。在我的操作执行方法中,屏幕无法解析,因为我尚未在方法中声明它。screen 在我的 init 方法的开头声明。
我不明白第二种方法中的代码将如何应用于第一种方法(GUI)。
有小费吗?
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");
}
互换的青春
相关分类