HINWEIS: Mein Englisch ist nicht das Beste Also bitte nicht zu viel Grammatik Fehler.Java; Wie bekomme ich die Variable außerhalb des Action-Listeners?
Hey da, Java Anfänger hier, Wie auch immer, ich habe mein CPS Tester Programm programmiert, nur als erstes Mini Programm. Wie auch immer, diese Frage gestellt haben, bevor aber, Ich brauche eine Variable außerhalb eines Action Code erhalten:
public static void startB() {
Font f = new Font(null, Font.BOLD , 0);
Font size = f.deriveFont(20f);
JLabel text = new JLabel("");
text.setPreferredSize(new Dimension(250,250));
text.setFont(size);
JButton b = new JButton();
JFrame cps = new JFrame("CLICK");
cps.setPreferredSize(new Dimension(400,400));
cps.setLocationRelativeTo(null);
cps.setLayout(new FlowLayout());
b.setText("<html> CLICK ME <br> As much as you can! <html> ");
cps.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
cps.getContentPane().add(b , BorderLayout.CENTER);
cps.getContentPane().add(text, BorderLayout.CENTER);
cps.pack();
cps.setVisible(true);
text.setText("<html> Starting in... <br> 3<html>");
try {
TimeUnit.SECONDS.sleep((long)1.0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setText("<html> Starting in... <br> 2<html>");
try {
TimeUnit.SECONDS.sleep((long)1.0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setText("<html> Starting in... <br> 1<html>");
try {
TimeUnit.SECONDS.sleep((long)1.0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setText("<html> CLICK! <html>");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double clicks = 0;
clicks++;
// How to get Clicks variable out of the actionListener?
}
});
try {
TimeUnit.SECONDS.sleep((long)10.0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//I need get the Clicks Variable to here.
}
Wenn Sie mir helfen können, sich auf die Post antworten Bitte. Vielen Dank.
@davidxxx Ich habe nur eine Antwort auf die Frage und bereits erwähnt, dass es nicht der beste Weg ist, es zu tun. – dpr
Ah sorry ich schlecht lesen. Ich habe das Gegenteil verstanden. Es ist gut als Alternative mit dieser Warnung. – davidxxx