Topic: 求助:事件处理。

  Print this page

1.求助:事件处理。 Copy to clipboard
Posted by: kirk
Posted on: 2003-06-02 07:44

在JDialog上有一JComboBox,现在要监听其itemStateChanged事件,怎么办?

JDialog是这样构造的:new JDialog(this," ",true);//this是frame的子类
JComboBox是这样改造的:new JComboBox(oneVector);//oneVector是一Vector对象,是this的成员。

2.Re:求助:事件处理。 [Re: kirk] Copy to clipboard
Posted by: vjava
Posted on: 2003-06-02 08:16

你到http://javaalmanac.com/egs/index.html 看看 一定能找到

3.Re:求助:事件处理。 [Re: vjava] Copy to clipboard
Posted by: nothing
Posted on: 2003-06-02 08:31

哦,这个网站啊,我好久没去了,知道,知道的...

4.Re:求助:事件处理。 [Re: kirk] Copy to clipboard
Posted by: kirk
Posted on: 2003-06-02 08:55

哦,没时间了...........

5.参考java年鉴我做了一个测试例程,我的JComboBox放在JFrame中不过容器不会影响事件的。 [Re: kirk] Copy to clipboard
Posted by: pilgrimhuyu
Posted on: 2003-06-02 09:46

import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;

public class TestCombo
extends JFrame {
JComboBox jComboBox1 = new JComboBox();
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();

public TestCombo() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
TestCombo testCombo = new TestCombo();
//testCombo.setSize(600,600);
testCombo.show();
testCombo.pack();
}

private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
jTextField1.setText("jTextField1");
jTextField2.setText("jTextField2");
this.getContentPane().add(jComboBox1, new XYConstraints(21, 19, 192, 24));
this.getContentPane().add(jTextField1, new XYConstraints(16, 137, 188, 39));
this.getContentPane().add(jTextField2, new XYConstraints(13, 201, 191, 43));

jComboBox1.addItem("111");
jComboBox1.addItem("222");
jComboBox1.addItem("333");
jComboBox1.addItem("444");
MyItemListener actionListener = new MyItemListener();
jComboBox1.addItemListener(actionListener);
}

class MyItemListener
implements ItemListener {

public void itemStateChanged(ItemEvent evt) {
JComboBox cb = (JComboBox) evt.getSource();

Object item = evt.getItem();

if (evt.getStateChange() == ItemEvent.SELECTED) {//选定事件
jTextField1.setText((String)jComboBox1.getSelectedItem());

}
else if (evt.getStateChange() == ItemEvent.DESELECTED) {//取消选定事件
jTextField2.setText((String)jComboBox1.getSelectedItem());
}
}
}

}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923