Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java swing,更改Java swing窗口,但出现空白屏幕。单独运行每个程序时,每个程序都可以正常工作_Java_Swing - Fatal编程技术网

Java swing,更改Java swing窗口,但出现空白屏幕。单独运行每个程序时,每个程序都可以正常工作

Java swing,更改Java swing窗口,但出现空白屏幕。单独运行每个程序时,每个程序都可以正常工作,java,swing,Java,Swing,我正在使用JavaSwing用Java设计一个登录屏幕和一个主菜单屏幕。我遇到的问题是,当登录正确时,当我已经设计主菜单时,我会得到一个空白的JavaSwing窗口 我刚开始学习Java,所以我对它不是很了解 我自己设计框架,这意味着我没有使用GUI界面,所有按钮和文本框都是手动完成的 这是登录代码 package loginapp; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import

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

我刚开始学习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);
}
}
我尝试了不同的方法,但找不到。请帮忙。
谢谢。

尝试在登录操作开始时调用方法revalidate()。它将重新加载帧

在你的
Login\u应用程序
类中,在
actionPerformed()
方法中,你调用类
主菜单的构造函数,就像这样

MainMenu mainmenu =new MainMenu();
但是,class
main菜单
没有构造函数-至少在您发布的代码中没有。在类
main菜单的方法
main()
中构建GUI。因此,根据您发布的代码,我建议最简单的解决方案是更改类
Login\u App
的方法
actionPerformed()
中的代码。将上面显示的行替换为

MainMenu.main(new String[]{});

虽然每个人的评论都是绝对正确和有益的,但你的问题并没有出现在任何地方您的问题是将
组件
添加到
主菜单
对象
。 简而言之,复制
main菜单
main
main
的孔代码,并将其放入另一种方法中,我们将其称为go
public void go()
并替换该行:-

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


并用关键字
this

替换对象
框架的任何引用,它们可能不会显示,因为您没有将组件添加到contentPane()1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 看。。。。3) 请学习常见的Java命名法(命名约定-例如
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是
大写常量
),并一致使用它。4)
frame.setVisible(true)被多次调用。它应该在添加所有组件后以及调用
pack()
后执行一次。“它重新加载帧。”不,它会重新验证它们(就像方法名称所建议的那样)。
this.setTitle("MainMenue");