刚开始学eclipse的使用,下载了一个VE写了个简单的GUI。
但是触发 下拉菜单和列表 的事件的时候,不知道为什么执行两次。。按钮的事件正常,谁能给我解释下。
我把代码贴出来import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JButton;public class MainPanel extends JPanel { private JLabel TypeLabel = null;
private JComboBox TypeComboBox = null;
private JScrollPane ListScrollPane = null;
private JList jList = null;
private JLabel EditLabel = null;
private JTextArea EditTextArea = null;
private JButton SaveButton = null;
private JButton CancelButton = null; /**
* This is the default constructor
*/
public MainPanel() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
EditLabel = new JLabel();
EditLabel.setBounds(new java.awt.Rectangle(168,23,82,29));
EditLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
EditLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
EditLabel.setText("文件编辑区");
TypeLabel = new JLabel();
TypeLabel.setBounds(new java.awt.Rectangle(1,22,65,30));
TypeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
TypeLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
TypeLabel.setText("选择类型");
this.setLayout(null);
this.setSize(605, 345);
this.add(TypeLabel, null);
this.add(getTypeComboBox(), null);
this.add(getListScrollPane(), null);
this.add(EditLabel, null);
this.add(getEditTextArea(), null);
this.add(getSaveButton(), null);
this.add(getCancelButton(), null);
this.setVisible(true);
} /**
* This method initializes TypeComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getTypeComboBox() {
if (TypeComboBox == null) {
TypeComboBox = new JComboBox();
TypeComboBox.setBounds(new java.awt.Rectangle(67,22,100,30));
TypeComboBox.addItem("选项一");
TypeComboBox.addItem("选项二");
TypeComboBox.addItem("选项三");
TypeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
System.out.println("下拉列表事件!"); // TODO Auto-generated Event stub itemStateChanged()
}
});
}
return TypeComboBox;
} /**
* This method initializes ListScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getListScrollPane() {
if (ListScrollPane == null) {
ListScrollPane = new JScrollPane(jList);
ListScrollPane.setBounds(new java.awt.Rectangle(2,52,165,289));
ListScrollPane.setViewportView(getJList());
}
return ListScrollPane;
} /**
* This method initializes jList
*
* @return javax.swing.JList
*/
private JList getJList() {
if (jList == null) {
jList = new JList();
Object d[]=new String[30];
for(int i=0;i<30;i++){
d[i]="列项 "+i;
}
jList.setListData(d);
jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
System.out.println("列表事件!"); // TODO Auto-generated Event stub valueChanged()
}
});
}
return jList;
} /**
* This method initializes EditTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getEditTextArea() {
if (EditTextArea == null) {
EditTextArea = new JTextArea();
EditTextArea.setBounds(new java.awt.Rectangle(170,54,430,232));
}
return EditTextArea;
} /**
* This method initializes SaveButton
*
* @return javax.swing.JButton
*/
private JButton getSaveButton() {
if (SaveButton == null) {
SaveButton = new JButton();
SaveButton.setBounds(new java.awt.Rectangle(297,300,78,30));
SaveButton.setText("保存");
SaveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
EditTextArea.append("添加一条信息!\n");
System.out.println("按钮事件!"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return SaveButton;
} /**
* This method initializes CancelButton
*
* @return javax.swing.JButton
*/
private JButton getCancelButton() {
if (CancelButton == null) {
CancelButton = new JButton();
CancelButton.setBounds(new java.awt.Rectangle(397,300,76,30));
CancelButton.setText("取消");
}
return CancelButton;
}} // @jve:decl-index=0:visual-constraint="10,10"测试代码import javax.swing.JFrame;
import java.awt.FlowLayout;
public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainPanel panel =new MainPanel();
JFrame jFrame=null;
jFrame = new JFrame();
jFrame.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
jFrame.setSize(new java.awt.Dimension(615, 375));
jFrame.setTitle("XML文件维护器");
jFrame.setContentPane(panel);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
但是触发 下拉菜单和列表 的事件的时候,不知道为什么执行两次。。按钮的事件正常,谁能给我解释下。
我把代码贴出来import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JButton;public class MainPanel extends JPanel { private JLabel TypeLabel = null;
private JComboBox TypeComboBox = null;
private JScrollPane ListScrollPane = null;
private JList jList = null;
private JLabel EditLabel = null;
private JTextArea EditTextArea = null;
private JButton SaveButton = null;
private JButton CancelButton = null; /**
* This is the default constructor
*/
public MainPanel() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
EditLabel = new JLabel();
EditLabel.setBounds(new java.awt.Rectangle(168,23,82,29));
EditLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
EditLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
EditLabel.setText("文件编辑区");
TypeLabel = new JLabel();
TypeLabel.setBounds(new java.awt.Rectangle(1,22,65,30));
TypeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
TypeLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
TypeLabel.setText("选择类型");
this.setLayout(null);
this.setSize(605, 345);
this.add(TypeLabel, null);
this.add(getTypeComboBox(), null);
this.add(getListScrollPane(), null);
this.add(EditLabel, null);
this.add(getEditTextArea(), null);
this.add(getSaveButton(), null);
this.add(getCancelButton(), null);
this.setVisible(true);
} /**
* This method initializes TypeComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getTypeComboBox() {
if (TypeComboBox == null) {
TypeComboBox = new JComboBox();
TypeComboBox.setBounds(new java.awt.Rectangle(67,22,100,30));
TypeComboBox.addItem("选项一");
TypeComboBox.addItem("选项二");
TypeComboBox.addItem("选项三");
TypeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
System.out.println("下拉列表事件!"); // TODO Auto-generated Event stub itemStateChanged()
}
});
}
return TypeComboBox;
} /**
* This method initializes ListScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getListScrollPane() {
if (ListScrollPane == null) {
ListScrollPane = new JScrollPane(jList);
ListScrollPane.setBounds(new java.awt.Rectangle(2,52,165,289));
ListScrollPane.setViewportView(getJList());
}
return ListScrollPane;
} /**
* This method initializes jList
*
* @return javax.swing.JList
*/
private JList getJList() {
if (jList == null) {
jList = new JList();
Object d[]=new String[30];
for(int i=0;i<30;i++){
d[i]="列项 "+i;
}
jList.setListData(d);
jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
System.out.println("列表事件!"); // TODO Auto-generated Event stub valueChanged()
}
});
}
return jList;
} /**
* This method initializes EditTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getEditTextArea() {
if (EditTextArea == null) {
EditTextArea = new JTextArea();
EditTextArea.setBounds(new java.awt.Rectangle(170,54,430,232));
}
return EditTextArea;
} /**
* This method initializes SaveButton
*
* @return javax.swing.JButton
*/
private JButton getSaveButton() {
if (SaveButton == null) {
SaveButton = new JButton();
SaveButton.setBounds(new java.awt.Rectangle(297,300,78,30));
SaveButton.setText("保存");
SaveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
EditTextArea.append("添加一条信息!\n");
System.out.println("按钮事件!"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return SaveButton;
} /**
* This method initializes CancelButton
*
* @return javax.swing.JButton
*/
private JButton getCancelButton() {
if (CancelButton == null) {
CancelButton = new JButton();
CancelButton.setBounds(new java.awt.Rectangle(397,300,76,30));
CancelButton.setText("取消");
}
return CancelButton;
}} // @jve:decl-index=0:visual-constraint="10,10"测试代码import javax.swing.JFrame;
import java.awt.FlowLayout;
public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainPanel panel =new MainPanel();
JFrame jFrame=null;
jFrame = new JFrame();
jFrame.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
jFrame.setSize(new java.awt.Dimension(615, 375));
jFrame.setTitle("XML文件维护器");
jFrame.setContentPane(panel);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
解决方案 »
- 关于验证码,不知道怎么做
- 斗地主中的叫分算法
- JavaGroups—构建分布式通信的基础(上)
- 关于JPG图像的问题
- java 如何控制键盘,让其自动输入
- 100分送,请教高手帮帮忙!~!~为什么我这个FTP上传做完后会出现100个错误!~!~请解救!~!~
- 有谁知道哪里可以下载到java的函数库及这些函数的具体使用方法的???——谢谢!!!
- Sun-中国教育和科研计算机网SAI免费课程在线学习,详情在内。谁能给我可用的学校ID话,我再开帖给分。300分!!或更多!
- 偶然发现了一个以前没注意的问题,大家看看
- 一道while习题,求帮助
- 求TextMining的下载!!!!!!!!!顶者有分!!!!!!!!
- Struts可以利用外部资源文件来实现国际化,那么一个标准的带有main()方法的JAVA应用程序如何实现利用外部资源文件的国际化?
原因不详,以前没用过
1,DESELECTED 当前已经选中的ITEM
2,SELECTED 新的ITEM
1,DESELECTED 当前已经选中的ITEM
2,SELECTED 新的ITEM
///////////////////修改一下你的程序
TypeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
System.out.println("下拉列表事件!"); // TODO Auto-generated Event stub itemStateChanged()
System.out.println(e);
}
});
///////////////////修改一下你的程序
得到下面的结果
public void itemStateChanged(java.awt.event.ItemEvent e) {
System.out.println("下拉列表事件!"); // TODO Auto-generated Event stub itemStateChanged()
}
});下拉列表事件!
java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=选项二,stateChange=DESELECTED] on javax.swing.JComboBox[,67,22,100x30,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=null,alignmentY=null,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=选项二]
下拉列表事件!
java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=选项三,stateChange=SELECTED] on javax.swing.JComboBox[,67,22,100x30,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=null,alignmentY=null,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=选项三]