2013-01-08 2 views
1

의 폭이 내가이 곳에서 centerPanel의 폭을 얻기 위해 노력하고 다음 코드인 JPanel는 다음 JPanel을

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class TestForm extends JFrame 
{ 
    private JLabel firstLabel, secondLabel; 

    private JTextField firstTxt, secondTxt; 

    private GridBagLayout mainLayout = new GridBagLayout(); 
    private GridBagConstraints mainCons = new GridBagConstraints(); 
    private JPanel centerPanel; 

     public TestForm() 
     { 
      this.setLayout(mainLayout); 
     //Declaring instance variables 
     firstLabel = new JLabel("First: "); 
     secondLabel = new JLabel("Second: "); 

     firstTxt = new JTextField(7); 
     secondTxt = new JTextField(7); 


     mainCons.anchor = GridBagConstraints.WEST; 
     mainCons.weightx = 1; 
     mainCons.gridy = 2; 
     mainCons.gridx = 1; 
     mainCons.insets = new Insets(15, 0, 0, 0); 
     this.add(createCenterPanel(), mainCons); 

     System.out.println("Center Panel Width: "+centerPanel.getWidth());   
     this.setTitle("The Test Form"); 
     this.pack(); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    } 

    private JPanel createCenterPanel() 
    { 
     centerPanel = new JPanel(); 

     GridBagLayout gbl = new GridBagLayout(); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     centerPanel.setLayout(gbl); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.insets = new Insets(15,0,0,0); 
     centerPanel.add(firstLabel,gbc); 

     gbc.gridx = 2; 
     gbc.gridy = 1; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.insets = new Insets(15,0,0,0); 
     centerPanel.add(firstTxt,gbc); 

     gbc.gridx = 3; 
     gbc.gridy = 1; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.insets = new Insets(15,10,0,0); 
     centerPanel.add(secondLabel,gbc); 

     gbc.gridx = 4; 
     gbc.gridy = 1; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.insets = new Insets(15,-10,0,0); 
     centerPanel.add(secondTxt,gbc); 


     centerPanel.setBorder(BorderFactory.createTitledBorder("The Testing Form")); 
     centerPanel.setPreferredSize(centerPanel.getPreferredSize()); 
     centerPanel.validate(); 

     System.out.println("Width is: "+centerPanel.getWidth()); 
     System.out.println("Width is: "+centerPanel.getHeight()); 

     return centerPanel; 

    } 


    public static void main(String[]args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      new TestForm(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

에서 참조하시기 바랍니다 가져올 수 없습니다. 하지만 두 경우 모두 0으로 대답합니다! southPanel (여기에 포함되지 않음)이 일부 값을 높이면서 값을 사용하기 때문에 어떻게 든 너비와 높이를 가져와야합니다. 도와주세요!

+0

자신의 또 다른 레이아웃 부분의 높이에 대한 폭을 사용 LayoutManager (southPanel)를 정의 pack() 호출 후에 사용할 수 있습니다 다른 임의의 구성 요소에 상대적으로 크기를 설정할 수 있습니다 (높이가 작은 경우 제외). –

답변

4

pack() 또는 setSize()이 호출되기 전에 너비가 0입니다. 당신은 폭을 얻을

또는

가 만드는`southPanel`에 무엇

+0

+1이지만 2 위. 옵션은 - 또는 한 번 화면에 표시됩니다. – mKorbel

+0

도움을 많이 주셔서 감사합니다! 정말 감사 :) –

관련 문제