微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GUI 中的 JTextField 未按预期顺序打印列

如何解决GUI 中的 JTextField 未按预期顺序打印列

在这代码中,我有一个 cols[] String 数组,其中包含我想在 GUI 上显示的字符串,其中每个字符串都有一个 JTextField

例如:

cols[0]    [JTextField]
cols[1]    [JTextField]
cols[2]    [JTextField]
.... 
cols[n]    [JTextField]
public class TWModify {
    static JFrame frame;
    static ImageIcon imageIcon;
    static JPanel panel;

    public void execute(String tableName) throws sqlException {
        tableName = tableName.toLowerCase();
        frame = new JFrame("Airport");
        frame.setSize(500,500);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        imageIcon = new ImageIcon("C:\\Users\\User\\Videos\\Captures\\takologo.jpg");
        frame.setIconImage(imageIcon.getimage().getScaledInstance(100,100,23));
        panel = new JPanel();
        frame.add(panel);
        Connection connection = DriverManager.getConnection("jdbc:MysqL://127.0.0.1:3306/airport","root","2605");
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("SHOW COLUMNS FROM airport." + tableName + ";");
        List<String> columns = new ArrayList<>();
        while (resultSet.next()) {
            String field = resultSet.getString("field");
            columns.add(field);
        }

        int x1 = 10,y1 = 20,width1 = 80,height1 = 25;              // the problem
        int x2 = 100,y2 = 20,width2 = 165,height2 = 25;
        String[] cols = listToArray(columns);
        JLabel[] labels = new JLabel[cols.length];
        JTextField[] jTextField = new JTextField[cols.length];
        for (int i = 0; i < cols.length; i++) {
            labels[i] = new JLabel(cols[i]);
            labels[i].setBounds(x1,y1,width1,height1);
            panel.add(labels[i]);
            jTextField[i] = new JTextField(20);
            jTextField[i].setBounds(x2,y2,width2,height2);
            panel.add(jTextField[i]);
            y1 += 30;
            y2 += 30;
        }                                                   // end of the problem
        frame.setVisible(true);
    }

然而,在运行我的代码后,这是我得到的:

cols[0] [JTextField] cols[1] 
      [JTextField]

它变得如此凌乱。我不知道 cols[n] 中的字符串数量,因为它取决于许多变量。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。