1

我对 Swing 应用程序感到震惊。这是一个简短的问题。我有一个框架,然后是一个面板。我将 JTabbedPane 添加到面板并在 JTabbedPane 上添加组件。我正在为 TabbedPane 设置网格布局,但它没有以正确的方式呈现组件。

它应该将组件呈现为 A 标签,然后在其旁边呈现文本字段。但它正在呈现一个标签,然后在该标签下方是一个文本字段。

下面是代码:

public static void main(String[] args) throws IOException {

     File file = new File("C:\\Documents and Settings\\rkumar\\Desktop\\IOSAutomationParameters.properties");

     if( file != null ){

         Properties property = new Properties();

         property.load(new FileInputStream(file));

          JFrame frame = new JFrame("IOSAutomationToolFourthPage");
          JPanel framePanel = new JPanel();
          framePanel.setSize(700,700);
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          String numberOfClients = property.getProperty("numberOfClients");
          System.out.println("number of clients: "+numberOfClients);
          if (!numberOfClients.isEmpty()) {

               int numberOfClientNumb = Integer.parseInt(numberOfClients);

                  System.out.println("numberOfClientNumb ::" + numberOfClientNumb);

                   JTabbedPane tab = new JTabbedPane();

                  // JTabbedPane tab2 = new JTabbedPane();
                for( int i=1; i<=numberOfClientNumb; i++){

                   JPanel panel = new JPanel();
                  panel.setSize(400, 400);
                  panel.setLayout(new GridLayout(10,2,1,1));

                  //Remote IP 
                  JLabel remoteIPLbl = new JLabel("Remote IP : ");
                  String remoteIPVal = property.getProperty("remoteIP_"+i);
                  JTextField remoteIPField = new JTextField(remoteIPVal);
                  remoteIPLbl.setBounds(50, 100, 2, 2);
                  remoteIPField.setBounds(200, 100, 2, 2);
                  remoteIPField.setColumns(30);


                  //Remote User Name
                  JLabel userNameLbl = new JLabel("User Name : ");
                  String userNameVal = property.getProperty("remoteUserName_"+i);
                  JTextField userNameField = new JTextField(userNameVal);
                  userNameLbl.setBounds(50, 200, 2, 2);
                  userNameField.setBounds(200, 200, 2, 2);
                  userNameField.setColumns(20);


                  //Remote Password
                  JLabel remotePasswordLbl = new JLabel("Remote Password : ");
                  String remotePasswordVal = property.getProperty("remotePassword_"+i);
                  JTextField remotePasswordField = new JTextField(remotePasswordVal);
                  remotePasswordLbl.setBounds(50, 300, 2, 2);
                  remotePasswordField.setBounds(200, 300, 2, 2);
                  remotePasswordField.setColumns(20);




                  //Remote Path
                  JLabel remotePathLbl = new JLabel("Remote Path : ");
                  String remotePathVal = property.getProperty("remotePath_"+i);
                  JTextField remotePathField = new JTextField(remotePathVal);
                  remotePathLbl.setBounds(50, 400, 2, 2);
                  remotePathField.setBounds(200, 400, 2, 2);
                  remotePathField.setColumns(100);




                  //deviceOrSimulator
                  JLabel deviceOrSimulatorLbl = new JLabel("Device or Simulator : ");
                  String deviceOrSimulatorVal = property.getProperty("deviceOrSimulator_"+i);
                  JTextField deviceOrSimulatorField = new JTextField(deviceOrSimulatorVal);
                  deviceOrSimulatorLbl.setBounds(50, 500, 2, 2);
                  deviceOrSimulatorField.setBounds(200, 500, 2, 2);
                  deviceOrSimulatorField.setColumns(1);


                  panel.add(remoteIPLbl);
                  panel.add(remoteIPField);
                  panel.add(userNameLbl);
                  panel.add(userNameField);
                  panel.add(remotePasswordLbl);
                  panel.add(remotePasswordField); 
                  panel.add(remotePathLbl);
                  panel.add(remotePathField); 
                  panel.add(deviceOrSimulatorLbl);
                  panel.add(deviceOrSimulatorField); 


                  tab.add("Client "+i,panel);
                  System.out.println(""+i);                     
               }

                framePanel.add(tab);
                frame.add(framePanel);
               // frame.setLayout(new Lay);
          }

          frame.setSize(800, 800);
          frame.setVisible(true);

     }
}
4

1 回答 1

3

将行数从 更改105(或0),例如:

panel.setLayout(new GridLayout(0,2,1,1));

你会没事的,但是,在这个问题中你会遇到一些麻烦,GridLayout因为两列的大小相同。因此,我建议您改用(和学习)GridBagLayout


GridLayout和之间的区别GridBagLayout

网格布局

grigbag布局


上面的示例使用GridBagLayout

public static void main(String[] args) throws IOException {

    JFrame frame = new JFrame("Test");
    frame.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(1, 1, 1, 1);

    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0;
    gbc.gridx = 0;

    frame.add(new JLabel("Remote IP : "), gbc);
    frame.add(new JLabel("User Name : "), gbc);
    frame.add(new JLabel("Remote Password : "), gbc);
    frame.add(new JLabel("Remote Path : "), gbc);
    frame.add(new JLabel("Device or Simulator : "), gbc);

    gbc.gridx = 1;
    gbc.weightx = 1;

    frame.add(new JTextField("Remote IP"), gbc);
    frame.add(new JTextField("User Name"), gbc);
    frame.add(new JTextField("Remote Password"), gbc);
    frame.add(new JTextField("Remote Path"), gbc);
    frame.add(new JTextField("Device or Simulator"), gbc);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
于 2012-05-28T11:42:12.030 回答