java窗口跳转

假如有两个frame,分别为frame1,frame2,frame1加个按钮实现跳转.frame1代码如下
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class frame1 extends JFrame implements ActionListener{

/**
* @param args
*/
private JButton jb;
public frame1()
{
this.setSize(300, 200);
this.setLocation(300, 400);
jb=new JButton("跳转");
this.add(jb);
jb.addActionListener(this);//加入事件监听
this.setVisible(true);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
frame1 frame=new frame1();

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb)
{
this.dispose();//点击按钮时frame1销毁,new一个frame2
new frame2();
}
}

}

frame2是个单纯的 界面
import javax.swing.JButton;
import javax.swing.JFrame;

public class frame2 extends JFrame{

/**
* @param args
*/
public frame2()
{

this.setSize(300, 200);
this.setLocation(300, 400);

this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
frame2 frame=new frame2();
}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69917861/viewspace-2653083/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/69917861/viewspace-2653083/

  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Java窗口跳转到新窗口,可以使用Java Swing的JFrame和JDialog组件。 首先,您需要创建一个JFrame对象作为主窗口。然后,在该窗口中添加一个按钮或其他组件,以便当用户单击该组件时打开新窗口。 以下是一个简单的示例代码,演示如何实现窗口跳转: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class WindowSwitcher extends JFrame implements ActionListener { private JButton openButton; public WindowSwitcher() { super("Main Window"); // 创建一个按钮并将其添加到主窗口 openButton = new JButton("Open New Window"); openButton.addActionListener(this); getContentPane().add(openButton, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { if (e.getSource() == openButton) { // 创建一个新对话框并打开它 JDialog dialog = new JDialog(this, "New Window", true); dialog.setSize(200, 100); dialog.setLocationRelativeTo(null); dialog.setVisible(true); } } public static void main(String[] args) { WindowSwitcher mainWindow = new WindowSwitcher(); mainWindow.setSize(300, 200); mainWindow.setLocationRelativeTo(null); mainWindow.setVisible(true); } } ``` 在这个例子中,我们创建了一个JFrame对象作为主窗口,并添加了一个名为“Open New Window”的按钮。当用户单击该按钮时,我们创建了一个JDialog对象作为新窗口,并将其显示出来。 请注意,我们将JDialog的第三个参数设置为true,这意味着它是一个模态对话框,当它处于打开状态时,用户无法与主窗口进行交互。如果您不想使用模态对话框,则可以将该参数设置为false。 希望这可以帮助您实现窗口跳转功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值