2012-07-17 3 views
1

객체 지향 방식으로 GUI를 설계하는 데 문제가 있습니다. 다음 코드는 좀 더 명확하게 내 질문을 표현하는 데 도움이됩니다Java GUI 설계시 문제점

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

public class QuoteOptionsPanel extends JPanel 
{ 
    private JLabel quote; 
    private JRadioButton comedy, philosophy, carpentry; 
    private String comedyQuote, philosophyQuote, carpentryQuote; 
    //----------------------------------------------------------------- 
    // Sets up a panel with a label and a set of radio buttons 
    // that control its text. 
    //----------------------------------------------------------------- 
    public QuoteOptionsPanel() 
    { 
     comedyQuote = "Take my wife, please."; 
     philosophyQuote = "I think, therefore I am."; 
     carpentryQuote = "Measure twice. Cut once."; 

     quote = new JLabel (comedyQuote); 
     quote.setFont (new Font ("Helvetica", Font.BOLD, 24)); 

     comedy = new JRadioButton ("Comedy", true); 
     comedy.setBackground (Color.green); 

     philosophy = new JRadioButton ("Philosophy"); 
     philosophy.setBackground (Color.green); 

     carpentry = new JRadioButton ("Carpentry"); 
     carpentry.setBackground (Color.green); 

     ButtonGroup group = new ButtonGroup(); 
     group.add (comedy); 
     group.add (philosophy); 
     group.add (carpentry); 

     QuoteListener listener = new QuoteListener(); 
     comedy.addActionListener (listener); 
     philosophy.addActionListener (listener); 
     carpentry.addActionListener (listener); 

     add (quote); 
     add (comedy); 
     add (philosophy); 
     add (carpentry); 

     setBackground (Color.green); 
     setPreferredSize (new Dimension(300, 100)); 
    } 
    //***************************************************************** 
    // Represents the listener for all radio buttons. 
    //***************************************************************** 
    private class QuoteListener implements ActionListener 
    { 
     //-------------------------------------------------------------- 
     // Sets the text of the label depending on which radio 
     // button was pressed. 
     //-------------------------------------------------------------- 
     public void actionPerformed (ActionEvent event) 
     { 
      Object source = event.getSource(); 
      if (source == comedy) 
       quote.setText (comedyQuote); 
      else 
       if (source == philosophy) 
        quote.setText (philosophyQuote); 
       else 
        quote.setText (carpentryQuote); 
     } 
    } 
} 

은 위의 코드는 단순히 각각의 견적에 해당하는 세 개의 라디오 버튼이있는 패널을 만듭니다. 또한 견적을 표시하는 레이블을 만듭니다. 버튼을 선택할 때마다 레이블의 텍스트가 해당 인용 부호로 설정됩니다. 나는이 코드를 잘 이해하고있다. 문제가 생겨서 수정하려고합니다. 같은 프로그램을 만들고 싶지만 라디오 버튼이 수직으로 겹쳐져 있다고 가정 해 봅시다. BoxPayel 클래스에서 정의한 BoxLayout이있는 패널에 라디오 버튼을 추가하여이 문제를 해결하기로 결정했다고 가정 해 보겠습니다. (그때 여전히 내 인용의 JLabel을 포함 할 것이다, 내 QuoteOptionsPanel에 BoxPanel를 추가합니다.) 을 그래서 내 BoxPanel의 코드는 다음과 같이 보일 수 있습니다 : 당신이 볼 수 그래서

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

public class BoxPanel extends JPanel 
{ 
    private JRadioButton comedy, philosophy, carpentry; 
    public BoxPanel() 
    { 
     setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); 
     setBackground (Color.green); 

     comedy = new JRadioButton ("Comedy", true); 
    comedy.setBackground (Color.green); 
    philosophy = new JRadioButton ("Philosophy"); 
    philosophy.setBackground (Color.green); 
    carpentry = new JRadioButton ("Carpentry"); 
    carpentry.setBackground (Color.green); 

    ButtonGroup group = new ButtonGroup(); 
    group.add (comedy); 
    group.add (philosophy); 
    group.add (carpentry); 

    QuoteListener listener = new QuoteListener(); 
    comedy.addActionListener (listener); 
    philosophy.addActionListener (listener); 
    carpentry.addActionListener (listener); 

    } 
    //***************************************************************** 
    // Represents the listener for all radio buttons. 
    //***************************************************************** 
    private class QuoteListener implements ActionListener 
    { 
     //-------------------------------------------------------------- 
     // Sets the text of the label depending on which radio 
     // button was pressed. 
     //-------------------------------------------------------------- 
     public void actionPerformed (ActionEvent event) 
     { 
      Object source = event.getSource(); 

      I do not know what to do here. 
     } 
    } 
} 

을 나는 방법을 몰랐다 내 QuoteListener 클래스를 정의하십시오. 게시 한 원래 프로그램과 동일한 기능을 수행하기를 원하지만 그렇게하는 방법을 잘 모르겠습니다. 견적을 표시하는 레이블은 QuoteOptionsPanel에 있으므로 액세스 할 수 없습니다. 본질적으로 나는 다른 패널의 구성 요소에 속하는 이벤트 리스너를 사용하여 한 패널에서 레이블을 변경하는 최적의 방법을 묻습니다. 나는 당신이 제공 할 수있는 도움에 대해 대단히 감사 할 것입니다. 제 질문을 명확하게 표명하지 않았다면 알려주십시오.

답변

4

이 문제를 해결하는 방법은 여러 가지가 있지만, 대부분의 경우 중요한 것은 참조를 얻고 사용하는 것입니다. 개인 필드가 공개 방법을 가지고 같이 JLabel의를 보유하고있는 클래스,

public void setQuoteLabelText(String text) { 
    quoteLabel.setText(text); 
} 

그런 다음 당신도 생성자 매개 변수 또는 setXXX(...)을 통해, 당신의 BoxPanel 클래스에이 클래스의 시각화 개체에 대한 참조를 전달해야 할 말 setter 메서드입니다. 그런 다음 ActionListener는이 클래스의 객체에 대한 메소드를 호출 할 수 있습니다.

1

1. 당신은 개인 예 변수는 액세스해야하는 클래스의 인스턴스를 만들 수 있습니다.

2 캡슐화의 많은 사용의 하나를 따라, 즉 해당 인스턴스 변수에 대한 개인 인스턴스 변수 공공 게터 세터를하는 것입니다.

3. 이제 클래스의 인스턴스의 공용 메소드를 호출하여, 전용 멤버에 액세스 할 수 있습니다.

4. 한 가지 더, 구글에서 창 빌더는 이제 프로 무료 2005 사용에 넷빈즈 팀에 의해 생성 된 그룹 레이아웃를 사용해보십시오.