我刚刚开始学习JFrame,我试着做一个简单的计算器。它可以工作,但它也是重复的,必须为每个按钮创建一个ActionListener。有没有办法让它变得更简单?
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。我该怎么做呢?
发布于 2018-06-04 05:12:24
你可以做的一件事就是为你所有的按钮使用一个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
的actionCommand )的String
附加到先前输入数字的现有String
上:
private String numberInDisplay = "";
// remember enterDigit from the ActionListener above? This is it...
private void enterDigit(String digit){
numberInDisplay = numberInDisplay + digit;
}
如果要使用数字类型,如long
,只需将现有数字乘以十,然后将新数字的值相加:
private long numberInDisplay = 0;
private void enterDigit(String digit){
numberInDisplay = numberInDisplay * 10 + Long.valueOf(digit);
当然,这是一个相当简单的例子。如果您想要处理小数点、负号、科学记数法等,将会有一些额外的复杂性,但它至少应该让您沿着正确的道路开始。
https://stackoverflow.com/questions/50670688
复制相似问题