2014-01-26 4 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. 두 번째로 을 actionPerformed 메서드 위로 이동하고 final을 선언하여 actionPerformed 메서드 내에서 액세스 할 수 있습니다. 당신이 패널에 JLabel을 추가 곳
  3. 나는 보지 않는다, 그래서 이것은 당신이 원하는 일을해야

그 라인 추가 :

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

다른 클래스의 정적 변수에 액세스하는 것은 static이라는 것은 클래스 변수라는 의미이므로 클래스 이름을 사용하여 변수 이름을 계속 처리한다는 의미입니다. 카운터 클래스 JButtonTester의 정적 변수이기 때문에 따라서 다른 클래스에서 카운터에 액세스 할 수는

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

카운터가 참조 여기 JButtonTester.counter

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

와우, 빠르고 유용한 답변, 조쉬 주셔서 감사합니다. 그것은 효과가있다! 왜 이렇게 필요한지 잘 모르겠습니다. 변수가 main 메소드의 범위 안에 있지 않습니까? – user3236859

+1

이 솔루션은 'counter'라는 두 개의 변수가 있기 때문에 작동합니다. 하나는'int'이고 다른 하나는'JLabel'입니다. 라벨의 이름을'counter_label'로 변경하면 잘 작동합니다. – ryvantage

+0

설명해 주셔서 감사합니다. @ 효용 –

1

당신이있는 거 라인이 잘못 말할 것 작성중인 JLabel의에, 당신이해야 할 다른 변수 이름을

관련 문제