用JAVA语言实现图形用户界面类:创建两个窗口,单击窗口1中按钮打开窗口2
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Demo extends JFrame implements ActionListener{
private JButton jb;//定义按钮
private JPanel jp;//定义面板
public Demo(){//构造函数
jb = new JButton("单击这里");//实例化按钮对象
jb.addActionListener(this);//添加侦听器
jp = new JPanel();//实例化面板
jp.setLayout(new FlowLayout());
jp.setSize(400,300);
jp.setVisible(true);
jp.add(jb);
jb.setPreferredSize(new Dimension(90, 30));
jb.setVisible(true);
this.add(jp);
this.setTitle("窗口1");
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this. setLocationRelativeTo(null);
}
public static void main(String[] args){
System.out.println("程序开始运行···");
new Demo();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String arg = e.getActionCommand();//获取事件侦听参数
if (arg == "单击这里") {
Demo d = new Demo();
d.setTitle("窗口2");
d.setBounds(10,10,400,300);
}
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Demo extends JFrame implements ActionListener{
private JButton jb;//定义按钮
private JPanel jp;//定义面板
public Demo(){//构造函数
jb = new JButton("单击这里");//实例化按钮对象
jb.addActionListener(this);//添加侦听器
jp = new JPanel();//实例化面板
jp.setLayout(new FlowLayout());
jp.setSize(400,300);
jp.setVisible(true);
jp.add(jb);
jb.setPreferredSize(new Dimension(90, 30));
jb.setVisible(true);
this.add(jp);
this.setTitle("窗口1");
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this. setLocationRelativeTo(null);
}
public static void main(String[] args){
System.out.println("程序开始运行···");
new Demo();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String arg = e.getActionCommand();//获取事件侦听参数
if (arg == "单击这里") {
Demo d = new Demo();
d.setTitle("窗口2");
d.setBounds(10,10,400,300);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询