gpt4 book ai didi

java - 更新标签内的变量

转载 作者:行者123 更新时间:2023-12-01 17:35:30 25 4
gpt4 key购买 nike

 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestSwingListeners1 {

private static int cnt1;
private static int cnt2;
public static void main(String[] args) {
JFrame fr1 = new JFrame("Swing Window");
Container cp;
JButton bt1;
JButton bt2;
cnt1 = 0;
cnt2 = 0;
String scr = null;
String wnr = null;
JButton btOK, btCancel;
fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr1.setSize(300, 200);
fr1.setResizable(false);
cp = fr1.getContentPane();
cp.setLayout(new GridLayout(5,1));
btOK = new JButton("AC Milan");
btCancel = new JButton("Real Madrid");
JLabel lbl1 = new JLabel("Result: " + cnt1 + "X" + cnt2);
JLabel lbl2 = new JLabel("Last Scorer: " + scr);
JLabel lbl3 = new JLabel("Winner: " + wnr);
cp.add(btOK);
cp.add(btCancel);
cp.add(lbl1);
cp.add(lbl2);
cp.add(lbl3);
//lbl1.setText(displayText);

btOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
//String displayText = "" + 1;
cnt1++;
}
});

btCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
cnt2++;
}
});
fr1.show();
}

当我按下btOK按钮时,我希望cnt1增加,与btCancel相同 - 增加cnt2当它被按下时。

怎么做?

最佳答案

您需要注册ActionListener Read More

关于java - 更新标签内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com