如何在 JFrames 之间来回切换
How to switch back and forth between JFrames
首先,我只是在玩按钮和 JFrame。问题是我可以在这里用几行代码从一个 Jframe 切换到下一个 Jframe...
JButton studentLoginButton = new JButton("Student");
studentLoginButton.setBounds(85, 80, 80, 20);
studentLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(studentLoginButton);
但是当我将学生 JFrame 设置为可见并使用此处的代码时...
JButton cancelButton = new JButton("Go Back");
cancelButton.setBounds(205, 80, 80, 20);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
StudentFrame.super.setVisible(false);
login1.setVisible(true);
}
});
add(cancelButton);
取消并返回是行不通的。没有任何显示,应用程序未终止。我能做些什么来解决这个问题?如果它可以帮助其他人找到解决方案,我可以提供更多代码。
看起来更多的代码会对这里有帮助 类。
LoginFrame.java...
package system;
import java.awt.FlowLayout;
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.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
StudentFrame student;
public static void main(String[] args) {
new LoginFrame().setVisible(true);
}
LoginFrame(){
super(" User Login ");
setSize(400, 80);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
StudentFrame student = new StudentFrame(null);
setLayout(new FlowLayout());
//student
JButton studentLoginButton = new JButton("Student");
studentLoginButton.setBounds(85, 80, 80, 20);
studentLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(studentLoginButton);
//Dept. staff
JButton deptStaffLoginButton = new JButton("Department Staff");
deptStaffLoginButton.setBounds(85, 80, 80, 20);
deptStaffLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(deptStaffLoginButton);
//Instructor
JButton InstructorLoginButton = new JButton("Instructor");
InstructorLoginButton.setBounds(85, 80, 80, 20);
InstructorLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(InstructorLoginButton);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
接下来是...
StudentFrame.java
包系统;
import java.awt.FlowLayout;
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.JPasswordField;
import javax.swing.JTextField;
public class StudentFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
static LoginFrame login1;
public static void main(String[] args) {
new StudentFrame(login1).setVisible(true);
}
StudentFrame(LoginFrame login){
super(" User Login ");
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
login1 = login;
// LoginFrame login = new LoginFrame();
JButton loginButton = new JButton("Login");
loginButton.setBounds(85, 80, 80, 20);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// LoginFrame.super.setVisible(false);
// student.setVisible(true);
}
});
add(loginButton);
JButton cancelButton = new JButton("Go Back");
cancelButton.setBounds(205, 80, 80, 20);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
StudentFrame.super.setVisible(false);
login1.setVisible(true);
}
});
add(cancelButton);
JLabel passLabel = new JLabel("Password: ");
passLabel.setBounds(10, 50, 80, 25);
add(passLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(85, 50, 240, 25);
add(passwordText);
JLabel userLabel = new JLabel("User name: ");
userLabel.setBounds(10, 10, 80, 25);
add(userLabel);
JTextField userNameText = new JTextField();
userNameText.setBounds(85, 10, 240, 25);
add(userNameText);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
很难知道为什么您当前的代码无法正常工作,因为我们没有 运行ning 示例,但当我看到静态调用和像这样调用 super 时我确实很担心,StudentFrame.super.setVisible(false);
.
但不管您问题的直接解决方案如何,虽然我知道这不能回答您的问题,但最好的解决方案是:不要。不要向用户抛出不同的 JFrames,因为这对用户来说可能是一件很烦人的事情,尤其是当有更好的交换视图的方法时,最好的方法是使用 CardLayout 来交换 JPanel 视图,这正是我的建议。您可以在此处找到教程:CardLayout Tutorial.
也请看这个link:The Use of Multiple JFrames, Good/Bad Practice?
另一个选项,如果你想使用登录对话框就是这样做,使用模态 JDialog 来显示登录 window,并使其脱离主应用程序。
有关这些解决方案的详细信息,请显示更相关的代码。
作为 "side" 的推荐,我不得不提一下你打给 setBounds
的电话。虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用它们时就会 运行 遇到更严重的困难。它们不会在 GUI 调整大小时调整组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失效,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕来自原来的
在进一步审查您的代码后,我相信您最好的选择是使用模态 JDialog 来显示登录 window。它的行为类似于 JFrame,但它使用不同的构造函数(API 可以帮助您)。其行为的主要区别在于,它会在调用代码将 JDialog 设置为可见时立即停止调用代码——就像 JOptionPane 所做的那样(请注意,此 是 一个专门的 JDialog 本身) .这样,调用代码将知道 JDialog 何时已被处理并且不再可见,因为它的代码流在它设置 JDialog 可见之后立即恢复。这样,主 GUI 中的代码就可以显示对话框,然后在用户完成操作后从中获取信息。
首先,我只是在玩按钮和 JFrame。问题是我可以在这里用几行代码从一个 Jframe 切换到下一个 Jframe...
JButton studentLoginButton = new JButton("Student");
studentLoginButton.setBounds(85, 80, 80, 20);
studentLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(studentLoginButton);
但是当我将学生 JFrame 设置为可见并使用此处的代码时...
JButton cancelButton = new JButton("Go Back");
cancelButton.setBounds(205, 80, 80, 20);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
StudentFrame.super.setVisible(false);
login1.setVisible(true);
}
});
add(cancelButton);
取消并返回是行不通的。没有任何显示,应用程序未终止。我能做些什么来解决这个问题?如果它可以帮助其他人找到解决方案,我可以提供更多代码。
看起来更多的代码会对这里有帮助 类。 LoginFrame.java...
package system;
import java.awt.FlowLayout;
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.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
StudentFrame student;
public static void main(String[] args) {
new LoginFrame().setVisible(true);
}
LoginFrame(){
super(" User Login ");
setSize(400, 80);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
StudentFrame student = new StudentFrame(null);
setLayout(new FlowLayout());
//student
JButton studentLoginButton = new JButton("Student");
studentLoginButton.setBounds(85, 80, 80, 20);
studentLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(studentLoginButton);
//Dept. staff
JButton deptStaffLoginButton = new JButton("Department Staff");
deptStaffLoginButton.setBounds(85, 80, 80, 20);
deptStaffLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(deptStaffLoginButton);
//Instructor
JButton InstructorLoginButton = new JButton("Instructor");
InstructorLoginButton.setBounds(85, 80, 80, 20);
InstructorLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(InstructorLoginButton);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
接下来是... StudentFrame.java 包系统;
import java.awt.FlowLayout;
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.JPasswordField;
import javax.swing.JTextField;
public class StudentFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
static LoginFrame login1;
public static void main(String[] args) {
new StudentFrame(login1).setVisible(true);
}
StudentFrame(LoginFrame login){
super(" User Login ");
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
login1 = login;
// LoginFrame login = new LoginFrame();
JButton loginButton = new JButton("Login");
loginButton.setBounds(85, 80, 80, 20);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// LoginFrame.super.setVisible(false);
// student.setVisible(true);
}
});
add(loginButton);
JButton cancelButton = new JButton("Go Back");
cancelButton.setBounds(205, 80, 80, 20);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
StudentFrame.super.setVisible(false);
login1.setVisible(true);
}
});
add(cancelButton);
JLabel passLabel = new JLabel("Password: ");
passLabel.setBounds(10, 50, 80, 25);
add(passLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(85, 50, 240, 25);
add(passwordText);
JLabel userLabel = new JLabel("User name: ");
userLabel.setBounds(10, 10, 80, 25);
add(userLabel);
JTextField userNameText = new JTextField();
userNameText.setBounds(85, 10, 240, 25);
add(userNameText);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
很难知道为什么您当前的代码无法正常工作,因为我们没有 运行ning 示例,但当我看到静态调用和像这样调用 super 时我确实很担心,StudentFrame.super.setVisible(false);
.
但不管您问题的直接解决方案如何,虽然我知道这不能回答您的问题,但最好的解决方案是:不要。不要向用户抛出不同的 JFrames,因为这对用户来说可能是一件很烦人的事情,尤其是当有更好的交换视图的方法时,最好的方法是使用 CardLayout 来交换 JPanel 视图,这正是我的建议。您可以在此处找到教程:CardLayout Tutorial.
也请看这个link:The Use of Multiple JFrames, Good/Bad Practice?
另一个选项,如果你想使用登录对话框就是这样做,使用模态 JDialog 来显示登录 window,并使其脱离主应用程序。
有关这些解决方案的详细信息,请显示更相关的代码。
作为 "side" 的推荐,我不得不提一下你打给 setBounds
的电话。虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用它们时就会 运行 遇到更严重的困难。它们不会在 GUI 调整大小时调整组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失效,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕来自原来的
在进一步审查您的代码后,我相信您最好的选择是使用模态 JDialog 来显示登录 window。它的行为类似于 JFrame,但它使用不同的构造函数(API 可以帮助您)。其行为的主要区别在于,它会在调用代码将 JDialog 设置为可见时立即停止调用代码——就像 JOptionPane 所做的那样(请注意,此 是 一个专门的 JDialog 本身) .这样,调用代码将知道 JDialog 何时已被处理并且不再可见,因为它的代码流在它设置 JDialog 可见之后立即恢复。这样,主 GUI 中的代码就可以显示对话框,然后在用户完成操作后从中获取信息。