【问题标题】:JPanel class is not adding to JFrameJPanel 类未添加到 JFrame
【发布时间】:2019-09-17 12:51:37
【问题描述】:

我无法从主类获取我的 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) {

            }
        });
    }
}

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 “但它没有显示该面板” null 布局可能是原因。
  • 3) public void Login(){ 如果您打算作为 构造函数, 它应该是 public Login(){..
  • 4) 这里没有充分的理由扩展JFrame JPanel。此外,鉴于它是一个登录,框架应该改为 JDialogJOptionPane

标签: java swing


【解决方案1】:

问题是Login()-函数在代码中的任何时候都没有执行。你可能想改变

public void Login() { ... }

public Login() { ... }

所以代码在对象初始化时执行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-11
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    相关资源
    最近更新 更多