除非调整大小,否则JFrame窗口不显示任何内容

时间:2014-12-17 21:59:12

标签: java swing jframe

我有一个扩展JFrame的Interface类。我正在创建3个JPanel并将它们添加到布局的不同区域的Container(边框布局)中。

当我运行应用程序时,没有任何内容只显示空白窗口和标题。如果我调整应用程序的大小,它将显示所有内容并按预期工作。

我不确定这次我做了哪些不同的事情,我之前使用过这种方法,它在以前的程序中有效。

感谢任何帮助。

这是我的接口类构造函数代码:http://pastebin.com/4UyEXsBr

代码:

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);

}

1 个答案:

答案 0 :(得分:2)

致电

setVisible(true);

在最后一行。因为直到你调用repaint(),revalidate()才会显示在行setvisible之后添加的组件。当你调整大小时,调用repaint()方法并且框架将正确显示。所以在调用setvisible之后添加所有组件

rootPane.setDefaultButton(loginButton);之后

调用setvisible

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

this is full working code