2016-12-03 109 views
1

我開發一點點「咔噠咔咔」,但是,如果我按下按鈕,我應該得到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); 

} 
} 

(對不起我的英文不好)

+1

我應該注意到'public static'變量遍地都是編程實踐不佳。查看「封裝」以獲取更多信息。 –

回答

1
public static String seePoints = "Deine Knöpfe: " + timesClicked.size(); 

這只是在程序開始時被調用一次。當您添加到timesClicked時,它不會重新計算seePoints

每次單擊時,您都需要將此變量設置爲正確的值。