由于某些原因,我的计算器程序无效。为了清楚起见,这是一个修改过的计算器,我正在将用户点击的两位数相乘,并在点击“计算”按钮时显示答案。错误是我正在尝试使用的用户单击按钮时使用的整数没有被使用。出于某种原因,它说小时和ig尚未解决,但我在上面宣布......任何解决方案都会有所帮助! 这是我的代码:
package Calculator;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Frame1 {
private JFrame frmIronGolemSpawner;
private JTextField txtHours;
private JTextField txtIronGolemSpawners;
private JButton button;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9;
private JButton button_10;
private JButton button_11;
private JButton button_12;
private JButton button_13;
private JButton button_14;
private JButton button_15;
private JButton button_16;
private JButton button_17;
private JButton button_18;
private JButton button_19;
private JButton button_20;
private JButton button_21;
private JButton button_22;
private JButton button_23;
private JButton button_24;
private JButton button_25;
private JButton button_26;
private JButton button_27;
private JButton button_28;
private JButton button_29;
private JButton button_30;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frmIronGolemSpawner.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmIronGolemSpawner = new JFrame();
frmIronGolemSpawner.setTitle("Iron Golem Spawner Calculator");
frmIronGolemSpawner.setBounds(200, 200, 900, 600);
frmIronGolemSpawner.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmIronGolemSpawner.getContentPane().setLayout(null);
txtHours = new JTextField();
txtHours.setEditable(false);
txtHours.setFont(new Font("Tahoma", Font.BOLD, 26));
txtHours.setText("Hours Days(1, 2, 3, 4, 5, 6, 7, 14, 21) Weeks of sleep (1, 2, 4)");
txtHours.setBounds(0, 0, 885, 61);
frmIronGolemSpawner.getContentPane().add(txtHours);
txtHours.setColumns(10);
txtIronGolemSpawners = new JTextField();
txtIronGolemSpawners.setEditable(false);
txtIronGolemSpawners.setText("Iron Golem Spawners");
txtIronGolemSpawners.setFont(new Font("Tahoma", Font.BOLD, 26));
txtIronGolemSpawners.setColumns(10);
txtIronGolemSpawners.setBounds(0, 266, 781, 68);
frmIronGolemSpawner.getContentPane().add(txtIronGolemSpawners);
JButton btnNewButton = new JButton("1");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
btnNewButton.setBounds(0, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(btnNewButton);
button = new JButton("3");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 3;
}
});
button.setBounds(55, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button);
button_1 = new JButton("6");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 6;
}
});
button_1.setBounds(111, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_1);
button_2 = new JButton("9");
button_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 9;
}
});
button_2.setBounds(166, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_2);
button_3 = new JButton("24");
button_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 24;
}
});
button_3.setBounds(222, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_3);
button_4 = new JButton("48");
button_4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 48;
}
});
button_4.setBounds(279, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_4);
button_5 = new JButton("72");
button_5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 72;
}
});
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
button_5.setBounds(334, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_5);
button_6 = new JButton("96");
button_6.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 96;
}
});
button_6.setBounds(390, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_6);
button_7 = new JButton("120");
button_7.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 120;
}
});
button_7.setBounds(445, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_7);
button_8 = new JButton("144");
button_8.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 144;
}
});
button_8.setBounds(501, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_8);
button_9 = new JButton("168");
button_9.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 168;
}
});
button_9.setBounds(558, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_9);
button_10 = new JButton("336");
button_10.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
int hour = 336;
}
});
button_10.setBounds(613, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_10);
button_11 = new JButton("672");
button_11.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 672;
}
});
button_11.setBounds(669, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_11);
button_12 = new JButton("63");
button_12.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 63;
}
});
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_12.setBounds(724, 59, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_12);
button_13 = new JButton("126");
button_13.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 126;
}
});
button_13.setBounds(780, 59, 57, 208);
frmIronGolemSpawner.getContentPane().add(button_13);
button_14 = new JButton("252");
button_14.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int hour = 252;
}
});
button_14.setBounds(838, 59, 57, 208);
frmIronGolemSpawner.getContentPane().add(button_14);
button_15 = new JButton("1");
button_15.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 1;
}
});
button_15.setBounds(0, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_15);
button_16 = new JButton("2");
button_16.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 2;
}
});
button_16.setBounds(55, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_16);
button_17 = new JButton("3");
button_17.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 3;
}
});
button_17.setBounds(111, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_17);
button_18 = new JButton("4");
button_18.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 4;
}
});
button_18.setBounds(166, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_18);
button_19 = new JButton("5");
button_19.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 5;
}
});
button_19.setBounds(222, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_19);
button_20 = new JButton("6");
button_20.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 6;
}
});
button_20.setBounds(279, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_20);
button_21 = new JButton("7");
button_21.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 7;
}
});
button_21.setBounds(334, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_21);
button_22 = new JButton("8");
button_22.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 8;
}
});
button_22.setBounds(390, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_22);
button_23 = new JButton("9");
button_23.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 9;
}
});
button_23.setBounds(445, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_23);
button_24 = new JButton("10");
button_24.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 10;
}
});
button_24.setBounds(501, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_24);
button_25 = new JButton("11");
button_25.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 11;
}
});
button_25.setBounds(558, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_25);
button_26 = new JButton("12");
button_26.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 12;
}
});
button_26.setBounds(613, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_26);
button_27 = new JButton("14");
button_27.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 14;
}
});
button_27.setBounds(669, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_27);
button_28 = new JButton("16");
button_28.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 16;
}
});
button_28.setBounds(724, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_28);
button_29 = new JButton("32");
button_29.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 32;
}
});
button_29.setBounds(780, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_29);
button_30 = new JButton("64");
button_30.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 64;
}
});
button_30.setBounds(828, 333, 57, 220);
frmIronGolemSpawner.getContentPane().add(button_30);
JButton btnCalculate = new JButton("Calculate");
btnCalculate.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
int answer;
answer = hour * ig * 6000;
JOptionPane.showMessageDialog(null, answer);
}
});
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnCalculate.setBounds(780, 266, 110, 68);
frmIronGolemSpawner.getContentPane().add(btnCalculate);
}
}
答案 0 :(得分:2)
您的问题与名为 scope 的概念有关。
String group=""+session.getAttribute("group");
和int ig
在匿名int hour
类的mouseClicked
内定义:
MouseAdapter
但是当你尝试在
中访问它们时button_30.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int ig = 64;
}
});
btnCalculate.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
int answer;
answer = hour * ig * 6000;
JOptionPane.showMessageDialog(null, answer);
}
});
和hour
未在此范围内定义。 Here是我对范围概念的回答,但您可以通过Google轻松获取更多信息。
要解决您的问题,您只需将它们定义为字段:
ig
并且在鼠标侦听器内使用public class Frame1 {
private int hour, ig;
private JFrame frmIronGolemSpawner;
private JTextField txtHours;
private JTextField txtIronGolemSpawners;
代替ig = ...
,int ig = ...
使用相同内容。