簡體   English   中英

JPanel類未添加到JFrame

[英]JPanel class is not adding to JFrame

我無法從主類獲取JFrame來顯示另一類的JPanel。 一切都能正確編譯這是我的擴展JFrame的主類代碼:

public OnlineCarSalesSystem(){
    setTitle("Online Car Sales System");
    setVisible(true);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setLayout(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(new Login());
}

public static void main(String[] args) {
    new OnlineCarSalesSystem();
}

在上面的代碼中,我添加了add(new Login()); 但它沒有在我的JFrame上顯示該面板。 在下面的代碼中,我使用JPanel擴展了我的課程。 這是JPanel類代碼:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JPanel{
    JLabel loginLabel = new JLabel("Login ID");
    JLabel passwordLabel = new JLabel("Password");
    JTextField loginTextField = new JTextField();
    JPasswordField passwordTextField = new JPasswordField();
    JButton submitButton = new JButton("Submit");
    JButton registration = new JButton("new Registration");
    JLabel noaccountLabel = new JLabel("No Account yet!!!");
    public void Login(){
        setBounds(0,0,500,500);
        setBackground(Color.red);
        setVisible(true);
        loginLabel.setBackground(Color.cyan);
        passwordLabel.setBackground(Color.cyan);
        loginTextField.setBounds(680, 103,90,20);
        add(loginTextField);
        loginLabel.setBounds(600, 100,90,30);
        add(loginLabel); 
        passwordTextField.setBounds(680, 153,90,20);
        passwordTextField.setEchoChar('*');
        add(passwordTextField);
        passwordLabel.setBounds(600, 150,90,30);
        add(passwordLabel); 
        add(submitButton);
        submitButton.setBounds(640,200,90,30);
        submitButton.addActionListener(new ActionListener() {   //////Submit Button
            public void actionPerformed(ActionEvent e) {

            }
        });
        add(registration);
        registration.setBounds(638,270,96,30);
        add(noaccountLabel);
        noaccountLabel.setBackground(Color.cyan);
        noaccountLabel.setBounds(640,250,90,30);
        registration.addActionListener(new ActionListener() {   //////registration Button
            public void actionPerformed(ActionEvent e) {

            }
        });
    }
}

問題在於代碼中的任何時候都不會執行Login()函數。 您可能要更改

public void Login() { ... }

public Login() { ... }

因此代碼在對象初始化時執行

暫無
暫無

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

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