Java 如何在JFrames之间来回切换
首先,我只是在玩按钮和JFrame。问题是我可以在这里用几行代码从一个Jframe切换到下一个JframeJava 如何在JFrames之间来回切换,java,jframe,actionlistener,Java,Jframe,Actionlistener,首先,我只是在玩按钮和JFrame。问题是我可以在这里用几行代码从一个Jframe切换到下一个Jframe JButton studentLoginButton = new JButton("Student"); studentLoginButton.setBounds(85, 80, 80, 20); studentLoginButton.addActionListener(new ActionListener() { public void actio
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);
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);
但是当我将student 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);
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);
要取消并返回,它不起作用。未显示任何内容,且应用程序未终止。我能做些什么来解决这个问题?如果能帮助其他人找到解决方案,我可以提供更多的代码
看起来更多的代码将有助于实现这两个类。
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
}
}
很难知道为什么您当前的代码不起作用,因为我们没有运行的示例,但是当我看到静态调用,以及像这样调用super时,我确实会担心,
StudentFrame.super.setVisible(false)代码>
但不管你问题的直接解决方案是什么,虽然我知道这并不能回答你的问题,但最好的解决方案是:不要。不要向用户抛出不同的JFrame,因为这对用户来说是一件非常烦人的事情,尤其是当有更好的交换视图的方法时,这里最好的方法是使用CardLayout交换JPanel视图,这正是我的建议。您可以在此处找到教程:
另外,请查看此链接:
另一个选项是,如果您想使用登录对话框,那么可以使用模态JDialog来显示登录窗口,并使其脱离主应用程序的模态
有关这两种解决方案的详细信息,请显示更相关的代码
作为一项“附带”建议,我必须提及您致电setBounds
。虽然空布局和setBounds()
可能会像创建复杂GUI的最简单和最好的方式一样吸引新手,但您创建的GUI越多,在使用它们时遇到的困难就越严重。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,当它们放置在滚动窗格中时会完全失败,当在所有平台或屏幕分辨率与原始分辨率不同的情况下查看时,它们看起来非常糟糕
在进一步检查代码时,我认为最好的选择是使用模态JDialog来显示登录窗口。这与JFrame的行为类似,但是它使用不同的构造函数(API可以帮助您实现这一点)。其行为的主要区别在于,当调用代码将JDialog设置为可见时,它会立即停止调用代码——就像JOptionPane一样(请注意,本身就是专门的JDialog)。这样,调用代码将知道何时处理了JDialog,并且不再可见,因为它的代码流在设置JDialog为可见后立即恢复。通过这种方式,主GUI中的代码可以显示对话框,然后在用户使用完对话框后从中获取信息。请提供一个能准确演示您正在做什么的对话框。到目前为止,我们只能猜测student和login1是什么。此外,您可能应该使用CardLayout,而不是多个JFrame。我真的希望我添加的内容能有所帮助。。。也谢谢你的教程!