jframe怎么从一个界面转到另一个界面
时间: 2023-09-17 08:11:26 浏览: 159
要从一个 JFrame 转到另一个 JFrame,可以使用以下步骤:
1. 在第一个 JFrame 中添加一个 JButton(或其他触发器),并为其添加一个 ActionListener。
2. 在 ActionListener 中创建一个新的 JFrame 对象。
3. 设置新 JFrame 的大小和位置,并将其设置为可见。
4. 在第一个 JFrame 中调用 setVisible(false),将其隐藏。
以下是一个示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstFrame extends JFrame {
private JButton button;
public FirstFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLayout(new FlowLayout());
button = new JButton("Go to Second Frame");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 创建新的 JFrame 对象
SecondFrame secondFrame = new SecondFrame();
// 设置新 JFrame 的大小和位置,并将其设置为可见
secondFrame.setSize(300, 200);
secondFrame.setLocationRelativeTo(null);
secondFrame.setVisible(true);
// 隐藏第一个 JFrame
setVisible(false);
}
});
this.add(button);
this.setVisible(true);
}
public static void main(String[] args) {
new FirstFrame();
}
}
class SecondFrame extends JFrame {
public SecondFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
JLabel label = new JLabel("This is the Second Frame");
this.add(label);
}
}
```
在这个示例中,点击 "Go to Second Frame" 按钮会创建一个新的 SecondFrame 对象,并将其设置为可见。同时,第一个 FirstFrame 对象会被隐藏。
阅读全文
相关推荐
















