2014-01-26 106 views
-3

我正在玩JFrames的樂趣,並不能完全得到一個面板來顯示一個靜態變量。我會很感激任何幫助。這裏是我使用的代碼:使JLabel可以訪問靜態變量?

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

public class JButtonTester 
{ 
    static int counter = 0; 
    public static void main(String[]args) 
    { 
     class ClickCounter implements ActionListener 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       counter++; 
       System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment"); 
      } 
     } 
     class ClickDecrement implements ActionListener 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       counter--; 
       System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment"); 
      } 
     } 
     JFrame firstFrame = new JFrame(); 
     JLabel counter = new JLabel("Count: " + counter); 
     JPanel firstPanel = new JPanel(); 

     JButton firstButton = new JButton("Click me to increase your count!"); 
     firstPanel.add(firstButton); 
     ActionListener firstListener = new ClickCounter(); 
     firstButton.addActionListener(firstListener); 

     JButton secondButton = new JButton("Click me to decrease your count!"); 
     firstPanel.add(secondButton); 
     ActionListener secondListener = new ClickDecrement(); 
     secondButton.addActionListener(secondListener); 

     firstFrame.add(firstPanel); 
     firstFrame.setSize(200, 120); 
     firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     firstFrame.setVisible(true); 
    } 
} 

我試圖訪問的變量是「計數器」。

+0

問題是什麼? –

+0

來自BlueJ的錯誤消息:「可變計數器可能未被初始化」 在這一行上:JLabel counter = new JLabel(「Count:」+ counter); – user3236859

回答

1

幾件事情你需要做的:

  1. 重命名您的字段:您有兩個名爲counter的變量。
  2. 其次,您需要將JLabel移動到actionPerformed以上,並聲明final,以便您可以在actionPerformed方法中訪問它。
  3. 我沒有看到你添加的JLabel到面板上,所以我說這行

這應該做你想要什麼:

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

public class JButtonTester { 

    static int counter = 0; 

    public static void main(String[] args) { 

     final JLabel counter_label = new JLabel(); 
     class ClickCounter implements ActionListener { 

      public void actionPerformed(ActionEvent event) { 
       counter++; 
       System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment"); 
       counter_label.setText("Count: " + counter); 
      } 
     } 
     class ClickDecrement implements ActionListener { 

      public void actionPerformed(ActionEvent event) { 
       counter--; 
       System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment"); 
       counter_label.setText("Count: " + counter); 
      } 
     } 
     JFrame firstFrame = new JFrame(); 
     JPanel firstPanel = new JPanel(); 
     firstPanel.add(counter_label); 

     JButton firstButton = new JButton("Click me to increase your count!"); 
     firstPanel.add(firstButton); 
     ActionListener firstListener = new ClickCounter(); 
     firstButton.addActionListener(firstListener); 

     JButton secondButton = new JButton("Click me to decrease your count!"); 
     firstPanel.add(secondButton); 
     ActionListener secondListener = new ClickDecrement(); 
     secondButton.addActionListener(secondListener); 

     firstFrame.add(firstPanel); 
     firstFrame.setSize(200, 120); 
     firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     firstFrame.setVisible(true); 
    } 
} 
1

從另一個類訪問一個靜態變量將意味着繼續變量名和類的名稱,因爲靜態意味着它是一個類變量。因此,由於計數器是類JButtonTester的靜態變量,從另一個類訪問計數器,你會說JButtonTester.counter

JLabel counter = new JLabel("Count: " + JButtonTester.counter); 
+0

哇,謝謝你的快速和超級有用的迴應,喬希。有效!不過,我不太確定爲什麼這是必要的。不是主要方法範圍內的變量嗎? – user3236859

+1

此解決方案的工作原理是因爲您有兩個名爲'counter'的變量。一個是int,另一個是JLabel。如果您將您的標籤重命名爲「counter_label」,它也可以工作。 – ryvantage

+0

感謝您的澄清@ryvantage –

1

你在這裏行是錯誤的

JLabel counter = new JLabel("Count: " + counter); 

計數器參考與JLabel的你正在創建,使用不同的變量名