简体   繁体   English

netbeans中的组合框

[英]Combo Boxes in netbeans

i have a gui in netbeans. 我在netbeans中有一个GUI。 3 textfields animal id(ie MAM001) , type of animal(LION) and nickname(SIMBA). 3个文本字段,其中包括动物ID(即MAM001),动物类型(LION)和昵称(SIMBA)。 when the three textfields are entered the user can click on the submit button which will store the values from the three textfields in a database. 输入三个文本字段时,用户可以单击“提交”按钮,该按钮会将来自三个文本字段的值存储在数据库中。

what i want to do but don't know how to do it, is to use a combo box with three different fields in it (MAMMAL,BIRD REPTILE). 我想做的但不知道如何做的是使用一个组合框,其中包含三个不同的字段(MAMMAL,BIRD REPTILE)。 when i select mammal from the combo box it will the change mysql statements in the background so that the data entered into the textfields will go into the mammal table in the database. 当我从组合框中选择哺乳动物时,它将在后台更改mysql语句,以便输入到文本字段中的数据将进入数据库中的哺乳动物表。 if i click on the "bird" field in the combo box the same thing will happen except the data will go into the bird table in the database. 如果我单击组合框中的“鸟”字段,除了数据将进入数据库的鸟表之外,将会发生同样的事情。

Any help would be greatly appreciated 任何帮助将不胜感激
Thank you. 谢谢。

I assume, that you want to react on an action event on your combo box. 我假设您想对组合框上的某个动作事件做出反应。 You can do this with the help of an ItemListener. 您可以在ItemListener的帮助下完成此操作。 For more details see the java api . 有关更多详细信息,请参见java api

One approach could be the following: 一种方法可能如下:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class DemoFrame {

private JFrame frame;
private JComboBox comboBox;
private String[] items = {"IT1","IT2","IT3"};

public DemoFrame() {
    frame = new JFrame("Demo Frame");
    frame.setSize(300, 300);
    comboBox = new JComboBox(items);
    comboBox.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            //perform here your database querys for specific items.

            if(e.getItem().equals(items[0]) && e.getStateChange() == ItemEvent.SELECTED) {
                //db query for it "IT1"
            }
        }

    });

    frame.add(comboBox);
    frame.setVisible(true);

}

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new DemoFrame();
        }
    });
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM