如何将文本字段中的值添加到 Eclipse 中的 jcombobox
how to add values from a textfield to a jcombobox in eclipse
你好,我正在为我的学校项目使用 java 带 Swing UI 的 eclipse,我在尝试将输入的值从文本字段添加到组合框时遇到了问题,无论它是由用户按下回车键或按钮。
假设您有一个按钮:
JButton okButton = new JButton("OK");
要使按钮在您单击它时执行某些操作,您需要实现一个 ActionListener:
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// do stuff
}
});
现在您想从输入字段中读取文本:
JTextField userInput = new JTextField();
并将其添加到组合框:
JComboBox myComboBox = new JComboBox();
也许您的组合框中已经有一些这样的项目:
myComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "First Item" }));
无论如何 - 要读取输入并将其添加到组合框,您在 ActionListener 方法中所要做的就是:
String userInputText = userInput.getText(); // read the text from the JTextInput
myComboBox.addItem(userInputText); // add it as a new Item to the combobox
编辑
这里是 一种 的组合方式。它与 Eclipse 或您可以使用的任何其他 IDE 无关。它只是 Java - 你会发现 other/better 将它们组合在一起的方法,你对 Java 的了解越多。希望这可以帮助您入门:
public class MyProgram extends JFrame {
// here you declare your global variables
private JTextInput userInput;
private JButton okButton;
private JComboBox myComboBox;
public MyProgram () {
// here you create your objects
okButton = new JButton("OK");
myComboBox = new JComboBox<>();
userInput = new JTextField();
// then you initialize them
myComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "First Item" }));
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// and that is the code that gets executed once the user clicks the button
String userInputText = userInput.getText();
myComboBox.addItem(userInputText);
}
});
}
// this is what Eclipse will propably generate for you so you can launch the program and show your frame:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyProgram().setVisible(true);
}
});
}
}
你好,我正在为我的学校项目使用 java 带 Swing UI 的 eclipse,我在尝试将输入的值从文本字段添加到组合框时遇到了问题,无论它是由用户按下回车键或按钮。
假设您有一个按钮:
JButton okButton = new JButton("OK");
要使按钮在您单击它时执行某些操作,您需要实现一个 ActionListener:
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// do stuff
}
});
现在您想从输入字段中读取文本:
JTextField userInput = new JTextField();
并将其添加到组合框:
JComboBox myComboBox = new JComboBox();
也许您的组合框中已经有一些这样的项目:
myComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "First Item" }));
无论如何 - 要读取输入并将其添加到组合框,您在 ActionListener 方法中所要做的就是:
String userInputText = userInput.getText(); // read the text from the JTextInput
myComboBox.addItem(userInputText); // add it as a new Item to the combobox
编辑
这里是 一种 的组合方式。它与 Eclipse 或您可以使用的任何其他 IDE 无关。它只是 Java - 你会发现 other/better 将它们组合在一起的方法,你对 Java 的了解越多。希望这可以帮助您入门:
public class MyProgram extends JFrame {
// here you declare your global variables
private JTextInput userInput;
private JButton okButton;
private JComboBox myComboBox;
public MyProgram () {
// here you create your objects
okButton = new JButton("OK");
myComboBox = new JComboBox<>();
userInput = new JTextField();
// then you initialize them
myComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "First Item" }));
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// and that is the code that gets executed once the user clicks the button
String userInputText = userInput.getText();
myComboBox.addItem(userInputText);
}
});
}
// this is what Eclipse will propably generate for you so you can launch the program and show your frame:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyProgram().setVisible(true);
}
});
}
}