Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI中的JTextField未按预期顺序打印列_Java_Database_User Interface_Jlabel_Jtextfield - Fatal编程技术网

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

Java GUI中的JTextField未按预期顺序打印列,java,database,user-interface,jlabel,jtextfield,Java,Database,User Interface,Jlabel,Jtextfield,在这段代码中,我有一个cols[]字符串数组,其中包含我希望在GUI上显示的字符串,每个字符串中都有一个TextField。例如: cols[0] [JTextField] cols[1] [JTextField] cols[2] [JTextField] .... cols[n] [JTextField] public class TWModify { static JFrame frame; static ImageIcon imageIcon;

在这段代码中,我有一个cols[]字符串数组,其中包含我希望在GUI上显示的字符串,每个字符串中都有一个TextField。例如:

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);
    }
However, after running my code, this is what I obtain:

cols[0] [JTextField] cols[1] 
      [JTextField]
cols[0][JTextField]
cols[1][JTextField]
cols[2][JTextField]
.... 
cols[n][JTextField]
公共类TWModify{
静态JFrame;
静态图像图标;
静态JPanel面板;
公共void execute(字符串tableName)引发SQLException{
tableName=tableName.toLowerCase();
框架=新JFrame(“机场”);
框架。设置尺寸(500500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
imageIcon=新的imageIcon(“C:\\Users\\User\\Videos\\Captures\\takoLogo.jpg”);
frame.setIconImage(imageIcon.getImage().getScaledInstance(10010023));
panel=新的JPanel();
框架。添加(面板);
Connection Connection=DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/airport“,”根“,”2605”);
语句Statement=connection.createStatement();
ResultSet ResultSet=statement.executeQuery(“显示来自机场的列。”+tableName+“;”);
列表列=新的ArrayList();
while(resultSet.next()){
字符串字段=resultSet.getString(“字段”);
列。添加(字段);
}
int-x1=10,y1=20,宽度1=80,高度1=25;//问题
INTX2=100,y2=20,宽度2=165,高度2=25;
字符串[]cols=listToArray(列);
JLabel[]标签=新的JLabel[cols.length];
JTextField[]JTextField=新的JTextField[cols.length];
for(int i=0;i

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

不要尝试使用setBounds()。这意味着您使用的是空布局。Swing设计用于布局管理器。如果您试图显示数据库中的数据,则更好的解决方案是使用
JTable
。有关入门的基本示例,请参阅。