如何从jTextFIeld获取值并插入到jComboBox

时间:2017-03-15 10:27:36

标签: java netbeans jtextfield jcombobox

如何从jTextFIeld获取值并将其插入jComboBox?我有一个jTextField单独的jFrame和一个jComboBox ...任何帮助将非常感谢即时学习java谢谢你提前

1 个答案:

答案 0 :(得分:0)

检查以下示例代码..

import java.awt.FlowLayout;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Test 
{
    JFrame textFrame,comboFrame;
    JTextField text;
    JComboBox<String> cb;

    public Test()
    {
        textFrame = new JFrame("Text Frame");
        textFrame.setLayout(new FlowLayout());
        textFrame.setSize(300, 200);
        text = new JTextField("abc",10);
        textFrame.add(text);        
        textFrame.setVisible(true);


        comboFrame = new JFrame("ComboBox Frame");
        comboFrame.setLayout(new FlowLayout());
        comboFrame.setSize(300, 200);       
        cb = new JComboBox<String>();
        cb.addItem(text.getText());
        comboFrame.add(cb);
        comboFrame.setVisible(true);
    }
    public static void main(String args[])
    {
        new Test();
    }
}