【发布时间】:2016-04-27 12:01:20
【问题描述】:
我想创建一个combobox 和一个textbox。用户将在文本框中输入文本,该文本将添加为combobox 的项目。
我该怎么做?我写了一个代码,但是我找不到我要在actionlistener写什么。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Q2 extends JFrame {
JTextField t;
JComboBox combobox = new JComboBox();
public Q2() {
t = new JTextField("Enter text here", 20);
t.setEditable(true);
t.addActionListener(new act());
add(t);
add(combobox);
combobox.addItem(t.getText().toString());
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public class act implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
Q2 test = new Q2();
}
}
【问题讨论】:
-
为什么您需要一个文本字段来在 jcombobox 上写入数据,您只需使组合可编辑。我只是说。
-
我是 java 新手,我想学习所有代码 :) 而且我不知道可编辑的组合框..
-
杀掉它作为答案然后等待。
标签: java combobox jtextfield textfield