簡體   English   中英

除非調整大小,否則JFrame窗口不會顯示任何內容

[英]JFrame Window not showing any content unless resized

我有一個擴展JFrame的Interface類。 我正在創建3個JPanels,並將它們添加到布局的不同區域中的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);

}

呼叫

setVisible(true);

在最后一行中,因為在setvisible行之后添加的組件直到調用repaint(),revalidate()才會顯示。當您調整大小時,調用repaint()方法,並且幀將正確可見。 setvisible在添加所有組件后調用setvisible

rootPane.setDefaultButton(loginButton); 呼叫設置setvisible

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

這是完整的工作代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM