Java 如何使这个JFrame代码更简单?
我刚刚开始学习JFrame,我试着做一个简单的计算器。它可以工作,但也有重复性,必须为每个按钮创建一个ActionListener。有没有办法让它变得更简单Java 如何使这个JFrame代码更简单?,java,swing,Java,Swing,我刚刚开始学习JFrame,我试着做一个简单的计算器。它可以工作,但也有重复性,必须为每个按钮创建一个ActionListener。有没有办法让它变得更简单 public class gui extends JFrame { private JButton one; private JButton two; private JButton three; private JButton four; private JButton five; priv
public class gui extends JFrame
{
private JButton one;
private JButton two;
private JButton three;
private JButton four;
private JButton five;
private JButton six;
private JButton seven;
private JButton eight;
private JButton nine;
public double first = 0;
public double second = 0;
public double sum = 0;
public gui()
{
super("title");
setLayout(new FlowLayout());
one = new JButton("1"); //makes a new button to click.
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
one.addActionListener( //MAKES NEW INNER CLASS TO DEFINE WHAT ONE DOES
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// checks if clicked.
if (first == 0) first = 1;
else second = 1;
if (first != 0 && second != 0)
{
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
});
two.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 2;
}else {
second = 2;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
three.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 3;
}else {
second = 3;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
four.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 4;
}else {
second = 4;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
five.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 5;
}else {
second = 5;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
six.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 6;
}else {
second = 6;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
seven.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 7;
}else {
second = 7;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
eight.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 8;
}else {
second = 8;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
nine.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 9;
}else {
second = 9;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
}
}
我也找不到一种方法来“组合”我拥有的两个数字。如果用户按9和2,我想把这两个数字合并成92。我该怎么做呢?要消除大量重复,可以做的一件事是对所有按钮使用一个
ActionListener
。您可以在每个按钮上设置actionCommand
属性,操作侦听器将使用该属性来检测按下的按钮
下面是一个一般性的例子:
private ActionListener btnListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
switch (event.getActionCommand()) {
case "0":
case "1":
// ...
case "9":
enterDigit(event.getActionCommand());
break;
}
}
};
public gui(){
one = new JButton("1");
one.setActionCommand("1");
one.addActionListener(btnListener);
two = new JButton("2");
two.setActionCommand("2");
two.addActionListener(btnListener);
// ...
}
至于如何组合单独的数字按钮以形成更大的数字,根据您想要存储数字的方式,可能有几种方法。使用
String
s开始可能最简单,在这种情况下,您只需将包含输入数字(来自JButton
的action命令)的字符串
附加到先前输入数字的现有字符串
:
private String numberInDisplay = "";
// remember enterDigit from the ActionListener above? This is it...
private void enterDigit(String digit){
numberInDisplay = numberInDisplay + digit;
}
如果要使用数字类型,例如长的,只需将现有数字乘以10,然后将新数字的值相加:
private long numberInDisplay = 0;
private void enterDigit(String digit){
numberInDisplay = numberInDisplay * 10 + Long.valueOf(digit);
当然,这是一个相当简单的例子。如果您想处理小数点、负号、科学符号等,则会有一些额外的复杂情况,但至少应该让您开始走上正确的道路。一个明显的可能性是创建一个ActionListener类(而不是匿名类),该类接受一个参数(如构造函数中的参数)这会告诉它要使用的数字,而不是像现在这样在每个单独的类中硬编码数字。然而,我认为你的问题太离谱了。在代码审查网站上可能会更好。您现在确实应该使用数组。代码将大约缩短9倍,并且更易于扩展。如果您需要500个
JButton
s,您会怎么做?然后你意识到你想改变一件小事?你必须在500个地方做出改变。阵列解决了这个问题。