日期:2014-05-20  浏览次数:20704 次

一段Swing程序无法实现触发按钮事件
Java code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;


public class OOPPanel extends JPanel implements ActionListener
{
  private String[] OPPs={"中北大学","北京邮电大学","西安电子科技大学"};
  private String[] explains={"中北大学通信工程排名60","北京邮电大学通信工程排名第一","西安电子科技大学通信工程排名第二"};
  private LinkedList<String> explainList=new LinkedList<String>(Arrays.asList(explains));//增添到俩链接表
  private JPanel comboBoxPanel,buttonPanel,button2Panel,textPanel,textAreaPanel;
  private JComboBox comboBox;
  private JButton newItemButton ,exitButton,button2;
  private JTextArea explainTextArea,displayTextArea;
  private Border loweredBorder;
  private JTextField textField;

  public OOPPanel()//构造函数
  {
   Border loweredBorder=BorderFactory.createBevelBorder(BevelBorder.LOWERED);
   createGUIComponents();

  }

  public void createGUIComponents()
  {
   setLayout(new GridBagLayout());//创建网络包布局管理
   GridBagConstraints c=new GridBagConstraints();

   JPanel comboBoxPanel=new JPanel();//创建下拉列表控制板
   comboBoxPanel.setBorder(BorderFactory.createTitledBorder(loweredBorder,"选择一所大学准备考研"));
   comboBoxPanel.setLayout(new GridLayout(1,2));

   JComboBox comboBox=new JComboBox(OPPs);
   comboBoxPanel.add(comboBox);
   c=setupConstraints(0,0,2,1,GridBagConstraints.WEST);
   add(comboBoxPanel,c);
   comboBox.addActionListener(this);

   JPanel buttonPanel =new JPanel();
   buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));//嵌套一个流程布局并靠左对齐
   JButton newItemButton=new JButton("newItem");
   JButton exitButton=new JButton("exit");
   buttonPanel.add(newItemButton);
   buttonPanel.add(exitButton);
   c=setupConstraints(0,3,2,1,GridBagConstraints.WEST);
   add(buttonPanel,c);
   exitButton.addActionListener(this);
   newItemButton.addActionListener(this);

   JPanel textPanel =new JPanel();//创建文本字段控制面板
   textPanel.setLayout(new GridLayout(1,2));
   JLabel label=new JLabel("new term");
   JTextField textField=new JTextField(8);
   textPanel.add(label);
   textPanel.add(textField);
   c=setupConstraints(0,0,2,1,GridBagConstraints.WEST);
   add(textPanel,c);
   textPanel.setVisible(false);//默认为不显示

   JPanel textAreaPanel=new JPanel();
   textAreaPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
   explainTextArea=setupTextArea(explainTextArea,3,15,true);//调用自定义方法处理文本窗口
   textAreaPanel.add(explainTextArea);
   c=setupConstraints(0,1,2,3,GridBagConstraints.WEST);
   add(textAreaPanel,c);
   textAreaPanel.setVisible(false);

   JPanel button2Panel=new JPanel();//创建第二个按钮控制板
   JButton button2=new JButton("submit");
   button2Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
   button2Panel.add(button2);
   button2.addActionListener(this);
   c=setupConstraints(2,3,1,1,GridBagConstraints.WEST);
   add(button2Panel,c);
   button2Panel.setVisible(false);

   displayTextArea=setupTextArea(displayTextArea,5,20,true);

   comboBox.setSelectedIndex(0);
   updateTextArea(0);
   c=setupConstraints(3,0,3,5,GridBagConstraints.WEST);
   add(displayTextArea,c);

  }

  private JTextArea setupTextArea(JTextArea textArea, int rows, int cols,boolean editable)
  {
   textArea =new JTextArea(rows,cols);
   textArea.setLineWrap(true);//将超长的行折回到下一行
   textArea.setWrapStyleWord(true);//按字折会
   textArea.setEditable(editable);
   textArea.setBorder(BorderFactory.createTitledBorder(loweredBorder,"Explaination"));
   return textArea;
  }

  private void updateTextArea(int index)
  {
   displayTextArea.setText(explainList.get(index));
   displayTextArea.setVisible(true);
  }

  private GridBagConstraints setupConstraints(int gridx, int gridy,int gridwidth, int gridheight, int anchor)
  {
   GridBagConstraints c=new GridBagConstraints();
   c.gridx=gridx;
   c.gridy=gridy;
   c.gridwidth=gridwidth;
   c.gridheight=gridheight;
   c.insets=new Insets(5,5,5,5);//默认组件周围空隙5个像素
   c.ipadx=c.ipady=0;//设置组件内部周边空隙为5个像素
   c.anchor=anchor;//微调组件在网格空隙
   return c;
  }

  public void actionPerformed (ActionEvent e)
  {
   Object source=e.getSource();
   if(source==exitButton)
   {
    System.exit(0);
    System.out.println("exitbutton");
   }
   else if(source==comboBox)
   {
    int index=comboBox.getSelectedIndex();
    updateTextArea(index);
   }
   else if(source==newItemButton)
   {
    comboBoxPanel.setVisible(false);
    buttonPanel.setVisible(false);
    displayTextArea.setVisible(false);
    textPanel.setVisible(true);
    textAreaPanel.setVisible(true);
    button2Panel.setVisible(true);
   }
   else if(source==button2)
   {
    comboBox.addItem(textField.getText());
    explainList.addLast(explainTextArea.getText());
    comboBoxPanel.setVisible(false);
    buttonPanel.setVisible(false);
    displayTextArea.setVisible(false);
    textPanel.setVisible(true);
    textAreaPanel.setVisible(true);
    button2Panel.setVisible(true);

   }
  }
}