2014-12-17 2 views
1

JFrame을 확장하는 Interface 클래스가 있습니다. 저는 3 개의 JPanels를 만들고 레이아웃의 다른 영역에있는 Container (Border Layout)에 추가하고 있습니다.크기가 조정되지 않으면 JFrame 창에 내용이 표시되지 않습니다.

응용 프로그램을 실행할 때 아무 것도 표시되지 않지만 빈 창과 제목이 표시됩니다. 응용 프로그램의 크기를 조정하면 모든 내용이 표시되고 의도 한대로 작동합니다.

내가 이번에 다르게 한 것을 잘 모르겠다. 나는 이전에이 방법을 사용했고 이전 프로그램에서도 작동한다.

도움을 주시면 감사하겠습니다. http://pastebin.com/4UyEXsBr

코드 : 라인는, setVisible 이후에 추가 구성 요소 .because 마지막 줄에

public class Interface extends JFrame implements ActionListener { 

private Container contentPane; 

private JPanel buttonPanel, userPanel; 

private JButton loginButton, createUserButton, logoutButton, withdrawButton, depositButton, switchToRegisterButton, switchToLoginButton; 

private JLabel headerLabel, inputTopJTFLabel, inputPW1JPFLabel, toastLabel, inputPW2JPFLabel; 

public JTextField inputTopJTF; 
public JPasswordField inputPW1JPF, inputPW2JPF; 

JRootPane rootPane; 

public Interface(int width, int height, String title) { 

    //Setting up Interface 
    setTitle(title); 
    setSize(width, height); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLocation(100,100); 
    setVisible(true); 

    Font header = new Font("TimesRoman", Font.PLAIN, 50); 

    ///////////////////////BUTTON PANEL/////////////////////// 

    //Button Panel 
    buttonPanel = new JPanel(); 

    //Buttons 
    //loginButton 
    loginButton = new JButton("Login"); 
    loginButton.addActionListener(this); 
    loginButton.setAlignmentX(Component.CENTER_ALIGNMENT); 

    //switchToRegisterButton 
    switchToRegisterButton = new JButton("New User?"); 
    switchToRegisterButton.addActionListener(this); 
    switchToRegisterButton.setAlignmentX(Component.CENTER_ALIGNMENT); 

    //switchToLoginButton 
    switchToLoginButton = new JButton("Switch to Login"); 
    switchToLoginButton.addActionListener(this); 
    switchToLoginButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
    switchToLoginButton.setVisible(false); 

    //createUserButton 
    createUserButton = new JButton("Register"); 
    createUserButton.addActionListener(this); 
    createUserButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
    createUserButton.setVisible(false); 

    //logoutButton 
    logoutButton = new JButton("Logout"); 
    logoutButton.addActionListener(this); 
    logoutButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
    logoutButton.setVisible(false); 

    //withdrawButton 
    withdrawButton = new JButton("Withdraw"); 
    withdrawButton.addActionListener(this); 
    withdrawButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
    withdrawButton.setVisible(false); 

    //depositButton 
    depositButton = new JButton("Deposit"); 
    depositButton.addActionListener(this); 
    depositButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
    depositButton.setVisible(false); 

    //Adding items to buttonPanel 
    buttonPanel.add(loginButton); 
    buttonPanel.add(switchToRegisterButton); 
    buttonPanel.add(switchToLoginButton); 
    buttonPanel.add(createUserButton); 
    buttonPanel.add(logoutButton); 
    buttonPanel.add(withdrawButton); 
    buttonPanel.add(depositButton); 

    ///////////////BODY PANEL////////////////////// 

    //Body Panel 
    userPanel = new JPanel(); 
    userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.PAGE_AXIS)); 

    //JTextFields 
    //inputTopJTF 
    Dimension inputTopJTFDimension = new Dimension(100, 20); 
    inputTopJTF = new JTextField(); 
    inputTopJTF.setMaximumSize(inputTopJTFDimension); 

    //inputPW1JPF 
    Dimension inputPW1JPFDimension = new Dimension(100, 20); 
    inputPW1JPF = new JPasswordField(); 
    inputPW1JPF.setMaximumSize(inputPW1JPFDimension); 

    //inputPW2JPF 
    Dimension inputPW2JPFDimension = new Dimension(100, 20); 
    inputPW2JPF = new JPasswordField(); 
    inputPW2JPF.setMaximumSize(inputPW2JPFDimension); 
    inputPW2JPF.setVisible(false); 

    //JLabels 
    //toastLabel 
    toastLabel = new JLabel("Please sign in or create new account."); 
    toastLabel.setAlignmentX(Component.CENTER_ALIGNMENT); 

    //inputTopJTFLabel 
    inputTopJTFLabel = new JLabel("Username:"); 
    inputTopJTFLabel.setAlignmentX(Component.CENTER_ALIGNMENT); 

    //inputPW1JPFLabel 
    inputPW1JPFLabel = new JLabel("Password:"); 
    inputPW1JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT); 

    //inputPW2JPFLabel 
    inputPW2JPFLabel = new JLabel("Confirm Password:"); 
    inputPW2JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT); 
    inputPW2JPFLabel.setVisible(false); 

    //Adding items to userPanel 
    userPanel.add(toastLabel); 
    userPanel.add(inputTopJTFLabel); 
    userPanel.add(inputTopJTF); 
    userPanel.add(inputPW1JPFLabel); 
    userPanel.add(inputPW1JPF); 
    userPanel.add(inputPW2JPFLabel); 
    userPanel.add(inputPW2JPF); 

///////////////CONTENT PANE///////////////////// 

//Content Pane 
contentPane = getContentPane(); 

//JLabels 
//headerLabel 
headerLabel = new JLabel("Bank", SwingConstants.CENTER); 
headerLabel.setFont(header); 

//PAGE_START 
contentPane.add(headerLabel, BorderLayout.PAGE_START); 

//LINE_START 

//CENTER 
contentPane.add(userPanel, BorderLayout.CENTER); 

//LINE_END 

//PAGE_END 
contentPane.add(buttonPanel, BorderLayout.PAGE_END); 

userPanel.setFocusable(true); 
userPanel.requestFocus(); 

//Default Button 
rootPane = getRootPane(); 
rootPane.setDefaultButton(loginButton); 

} 

답변

2

전화

setVisible(true); 

는 당신이 때까지 표시되지 않습니다 여기

내 인터페이스 클래스 생성자 코드 repaint(),revalidate()을 호출하면 크기를 변경할 때 repaint() 메서드가 호출되고 프레임이 올바르게 표시됩니다. 따라서 setvisible은 DD 모든 구성 요소

라인 rootPane.setDefaultButton(loginButton); 호출 후 setvisible

rootPane.setDefaultButton(loginButton); 
setVisible(true);//after add all component to frame call setvisible method 

this is full working code

+0

고마워요! 그게 내 마지막 프로그램에서 작동 한 것이지이 프로그램이 아닙니다. 나는 setVisible (true); 그러나 생성자의 시작 부분이 아니라 끝 부분입니다. – MasonAlt

+0

@MasonAlt http://pastebin.com/q9d2fpi5 –

+0

* "repaint()"을 호출 할 때까지는 "표시되지 않습니다."* - 실제로는 표시되지 않을 가능성이 높으므로 revalidate를 호출하고 'repaint' - 또한 코드에 링크하지 말고 응답에 포함 시키면 시간이 지나면 링크가 끊어져 답변의 관련성이 낮아짐) – MadProgrammer

관련 문제