我正在开发一个小“点击器”,但是,如果我按下按钮,我应该得到 1+ 分数,但它不起作用!有什么办法可以重新加载或者其他什么吗? 这是我的代码:(ClickEvent)
public class event implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("PRESS");
game.timesClicked.add(1);
points.setText(game.seePoints);
}
}
这是我的 JFrame:
public class game extends JFrame
{
public static JButton buttonStart;
JButton buttonCredits;
JButton buttonBack;
JButton buttonLeave;
public static JFrame panel = new game();
public static ArrayList<Integer> timesClicked = new ArrayList<Integer>();
public static JLabel label1;
public static JLabel points;
public static String seePoints = "Deine Knöpfe: " + timesClicked.size();
public game()
{
setLayout(null);
label1 = new JLabel("ButtonClicker");
points = new JLabel(seePoints);
points.setFont(new Font("Tahoma", Font.BOLD, 15));
points.setBounds(0, 0, 200, 200);
label1.setFont(new Font("Tahoma", Font.BOLD, 50));
label1.setBounds(315, 50, 500, 200);
event e1 = new event();
JButton b = new JButton("KNOPF");
b.setBackground(new Color(96, 140, 247));
b.setForeground(Color.WHITE);
b.setFocusPainted(false);
b.setFont(new Font("Tahoma", Font.BOLD, 15));
b.setBounds( 402, 380, 180, 50 );
b.addActionListener(e1);
add(b);
add(label1);
add(points);
}
}
(抱歉我的英语不好)
最佳答案
public static String seePoints = "Deine Knöpfe: " + timesClicked.size();
这只在程序开始时被调用一次。当您添加到 timesClicked
时,它不会重新计算 seePoints
。
每次单击时,您都需要将此变量设置为正确的值。
关于java - 重新加载 JFrame 或 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40947220/