在我的程序中,我有一个ComboBox,用户可以在其中选择他想要编辑的用户,目前我用来填充ComboBox的类没有这样做。我在网站上看到了一些类似的问题,但没看到我做错了什么。如果有人可以解决问题,我们将不胜感激。我对大部分内容还很陌生。
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class AdminPanelDelUser {
private String Host = "Hidden";
private String Name = "Hidden";
private String Pass = "Hidden";
private JComboBox<String> userPicker;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AdminPanelDelUser window = new AdminPanelDelUser();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AdminPanelDelUser() {
initialize();
getUserPicker();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 352);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JComboBox<String> userPicker = new JComboBox<String>();
userPicker.setBounds(59, 6, 235, 27);
frame.getContentPane().add(userPicker);
}
public JComboBox<String> getUserPicker() {
try {
Connection conn = DriverManager.getConnection( Host, Name, Pass );
PreparedStatement pst = conn.prepareStatement("SELECT * From `table_1`");
ResultSet rs = pst.executeQuery();
while(rs.next()) {
String name =rs.getString("user_name");
userPicker.addItem(name);
}
}
catch (Exception e) {
//Place Pop Warning Later
}
return userPicker;
}
}
关于如何进一步改进代码的任何建议都会很愉快。感谢帮助我解决这个问题的任何人。