javagui设计的问题
我很难以面向对象的方式设计GUI。以下代码将帮助我更清楚地表达我的问题:javagui设计的问题,java,swing,oop,events,jradiobutton,Java,Swing,Oop,Events,Jradiobutton,我很难以面向对象的方式设计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
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);
}
}
}
上面的代码只是创建了一个带有三个单选按钮的面板,每个按钮对应一个报价。它还创建一个显示报价的标签。无论何时选择按钮,标签中的文本都将设置为相应的引号。我完全理解这段代码。我试图修改它时遇到了麻烦。假设我想创建相同的程序,但是单选按钮垂直堆叠在另一个上面。我们还可以说,我决定将单选按钮添加到具有BoxLayout的面板中,这是我在其自己的BoxPanel类中定义的。(然后我会将BoxPanel添加到我的QuoteOptionsPanel中,其中仍然包含我的quote JLabel。)
因此,我的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中,因此我无权访问它。本质上,我是在寻求一种最佳方式来更改一个面板上的标签,其中事件侦听器属于另一个面板上的组件。如果您能提供任何帮助,我将不胜感激。如果我的问题表达得不够清楚,请告诉我。有几种方法可以解决这个问题,但对于大多数人来说,关键是获取和使用参考资料。假设将JLabel作为私有字段保存的类有一个公共方法
public void setQuoteLabelText(String text) {
quoteLabel.setText(text);
}
然后,您必须通过构造函数参数或
setXXX(…)
setter方法将该类的可视化对象的引用传递给您的BoxPanel类。然后您的ActionListener可以调用此类对象上的方法。有几种方法可以解决此问题,但对于大多数情况,关键是获取和使用引用。假设将JLabel作为私有字段保存的类有一个公共方法
public void setQuoteLabelText(String text) {
quoteLabel.setText(text);
}
然后,您必须通过构造函数参数或
setXXX(…)
setter方法将该类的可视化对象的引用传递给您的BoxPanel类。然后,ActionListener可以调用此类对象上的方法。1。您可以创建类的实例,该类的私有实例变量需要
进入
2.遵循封装的众多用途之一,即使用私有实例变量
和该实例变量的公共getter setter。
3。现在,您可以通过对的实例调用public方法来访问私有成员
这个班
4.还有一件事,请尝试使用NetBeans团队在2005年创建的组布局。使用Windows Builder Pro,现在谷歌免费提供 1。您可以创建类的实例,该类的私有实例变量需要 进入 2.遵循封装的众多用途之一,即使用私有实例变量 和该实例变量的公共getter setter。 3。现在,您可以通过对的实例调用public方法来访问私有成员 这个班 4.还有一件事,请尝试使用NetBeans团队在2005年创建的组布局。使用Windows Builder Pro,现在谷歌免费提供