
正文
java代码打开新窗口 java 弹出新窗口
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java中怎么在一个窗体点击一个按钮打开另一个窗体?
假如你的那个按钮叫button,你要打开的那个窗体的类名叫Form2.
你在button的click事件里面写个
Form2 fm=new Form2();
fm.show();
就行了。。当然,你的Form2类,要设置Visible为True,同时设置大小位置。不然,你看不到窗体。
给你贴个代码,你自己看吧
该代码经过调试,验证可行。
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;
public class formshow extends JFrame implements ActionListener{
JButton button;
public formshow(){
button=new JButton("点击我,出现新窗体");
add(button);
button.addActionListener(this);
this.setLayout(new FlowLayout());
this.setBounds(520, 130, 200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
formshow fs=new formshow();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==button){
form2 fm=new form2();
}
}
}
class form2 extends JFrame{
//第二个窗体;
JLabel jl;
static int n=1;
public form2(){
n++;
jl=new JLabel("我是第"+n+"个窗体。");
add(jl);
this.setTitle("我是第"+n+"个窗体");
//设置属性。
this.setLayout(new FlowLayout());
this.setBounds(120+11*n, 230+10*n, 200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
相关问答
Q1: java如何打开编程窗口
新手建议要么下载个editplus编辑。。懒人的做法就是:新建一个文本文档写java代码,写完后,另存为 【XXX.java】后缀名为java类型的文件、、记得是后缀名,而不是后面有.java就行了、、、然后Win+R - cmd - 回车。打开cmd命令窗口。。。然后加入你那个java文件的文件夹路径。。(右击java文件复制属性就有了,然后在cmd命令窗口中输入: cd +路径 记得cd与路径间有空格的)要是你的文件时房在C盘的,输入【C:】回车就OK了,D盘输入【D:】,以此类推、、然后输入:javac XXX.java (回车)编译成功就会产生一个class文件在当前目录。然后再输入:java XXX (回车)结果就出来了、、、、我是学java编程的,不会的可以找我。。纯手打。望采纳、、、、、、
Q2: java打开新窗口,原窗口不可操作,关闭新窗口后,原窗口才可操作
对于自定义窗体来说,最简单直接的做法就是让新窗体继承javax.swing.JDialog(对话框属于顶级窗口,跟JFrame同级),在创建该窗体后加上setModalityType()就可实现打开新窗口后原窗口不能动作,关闭新窗口后方可动作的操作。
NewFrame nf = new NewFrame(); // 继承JDialog的窗体类
nf.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); // 设置模式类型。
// 参数 APPLICATION_MODAL:阻塞同一 Java 应用程序中的所有顶层窗口(它自己的子层次
// 结构中的顶层窗口除外)。
nf.setVisible(true);
P.S.. setModalityType()只有JDialog类才有。
非要用JFrame类的话,目前可行的只有设置setEnable(false),效果一样。但对于多个自定义的窗体类不在同一个类文件中时操作起来相对不友好。需进行监听操作以‘唤醒’窗体。
Q3: java弹出新窗口
定义一个按钮的OnClick事件
里面用写方法调用弹出窗口
代码
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Frame1 extends JFrame
{
private JButton jButton1=new JButton();
public Frame1 ()
{
try {
jbInit();
}
catch(Exception exception) {
exception.printStackTrace();
}
this.setVisible(true);
}
private void jbInit () throws Exception
{
this.setBounds(300,180,400,300);
getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(127, 120, 139, 36));
jButton1.setMnemonic('C');
jButton1.setText("点我(C)");
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jButton1);
}
public static void main (String[] args)
{
Frame1 frame1=new Frame1();
}
public void jButton1_actionPerformed (ActionEvent e)
{
this.setVisible(false);
JFrame jf1=new JFrame("子窗口");
jf1.setBounds(100,50,800,600);
jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
jf1.setVisible(true);
}
}
Q4: 如何在java程序中,当点击一个按钮后,关闭当前窗口,开启一个新的窗口?
JButton btn=new JButton(new AbstractAction("关闭并打开") { @Override public void actionPerformed(ActionEvent e) { oldFrame.dispose();// 关闭并销毁,无需销毁可采用oldFrame.setVisible(false); newFrame.setVisible(true);// 打开新窗口 }});
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.*;
public class Swing7 extends JFrame implements ActionListener {
JButton jb = new JButton();
public Swing7() {
this.setTitle("Java——");
jb.setText("确定");
jb.setMnemonic('a');
this.add(jb);
this.setBounds(200, 300, 250, 300);
ctionListener就是Swing7实例。
}
public void actionPerformed(ActionEvent e) {// 实现ActionListener接口的actionPerformed接口。
JFrame frame = new JFrame("新窗口");//构造一个新的JFrame,作为新窗口。
frame.setBounds(// 让新窗口与Swing7窗口示例错开50像素。
new Rectangle(
(int) this.getBounds().getX() + 50,
(int) this.getBounds().getY() + 50,
(int) this.getBounds().getWidth(),
(int) this.getBounds().getHeight()
)
);
JLabel jl = new JLabel();// 注意类名别写错了。
frame.getContentPane().add(jl);
jl.setText("这是新窗口");
jl.setVerticalAlignment(JLabel.CENTER);
jl.setHorizontalAlignment(JLabel.CENTER);// 注意方法名别写错了。
frame.setVisible(true);
}
public static void main(String args[]) {
Swing7 s = new Swing7();
}
}
关于java代码打开新窗口和java 弹出新窗口的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






