我有一个实现 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 类中包含的一个单独的方法:
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) 实例化。