객체 지향 방식으로 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에 있으므로 액세스 할 수 없습니다. 본질적으로 나는 다른 패널의 구성 요소에 속하는 이벤트 리스너를 사용하여 한 패널에서 레이블을 변경하는 최적의 방법을 묻습니다. 나는 당신이 제공 할 수있는 도움에 대해 대단히 감사 할 것입니다. 제 질문을 명확하게 표명하지 않았다면 알려주십시오.