Java用户界面设计:怎样为按钮设置监听,在点击按钮的时候,弹出一个新的窗口
我自己做了一个程序,实现的界面如下:我想在点击“登录”和“注册”按钮后弹出一个新的窗口,可是不知怎么做,请求帮助!我的程序如下:importjava.awt.*;impo...
我自己做了一个程序,实现的界面如下:
我想在点击“登录”和“注册”按钮后弹出一个新的窗口,可是不知怎么做,请求帮助!
我的程序如下:
import java.awt.*;import java.awt.event.*;
public class bysj { static Frame frm = new Frame("中小型超市薪酬管理系统");
public static void main(String[] args){
BorderLayout border = new BorderLayout(5,10);
//GridLayout grid = new GridLayout(2,2);
Panel pan1 = new Panel();
pan1.setSize(500,150);
Panel pan2 = new Panel();
pan2.setSize(500,150);
Panel pan3 = new Panel();
pan3.setSize(500,150);
Label label1 = new Label("欢迎登录**超市薪酬管理系统");
label1.setSize(500,50);
Label label2 = new Label("用户名:");
label2.setLocation(10,10);
label2.setSize(30,50);
Label label3 = new Label("密码:");
label3.setLocation(10,70);
label3.setSize(30,50);
Button bt1 = new Button("登录");
bt1.setBounds(40,70,100,40);
Button bt2 = new Button("注册");
bt2.setBounds(150,70,100,40);
TextField tf1 = new TextField("",20);
tf1.setBounds(50, 10, 50, 50);
TextField tf2 = new TextField("",20);
tf2.setEchoChar('*');
tf2.setBounds(10, 70, 30, 50);
pan1.setBackground(Color.gray);
pan2.setBackground(Color.gray);
//pan2.setLayout(grid);
pan3.setBackground(Color.gray);
frm.setLayout(border);
frm.setSize(600,400);
frm.setBackground(Color.gray);
frm.setLocation(350,100);
pan1.add(label1);
pan2.add(label2);
pan2.add(tf1);
pan2.add(label3);
pan2.add(tf2);
pan3.add(bt1);
pan3.add(bt2);
frm.add(pan1,BorderLayout.NORTH);
frm.add(pan2,BorderLayout.CENTER);
frm.add(pan3,BorderLayout.SOUTH);
frm.setVisible(true);
frm.addWindowListener(new WinCloser());
}
public static class WinCloser extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
十分感谢!!最好帮我在此程序的基础上改写一下。谢谢…… 展开
我想在点击“登录”和“注册”按钮后弹出一个新的窗口,可是不知怎么做,请求帮助!
我的程序如下:
import java.awt.*;import java.awt.event.*;
public class bysj { static Frame frm = new Frame("中小型超市薪酬管理系统");
public static void main(String[] args){
BorderLayout border = new BorderLayout(5,10);
//GridLayout grid = new GridLayout(2,2);
Panel pan1 = new Panel();
pan1.setSize(500,150);
Panel pan2 = new Panel();
pan2.setSize(500,150);
Panel pan3 = new Panel();
pan3.setSize(500,150);
Label label1 = new Label("欢迎登录**超市薪酬管理系统");
label1.setSize(500,50);
Label label2 = new Label("用户名:");
label2.setLocation(10,10);
label2.setSize(30,50);
Label label3 = new Label("密码:");
label3.setLocation(10,70);
label3.setSize(30,50);
Button bt1 = new Button("登录");
bt1.setBounds(40,70,100,40);
Button bt2 = new Button("注册");
bt2.setBounds(150,70,100,40);
TextField tf1 = new TextField("",20);
tf1.setBounds(50, 10, 50, 50);
TextField tf2 = new TextField("",20);
tf2.setEchoChar('*');
tf2.setBounds(10, 70, 30, 50);
pan1.setBackground(Color.gray);
pan2.setBackground(Color.gray);
//pan2.setLayout(grid);
pan3.setBackground(Color.gray);
frm.setLayout(border);
frm.setSize(600,400);
frm.setBackground(Color.gray);
frm.setLocation(350,100);
pan1.add(label1);
pan2.add(label2);
pan2.add(tf1);
pan2.add(label3);
pan2.add(tf2);
pan3.add(bt1);
pan3.add(bt2);
frm.add(pan1,BorderLayout.NORTH);
frm.add(pan2,BorderLayout.CENTER);
frm.add(pan3,BorderLayout.SOUTH);
frm.setVisible(true);
frm.addWindowListener(new WinCloser());
}
public static class WinCloser extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
十分感谢!!最好帮我在此程序的基础上改写一下。谢谢…… 展开
展开全部
程序改好了你在OpenJFrame这个类中画界面就行了, ML 这个类是一个监听功能,给你的按钮加上了监听,另外建议不要再main函数中写界面的代码 。
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;import javax.swing.JLabel;
import javax.swing.JPanel;
public class bysj{
static Frame frm = new Frame("中小型超市薪酬管理系统");
public static void main(String[] args){
BorderLayout border = new BorderLayout(5,10);
//GridLayout grid = new GridLayout(2,2);
Panel pan1 = new Panel();
pan1.setSize(500,150);
Panel pan2 = new Panel();
pan2.setSize(500,150);
Panel pan3 = new Panel();
pan3.setSize(500,150);
Label label1 = new Label("欢迎登录**超市薪酬管理系统");
label1.setSize(500,50);
Label label2 = new Label("用户名:");
label2.setLocation(10,10);
label2.setSize(30,50);
Label label3 = new Label("密码:");
label3.setLocation(10,70);
label3.setSize(30,50);
Button bt1 = new Button("登录");
bt1.setBounds(40,70,100,40); bt1.addActionListener(new ML());
Button bt2 = new Button("注册");
bt2.setBounds(150,70,100,40); bt2.addActionListener(new ML());
TextField tf1 = new TextField("",20);
tf1.setBounds(50, 10, 50, 50);
TextField tf2 = new TextField("",20);
tf2.setEchoChar('*');
tf2.setBounds(10, 70, 30, 50);
pan1.setBackground(Color.gray);
pan2.setBackground(Color.gray);
//pan2.setLayout(grid);
pan3.setBackground(Color.gray);
frm.setLayout(border);
frm.setSize(600,400);
frm.setBackground(Color.gray);
frm.setLocation(350,100);
pan1.add(label1);
pan2.add(label2);
pan2.add(tf1);
pan2.add(label3);
pan2.add(tf2);
pan3.add(bt1);
pan3.add(bt2);
frm.add(pan1,BorderLayout.NORTH);
frm.add(pan2,BorderLayout.CENTER);
frm.add(pan3,BorderLayout.SOUTH);
frm.setVisible(true);
frm.addWindowListener(new WinCloser());
}
public static class WinCloser extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}}
class ML implements ActionListener{ public void actionPerformed(ActionEvent event){
Button eventobj = (Button)event.getSource();
String labelname = eventobj.getLabel();
if("登录".equals(labelname)){
new bysj().frm.dispose();
new OpenJFrame();
System.out.println("我是登录 我要关闭当前页面 然后打开其他页面");
}else{
new bysj().frm.dispose();
new OpenJFrame();
System.out.println("我是注册 我要关闭当前页面 然后打开其他页面");
}
}
}
class OpenJFrame extends JFrame{
public OpenJFrame(){
JPanel jp2 = new JPanel(new BorderLayout());
this.setSize(380,245);
//设置此窗口永远为最上方 是window的方法
this.setAlwaysOnTop(true);
//不允许用户改变窗口的大小
this.setResizable(false);
//让窗口在屏幕的正中间显示
this.setLocationRelativeTo(null);
this.setTitle("新的窗口");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;import javax.swing.JLabel;
import javax.swing.JPanel;
public class bysj{
static Frame frm = new Frame("中小型超市薪酬管理系统");
public static void main(String[] args){
BorderLayout border = new BorderLayout(5,10);
//GridLayout grid = new GridLayout(2,2);
Panel pan1 = new Panel();
pan1.setSize(500,150);
Panel pan2 = new Panel();
pan2.setSize(500,150);
Panel pan3 = new Panel();
pan3.setSize(500,150);
Label label1 = new Label("欢迎登录**超市薪酬管理系统");
label1.setSize(500,50);
Label label2 = new Label("用户名:");
label2.setLocation(10,10);
label2.setSize(30,50);
Label label3 = new Label("密码:");
label3.setLocation(10,70);
label3.setSize(30,50);
Button bt1 = new Button("登录");
bt1.setBounds(40,70,100,40); bt1.addActionListener(new ML());
Button bt2 = new Button("注册");
bt2.setBounds(150,70,100,40); bt2.addActionListener(new ML());
TextField tf1 = new TextField("",20);
tf1.setBounds(50, 10, 50, 50);
TextField tf2 = new TextField("",20);
tf2.setEchoChar('*');
tf2.setBounds(10, 70, 30, 50);
pan1.setBackground(Color.gray);
pan2.setBackground(Color.gray);
//pan2.setLayout(grid);
pan3.setBackground(Color.gray);
frm.setLayout(border);
frm.setSize(600,400);
frm.setBackground(Color.gray);
frm.setLocation(350,100);
pan1.add(label1);
pan2.add(label2);
pan2.add(tf1);
pan2.add(label3);
pan2.add(tf2);
pan3.add(bt1);
pan3.add(bt2);
frm.add(pan1,BorderLayout.NORTH);
frm.add(pan2,BorderLayout.CENTER);
frm.add(pan3,BorderLayout.SOUTH);
frm.setVisible(true);
frm.addWindowListener(new WinCloser());
}
public static class WinCloser extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}}
class ML implements ActionListener{ public void actionPerformed(ActionEvent event){
Button eventobj = (Button)event.getSource();
String labelname = eventobj.getLabel();
if("登录".equals(labelname)){
new bysj().frm.dispose();
new OpenJFrame();
System.out.println("我是登录 我要关闭当前页面 然后打开其他页面");
}else{
new bysj().frm.dispose();
new OpenJFrame();
System.out.println("我是注册 我要关闭当前页面 然后打开其他页面");
}
}
}
class OpenJFrame extends JFrame{
public OpenJFrame(){
JPanel jp2 = new JPanel(new BorderLayout());
this.setSize(380,245);
//设置此窗口永远为最上方 是window的方法
this.setAlwaysOnTop(true);
//不允许用户改变窗口的大小
this.setResizable(false);
//让窗口在屏幕的正中间显示
this.setLocationRelativeTo(null);
this.setTitle("新的窗口");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询