itemStateChanged和actionPerformed问题

时间:2011-02-13 16:34:40

标签: java swing jtextarea listeners

我有一个处理器类,它实现了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);
   }

我正在尝试从侦听器获取信息(包括我在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的textArea。对代码量表示歉意,但我想尽量彻底。

这是textarea的来源(这是我的ConferenceGUI类中包含的一个sperarate方法:

   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创建了几个面板,然后由applet(gui)实例化。

2 个答案:

答案 0 :(得分:2)

  

为代码量道歉,但我想尝试彻底

实际上,您发布的代码没有多大帮助,因为我们不知道代码的使用方式。

您的代码引用了“gui”对象,但我们不知道何时/如何创建此变量。您也引用“textArea”对象和相同的注释。所以您发布的代码不完整。

如果您无法在文本区域添加文本,则会发生以下两种情况之一:

a)代码永远不会被执行(您可以添加System.out.println(...)来验证这一点)。 b)您对添加到GUI的textArea组件有错误的引用。这可能有很多原因。也许你有一个同名的类和局部变量。

如果您需要更多帮助,请发布展示问题的SSCCE

答案 1 :(得分:2)

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

已编辑:

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

再次编辑:

您的代码太复杂且不够完整无法更正,但情况看起来像这样:

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        }
}