Java Jcombobox未选择项
我试图将Jcombobox值插入到我的sqlite数据库中,但每当我运行该程序时,它只会将'9'插入到我的数据库中。请告知。我正在尝试将用户选择的整数导入我的sqlite数据库。出于某种原因,即使使用ActionListener,默认值9仍然被输入到表中,我不知道为什么。这是完整的代码,不包括连接和JtableJava Jcombobox未选择项,java,database,sqlite,user-interface,Java,Database,Sqlite,User Interface,我试图将Jcombobox值插入到我的sqlite数据库中,但每当我运行该程序时,它只会将'9'插入到我的数据库中。请告知。我正在尝试将用户选择的整数导入我的sqlite数据库。出于某种原因,即使使用ActionListener,默认值9仍然被输入到表中,我不知道为什么。这是完整的代码,不包括连接和Jtable public class create extends JFrame { private JPanel contentPane; private JTextField textFiel
public class create extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
create frame = new create();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection connect = null;
private JTextField textField_2;
String uniqueString = UUID.randomUUID().toString();
private JTextField textField_3;
/**
* Create the frame.
*/
public create() {
connect = connection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(new Color(204, 204, 204));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblStudentName = new JLabel("Student ID:");
lblStudentName.setBounds(96, 69, 72, 16);
contentPane.add(lblStudentName);
textField = new JTextField();
textField.setBounds(173, 64, 216, 26);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblGrade = new JLabel("Grade:");
lblGrade.setBounds(125, 107, 47, 16);
contentPane.add(lblGrade);
JComboBox comboBox = new JComboBox();
comboBox.addItem(9);
comboBox.addItem(10);
comboBox.addItem(11);
comboBox.addItem(12);
comboBox.setBounds(173, 102, 72, 29);
contentPane.add(comboBox);
comboBox.setSelectedItem(9);
comboBox.addActionListener(comboBox);
int selectedNumber = (int)comboBox.getSelectedItem();
JLabel lblInputBookOver = new JLabel("Teacher:");
lblInputBookOver.setBounds(111, 146, 61, 21);
contentPane.add(lblInputBookOver);
textField_1 = new JTextField();
textField_1.setBounds(173, 143, 216, 26);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton button = new JButton("<");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int count = 0; count <= 0; count++) {
//dispose();
options sc = new options();
sc.setVisible(true);
}
}
});
button.setBounds(6, 6, 30, 29);
contentPane.add(button);
JLabel lblStudentname = new JLabel("Student Name:");
lblStudentname.setBounds(74, 31, 99, 16);
contentPane.add(lblStudentname);
textField_2 = new JTextField();
textField_2.setBounds(173, 26, 216, 26);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(125, 180, 42, 26);
contentPane.add(lblEmail);
textField_3 = new JTextField();
textField_3.setBounds(173, 180, 216, 26);
contentPane.add(textField_3);
textField_3.setColumns(10);
JButton btnCheckout = new JButton("Checkout");
btnCheckout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String uniqueString = UUID.randomUUID().toString().replace("-", "");
try {
String query = "insert into data ('Name', 'Student ID', 'Teacher', 'Grade', 'Email', 'Ebook') values (?,?,?,?,?,?)";
PreparedStatement pst = connect.prepareStatement(query);
pst.setString(1,textField_2.getText() );
pst.setString(2,textField.getText() );
pst.setString(3,textField_1.getText() );
pst.setInt(4, selectedNumber);
pst.setString(5,textField_3.getText() );
pst.setString(6, uniqueString);
for (int count = 0; count <= 0; count++) {
//dispose();
confirm sc = new confirm();
sc.setVisible(true);
count = 0;
}
pst.execute();
pst.close();
}
catch (Exception w){
w.printStackTrace();
}
}
});
btnCheckout.setBounds(173, 218, 117, 29);
contentPane.add(btnCheckout);
}
}
public类创建扩展JFrame{
私有JPanel内容窗格;
私有JTextField textField;
私有JTextField textField_1;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
创建帧=新建();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
连接=空;
私有JTextField textField_2;
字符串uniqueString=UUID.randomUUID().toString();
私有JTextField textField_3;
/**
*创建框架。
*/
公共创建(){
connect=connection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100450300);
contentPane=newjpanel();
contentPane.setBackground(新颜色(204204204));
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblStudentName=新JLabel(“学生ID:”);
lblStudentName.setBounds(96,69,72,16);
contentPane.add(lblStudentName);
textField=新的JTextField();
textField.setBounds(173,64,216,26);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblGrade=新JLabel(“等级:”);
lblGrade.挫折(125、107、47、16);
contentPane.add(lblGrade);
JComboBox comboBox=新的JComboBox();
组合框。附加项(9);
组合框。附加项(10);
组合框。附加项(11);
组合框。附加项(12);
组合框.立根(173,102,72,29);
contentPane.add(组合框);
comboBox.setSelectedItem(9);
comboBox.addActionListener(comboBox);
int selectedNumber=(int)组合框;
JLabel lblInputBookOver=新JLabel(“教师:”);
LblinPutboover.立根(111、146、61、21);
contentPane.add(lblInputBookOver);
textField_1=新的JTextField();
textField_1.立根(173143216,26);
contentPane.add(textField_1);
textField_1.设置列(10);
JButton button=new JButton(“它将9插入到数据库中,因为所选项目始终为9,这是因为您首先将其添加到组合框中。要插入所选值,您必须使用,然后您可以接受用户的选择并做任何您想做的事情
其用法示例如下:
import java.awt.FlowLayout;
import java.io.FileNotFoundException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class ComboBox extends JFrame {
public ComboBox() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
getContentPane().setLayout(new FlowLayout());
Integer[] numbers = { 4, 5, 8, 123, 42, 634 };
JComboBox<Integer> comboBox = new JComboBox<>(numbers);
comboBox.setSelectedItem(42); // The initial selection is 42.
comboBox.addActionListener(e -> {
int selectedNumber = (int) comboBox.getSelectedItem();
System.out.println("Selected number: " + selectedNumber);
// Do whatever with selected number
});
add(comboBox);
}
public static void main(String[] args) throws FileNotFoundException {
SwingUtilities.invokeLater(() -> new ComboBox().setVisible(true));
}
}
导入java.awt.FlowLayout;
导入java.io.FileNotFoundException;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
公共类组合框扩展了JFrame{
公共组合框(){
setDefaultCloseOperation(关闭时退出);
设置大小(300300);
setLocationRelativeTo(空);
getContentPane().setLayout(新的FlowLayout());
整数[]数={4,5,8,123,42,634};
JComboBox组合框=新的JComboBox(数字);
comboBox.setSelectedItem(42);//初始选择是42。
comboBox.addActionListener(e->{
int selectedNumber=(int)组合框;
System.out.println(“所选编号:“+selectedNumber”);
//用所选号码做任何事
});
添加(组合框);
}
公共静态void main(字符串[]args)引发FileNotFoundException{
SwingUtilities.invokeLater(()->new ComboBox().setVisible(true));
}
}
它将9插入到数据库中,因为所选项目始终为9,这是因为您首先将其添加到组合框中。要插入所选值,您必须使用,然后您可以接受用户的选择并执行任何您想要的操作
其用法示例如下:
import java.awt.FlowLayout;
import java.io.FileNotFoundException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class ComboBox extends JFrame {
public ComboBox() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
getContentPane().setLayout(new FlowLayout());
Integer[] numbers = { 4, 5, 8, 123, 42, 634 };
JComboBox<Integer> comboBox = new JComboBox<>(numbers);
comboBox.setSelectedItem(42); // The initial selection is 42.
comboBox.addActionListener(e -> {
int selectedNumber = (int) comboBox.getSelectedItem();
System.out.println("Selected number: " + selectedNumber);
// Do whatever with selected number
});
add(comboBox);
}
public static void main(String[] args) throws FileNotFoundException {
SwingUtilities.invokeLater(() -> new ComboBox().setVisible(true));
}
}
导入java.awt.FlowLayout;
导入java.io.FileNotFoundException;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
公共类组合框扩展了JFrame{
公共组合框(){
setDefaultCloseOperation(关闭时退出);
设置大小(300300);
setLocationRelativeTo(空);
getContentPane().setLayout(新的FlowLayout());
整数[]数={4,5,8,123,42,634};
JComboBox组合框=新的JComboBox(数字);
comboBox.setSelectedItem(42);//初始选择是42。
comboBox.addActionListener(e->{
int selectedNumber=(int)组合框;
System.out.println(“所选编号:“+selectedNumber”);
//用所选号码做任何事
});
添加(组合框);
}
公共静态void main(字符串[]args)引发FileNotFoundException{
SwingUtilities.invokeLater(()->new ComboBox().setVisible(true));
}
}
DefaultComboBoxModel的默认“selected item”在未选择任何内容时是第一个(selectedObject=getElementAt(0)
)。在您的代码中,没有选择任何项目。我明白了,所以9只是默认值。我应该将“selectedObject=getElementAt(0)”放在哪里?感谢您的输入默认值当未选择任何内容时,DefaultComboBoxModel的“selected item”是第一个(selectedObject=getElementAt(0)
)。在您的代码中,没有选择任何项目。我明白了,所以9只是默认值。我应该将“selectedObject=getElementAt(0)”放在“哪里?谢谢你的输入我做了编辑但似乎仍然不起作用。谢谢你的输入到目前为止我刚刚更新了所有内容。谢谢你的帮助我仍然是所有这些的初学者我做了编辑但仍然不起作用。”