首页 > 解决方案 > Java swing,更改 java swing 窗口但出现空白屏幕。单独运行每个程序时,每个程序都可以正常工作

问题描述

我正在使用 java swing 在 Java 中设计登录屏幕和主菜单屏幕。我遇到的问题是登录正确时,当我已经设计了主菜单时,我得到了一个空白的 java swing 窗口。

我刚开始学习Java,所以我对它不是很了解。

我自己设计框架,这意味着我没有使用 GUI 界面,所有按钮和文本框都是手动完成的。

这是登录代码。

package loginapp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Login_App extends JFrame {

    public static void main(String[] args) {


JFrame frame = new JFrame("Mo Garage Login");
        //frame setup
        frame.setSize(500, 600);
        frame.setVisible(true);

        //Label and Text boxes

        JLabel usernamelbl = new JLabel("Username");
        usernamelbl.setBounds(100,100, 100,20);
        frame.add(usernamelbl);
        frame.setLayout(null);
        frame.setVisible(true);


        JLabel passwordlbl = new JLabel("Password");
        passwordlbl.setBounds(100,200,100,20);
        frame.add(passwordlbl);
        frame.setLayout(null);
        frame.setVisible(true);



        JTextField username = new JTextField();
        username.setBounds(200,100,100,20);
        frame.add(username);
        frame.setLayout(null);//using no layout managers 
        frame.setVisible(true);



        JPasswordField pass = new JPasswordField();
        pass.setBounds(200,200,100,20);
        frame.add(pass);
        frame.setLayout(null);
        frame.setVisible(true);



        //Buttons

        JButton log=new JButton("Login");
        log.setBounds(200,500,100,40);

        frame.add(log);
        frame.setLayout(null);//using no layout managers  
        frame.setVisible(true);
        //login button action
        log.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent arg0) {

            String uname=username.getText();
            String passwd=pass.getText();

            if (uname.equals("") && passwd.equals(""))
            {
                JOptionPane.showMessageDialog(frame, "Login Successfull");

                MainMenu mainmenu =new MainMenu();

                mainmenu.setVisible(true);

                frame.setVisible(false);

            }
            else {
                JOptionPane.showMessageDialog(frame, "Incorrect Credentials");
            }


        }
        }
    );

        JButton clear=new JButton("Clear");
        clear.setBounds(50,500,100,40);

        frame.add(clear);
        frame.setLayout(null);
        frame.setVisible(true);
        //clear button action

        clear.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                username.setText("");
                pass.setText("");

            }
        }
        );

    }


}

这是我的主菜单代码。

package loginapp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class MainMenu extends JFrame {

    public static void main(String[] args) {
    JFrame frame= new JFrame("Main Menu");
    frame.setSize(500,600);
    frame.setVisible(true);

    JButton client= new JButton("Add new Client");
    client.setBounds(200,100,120,30);
    frame.add(client);
    frame.setLayout(null);
    frame.setVisible(true);

    JButton Mod= new JButton("Update Database");
    Mod.setBounds(190,200,140,30);
    frame.add(Mod);
    frame.setLayout(null);
    frame.setVisible(true);

    JButton info= new JButton("Information");
    info.setBounds(200,300,120,30);
    frame.add(info);
    frame.setLayout(null);
    frame.setVisible(true);

    JButton exit= new JButton("Quit");
    exit.setBounds(200,400,120,30);
    frame.add(exit);
    frame.setLayout(null);
    frame.setVisible(true);
}
}

我尝试了不同的方法,但我找不到它。请帮忙。谢谢你。

标签: javaswing

解决方案


虽然每个评论都是绝对正确和有帮助的,但你的问题没有!您的问题是将Component's 添加到MainMenuobject 中。所以简而言之,复制 的孔代码并将mainMainMenu放入另一种方法中,让我们调用它public void go()并替换该行:-

JFrame frame= new JFrame("Main Menu");

this.setTitle("MainMenue");

frame并用关键字替换对象的任何引用this


推荐阅读