java - 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);
}
}
我尝试了不同的方法,但我找不到它。请帮忙。谢谢你。
解决方案
虽然每个评论都是绝对正确和有帮助的,但你的问题没有!您的问题是将Component
's 添加到MainMenu
object 中。所以简而言之,复制 的孔代码并将main
其MainMenu
放入另一种方法中,让我们调用它public void go()
并替换该行:-
JFrame frame= new JFrame("Main Menu");
和
this.setTitle("MainMenue");
frame
并用关键字替换对象的任何引用this
。
推荐阅读
- html - Vue.js 中未打印索引数量
- polly - 将 Polly 重试策略添加到模拟的 HttpClient?
- algolia - 如何清除 algolia 的搜索查询缓存?
- java - 使用 Firebase 刷新列表的最佳方法是什么
- flutter - 如何在 Flutter 中获取 Asset 对象的路径?
- bash - 无法在文本文件上使用 SED 命令删除撇号
- git - 仅当前分支的 git 日志 - 不指定它来自哪里(master..)
- intellij-idea - 无论我在 Intellij 上做什么,JavaFX 运行时版本都不会更新
- python - 在 Python 中使用 awk 在文件中插入文本
- c# - 最顶部的设置选项卡 = true C#