gpt4 book ai didi

java - itemStateChanged 和 actionPerformed 问题

转载 作者:行者123 更新时间:2023-12-01 19:19:31 24 4
gpt4 key购买 nike

我有一个实现 ActionListner、FocusListner 和 ItemListner 的处理程序类。我已经从 ConfrenceGUI 类实例化了一个 gui 对象:

      public ConferenceGUI()
{
//Create a new JPanel and set its latyout mgr
conference = new JPanel();
setLayout(new BorderLayout());
//create a RegPanel panel
regPanel = new RegPanel();
//create new WorkshopPanel workshopPanel
workshopPanel = new WorkshopPanel();
//build a buttonpanel
buildButtonPanel();
//Create a title panel
titlePanel = new JPanel(new FlowLayout());
//create and set a font object
titlePanel.setFont(new Font ("sansserif", Font.BOLD, 18));
//add a label
titlePanel.add(new Label("Select Registration Options"));
//Add the instantiated subpanels to the main conference gui panel
add(titlePanel, BorderLayout.NORTH);
add(regPanel, BorderLayout.WEST);
add(workshopPanel, BorderLayout.EAST);
add(buttonPanel, BorderLayout.SOUTH);
//add an item listener to the combo box
ConferenceHandler handler = new ConferenceHandler(this);
regPanel.regComboBox.addItemListener(handler);
//add a focus listener to the name field
ConferenceHandler fhandler = new ConferenceHandler(this);
regPanel.regTextField.addFocusListener(fhandler);
}

我正在尝试从监听器获取信息(包括来 self 的 ConferenceGUI 类中的单独方法的两个按钮监听器,但我没有包含该信息)。

这是我的处理程序的代码片段:

  public void itemStateChanged(ItemEvent e)
{
String name = gui.regPanel.regTextField.getText();
if (e.getSource() == gui.regPanel.regComboBox)
{
if (gui.regPanel.getRegType() == "Please select a type")
JOptionPane.showMessageDialog(null, "Please select a registraion type",
"Type Error", JOptionPane.ERROR_MESSAGE);
else gui.textArea.setText(name+" is a " +
gui.regPanel.getRegType()+ " registration");
}

按钮:

      public void actionPerformed (ActionEvent e)
{
String name = gui.regPanel.regTextField.getText();
DecimalFormat $ = new DecimalFormat("$#,##0.00");
if (e.getSource() == gui.calcButton)//if the calculate buttton is pressed
{
//dislplay error box if user selects index 0
if (gui.regPanel.getRegType() == "Please select a type")
JOptionPane.showMessageDialog(null, "Please select a registraion type",
"Type Error",JOptionPane.ERROR_MESSAGE);
//prints to textarea if registrant will be attending keynote or not
if (gui.regPanel.regCheckBox.isSelected())
gui.textArea.setText("Keynote address will be attended");
else
gui.textArea.setText("Keynote address will not be attended");
//prints to textarea which workshops registrant will be attending
gui.textArea.setText(name+" is registered in these workshops:" +
gui.workshopPanel.getWorkshopList());
//prints total registration fees to textarea
gui.textArea.setText("Total charges for" + name + " are " + $.format(calcTotalCharges()));
}
else if (e.getSource() == gui.clearButton)//if the clear button is pressed
{
//clear the textarea
gui.textArea.setText("");
//reset the list
gui.workshopPanel.workshopList.setSelectedIndex(0);
//reset the combobox to index 0
gui.regPanel.regComboBox.setSelectedIndex(0);
}
}

问题对你们所有人来说都是显而易见的,但由于我刚刚开始,我不明白为什么我不能在 GUI 的文本区域中写入任何文本。对于代码量,我深表歉意,但我想尽量做到彻底。

这是文本区域的来源(这是我的 ConferenceGUI 类中包含的一个单独的方法:

   private void buildButtonPanel()
{
//create the buttonpanel
buttonPanel = new JPanel(new FlowLayout());
//create the buttons
calcButton = new JButton("Calculate Charges");
clearButton = new JButton ("Clear");
//add listeners to the buttons
ConferenceHandler ahandler = new ConferenceHandler(this);
calcButton.addActionListener(ahandler);
clearButton.addActionListener(ahandler);
//create a text area
JTextArea textArea = new JTextArea(5,30);
textArea.setLineWrap(true); textArea.setWrapStyleWord(true);
//add everything to the buttonpanel
buttonPanel.add(calcButton); buttonPanel.add(clearButton); buttonPanel.add(new JScrollPane(textArea));
}

还有其他三个类 RegPanel 和 WorkshopPanel,它们都为 ConferenceGUI 创建几个面板,而 ConferenceGUI 又由小程序 (gui) 实例化。

最佳答案

我不确定我是否理解你的问题(什么不起作用?你期望什么以及会发生什么?),但可以肯定的是在 JTextArea 上多次调用 setText()不是一个好主意:setText 替换文本区域的全部内容。您应该使用 append() 将多行文本添加到文本区域。

已编辑:

既然您向我们展示了如何构建文本区域,问题就更清楚了:您在 buildButtonPanel 方法中实例化了一个局部变量 textArea,但 GUI 中的实例变量指向另一个文本区域(或空)。

再次编辑:

您的代码太复杂,不够完整,无法纠正,但情况如下所示:

public class Bug extends JPanel {
private JTextArea textArea = new JTextArea(); // first text area

private void build() {
JTextArea textArea = new JTextArea(); // second text area. Inaccessible outside of this method
this.add(new JScrollPane(textArea));
}

public void actionPerformed(ActionEvent e) {
this.textArea.setText("foo"); // here, we modify the first text area, but it hasn't been added to the GUI, so it's invisible
}
}

要修复它,您必须将其更改为:

public class NoBug extends JPanel {
private JTextArea textArea = new JTextArea(); // unique text area

private void build() {
this.add(new JScrollPane(this.textArea));
}

public void actionPerformed(ActionEvent e) {
this.textArea.setText("foo"); // here, we modify the unique text area, which has been added to the GUI in the build() method }
}

关于java - itemStateChanged 和 actionPerformed 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4985372/

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