重新加载JFrame或JLabel
问题描述:
我开发一点点“咔哒咔咔”,但是,如果我按下按钮,我应该得到1+分数,它不工作!有什么方法可以重新加载或其他任何东西? 继承人我的代码:(ClickEvent)重新加载JFrame或JLabel
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
。
每次单击时,您都需要将此变量设置为正确的值。
我应该注意到'public static'变量遍地都是编程实践不佳。查看“封装”以获取更多信息。 –