2012-05-28 86 views
1

我在Swing應用程序中遇到了問題。這是一個簡短的問題。我有一個框架,然後是一個面板。我將JTabbedPane添加到面板並在JTabbedPane上添加組件。我正在爲TabbedPane設置網格佈局,但它不是以正確的方式呈現組件。Swing組件未按照佈局對齊

它應該呈現組件作爲標籤,然後在它旁邊的文本字段。但它是一個標籤,然後在這個標籤下面標註。

下面是代碼:

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

更改行從105(或0)的數量,如:

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

而且你會好起來的,但是,你會在這個問題中使用GridLayout遇到索姆麻煩,因爲兩列的大小相同。我會爲此建議您使用(並學習)GridBagLayout。 使用GridBagLayout

gridlayout

grigbaglayout


以上示例::


差異與GridBagLayout之間GridLayout

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); 
} 
+0

*「..建議您使用(並學習)'GridBagLayout'代替。」*'GroupLayout'也適用於這種用例。 –

+0

@AndrewThompson好點! – dacwe

+0

噢,是的,沒錯。謝謝你提醒我。忘了投票! –