gpt4 book ai didi

java - Java GUI 设计的问题

转载 作者:行者123 更新时间:2023-11-29 08:05:36 25 4
gpt4 key购买 nike

我在以面向对象的方式设计 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);
}
}
}

上面的代码简单地创建了一个包含三个单选按钮的面板,每个单选按钮对应一个引号。它还会创建一个显示引号的标签。 Whenever a button is selected, the text in the label is set to the corresponding quote.我理解这段代码就好了。我在尝试修改它时遇到了麻烦。假设我想创建相同的程序,但单选按钮一个一个垂直堆叠。假设我决定通过将单选按钮添加到带有 BoxLayout 的面板来解决这个问题,我在自己的 BoxPanel 类中定义了它。 (然后我会将 BoxPanel 添加到我的 QuoteOptionsPanel,它仍然包含我的报价 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 作为私有(private)字段保存的类有一个公共(public)方法,

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

然后您必须通过构造函数参数或 setXXX(...) setter 方法将对此类可视化对象的引用传递给您的 BoxPanel 类。然后您的 ActionListener 可以调用此类对象的方法。

关于java - Java GUI 设计的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11515061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com