我使用以下代码通过使用 JCombobox 从数据库中选择特定用户的用户名来显示特定用户的详细信息。组合框用于列出表中的用户名。 但当我运行代码时,组合框不可见。 (虽然没有错误)。 如果有人告诉代码有什么问题,那将会很有帮助。提前致谢。
public class EmpSearchApp extends JFrame implements ActionListener {
JLabel l, l1, l2, l3, l4, l5, l6, l7, l8;
JButton b;
JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7;
JComboBox bx;
String str;
EmpSearchApp() {
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("USER DATA");
l = new JLabel("Select Name:");
b = new JButton("Submit");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
l.setBounds(20, 20, 200, 20);
b.setBounds(50, 50, 150, 30);
add(l);
add(b);
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
tf4.setEditable(false);
tf5.setEditable(false);
tf6.setEditable(false);
tf7.setEditable(false);
b.addActionListener(this);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.45:1521:orcl", "gemsexam", "gems123");
PreparedStatement ps = con.prepareStatement("select uname from logf");
ResultSet rs = ps.executeQuery();
Vector v = new Vector();
while (rs.next()) {
String s = rs.getString(1);
v.add(s);
}
bx = new JComboBox(v);
bx.setBounds(240, 20, 200, 20);
add(bx);
} catch (Exception ex) {
System.out.println(ex);
}
}
public void actionPerformed(ActionEvent e) {
showData();
}
public void showData() {
JFrame f1 = new JFrame();
f1.setVisible(true);
f1.setSize(500, 500);
f1.setLayout(null);
f1.setTitle("USER DATA");
l5 = new JLabel("Displaying Data:");
l5.setForeground(Color.red);
l5.setFont(new Font("Serif", Font.BOLD, 20));
l1 = new JLabel("Name:");
l2 = new JLabel("Contact:");
l3 = new JLabel("email:");
l4 = new JLabel("qual:");
l6 = new JLabel("Tech:");
l7 = new JLabel("status");
l8 = new JLabel("address");
l5.setBounds(100, 50, 300, 30);
l1.setBounds(20, 110, 200, 20);
l2.setBounds(20, 140, 200, 20);
l3.setBounds(20, 170, 200, 20);
l4.setBounds(20, 200, 200, 20);
l6.setBounds(20, 230, 200, 20);
l7.setBounds(20, 260, 200, 20);
l8.setBounds(20, 290, 200, 20);
tf1.setBounds(240, 110, 200, 20);
tf2.setBounds(240, 140, 200, 20);
tf3.setBounds(240, 170, 200, 20);
tf4.setBounds(240, 200, 200, 20);
tf5.setBounds(240, 230, 200, 20);
tf6.setBounds(240, 260, 200, 20);
tf7.setBounds(240, 290, 200, 20);
f1.add(l5);
f1.add(l1);
f1.add(tf1);
f1.add(l2);
f1.add(tf2);
f1.add(l3);
f1.add(tf3);
f1.add(l4);
f1.add(tf4);
f1.add(l6);
f1.add(tf5);
f1.add(l7);
f1.add(tf6);
f1.add(l8);
f1.add(tf7);
str = (String) bx.getSelectedItem();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.45:1521:orcl", "gemsexam", "gems123");
PreparedStatement ps = con.prepareStatement("select * from logf where uname=?");
ps.setString(1, str);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
tf1.setText(rs.getString(1));
tf2.setText(rs.getString(2));
tf3.setText(rs.getString(3));
tf4.setText(rs.getString(4));
tf5.setText(rs.getString(4));
tf6.setText(rs.getString(4));
tf7.setText(rs.getString(4));
}
} catch (Exception ex) {
System.out.println(ex);
}
}
public static void main(String arr[]) {
new EmpSearchApp();
}
}
最佳答案
只需将 setVisible(true)
放在构造函数的末尾即可。
完全同意 MadProgrammer
的观点,最好使用特定布局,而不是为所有元素设置边界。
假设您从 Oracle 数据库获取正确的数据。
关于java - JCombobox不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075267/