【问题标题】:JCombobox is not displayedJCombobox 不显示
【发布时间】:2014-06-06 06:06:35
【问题描述】:

我正在使用以下代码显示特定用户的详细信息,方法是使用 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();
    }
}

【问题讨论】:

  • 删除数据库内容并对查询结果进行硬编码是否有效?你需要缩小你的问题(和你的代码):数据库问题或摇摆问题。
  • 而不是} catch (Exception ex) { System.out.println(ex);.. 我建议} catch (Exception ex) { e.printStackTrace()..。当坏事发生时,我们想要尽可能多的细节。你能用硬编码的数据做到这一点吗?为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。
  • 不要使用null 布局。像素完美布局是现代 UI 设计中的一种错觉,您无法控制字体、DPI、渲染管道或其他会改变组件在屏幕上渲染方式的因素。 Swing 旨在与布局管理器一起克服这些问题。如果您坚持忽略这些功能并违背 API 设计,请准备好面对很多令人头疼的问题和永无止境的辛勤工作......
  • 1JFrame f1 = new JFrame(); f1.setVisible(true);` 对setVisible(true) 的调用应该是last,紧跟在pack() 之后。这可能会导致 l5.setBounds(100, 50, 300, 30); .. 之类的一些(进一步)问题,但是,您应该使用布局来布局 GUI - use layout managerscombinations of them,以及 white space 的布局填充和边框。
  • 进一步提示:1) 对代码块使用一致且合乎逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。 2) 见The Use of Multiple JFrames, Good/Bad Practice?

标签: java database oracle swing jcombobox


【解决方案1】:

只需将setVisible(true) 放在构造函数的末尾即可。

完全同意MadProgrammer 更好地使用特定布局而不是为所有元素设置边界。

假设您正在从 Oracle 数据库中获取正确的数据。

【讨论】:

    【解决方案2】:

    null 布局的组合和setVisible 调用的时机对您不利。

    首先,不要使用null 布局,它们产生的问题多于其价值,其次,如果可以,请在构建窗口后最后一次调用setVisble

    我还会避免将数据检索的错误状态与 UI 元素的构造混为一谈,例如,在您的代码中,如果由于某种原因数据检索失败,组合框将永远不会显示,就个人而言,更好显示一个空的组合框和一个错误,例如...

    bx = new JComboBox();
    bx.setBounds(240, 20, 200, 20);
    add(bx);
    try {
        Vector v = new Vector();
        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();
        while (rs.next()) {
            String s = rs.getString(1);
    
            v.add(s);
        }
        bx.setModel(new DefaultComboBoxModel(v));
    
    } catch (Exception ex) {
        System.out.println(ex);
        JOptionPane.showMessageDialog(this, "I have failed");
    }
    

    查看Laying Out Components Within a Container了解更多详情

    【讨论】:

    • 是的,我同意。我将使用布局管理器而不是空布局。添加 catch 块将是一个额外的用途。谢谢。
    【解决方案3】:

    您应该在 try 块之外初始化并添加 JComboBox。因为,如果发生任何数据库异常,JComboBox 将不会被添加或可见。最好在启动时创建JComboBox 并在解决数据库查询时填充它。

     // Write these line, outside of try block.
     bx = new JComboBox(v);
     bx.setBounds(240, 20, 200, 20);
     add(bx);
    

    【讨论】:

    • 也许更好地创建组合。在应用程序。启动并在解决数据库查询时简单地填充它。
    • @AndrewThompson,是的,这样会更好。谢谢。
    • 谢谢大家,我将遵循所有建议的做法,错误是我的 Jdbc 驱动程序未包含在 eclipse IDE 中。我解决了这个错误。
    • 顺便说一句 - 请参阅 this answer 了解对齐 Displaying Data: 框架的标签和文本字段的好方法 (GroupLayout)(但它应该是一个对话框)。
    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 2012-06-05
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2017-01-02
    相关资源
    最近更新 更多