首先分析需要的GUI技术
java中一般使用swing 和awt技术来实现图形界面,Swing组件较多,功能比较强大,所以这里使用Swing组件来实现。窗口使用(JFrame),按钮使用(JButton)。
设想一个符合题目需求的场景
两个窗口关联并且跳转,最常见的场景就是登陆了。
登陆窗口,输入用户名和密码,如果成功就跳转到主窗口
核心逻辑,跳转窗口的关键代码
一般可以隐藏本窗口(setVisible(false)),显示新窗口来实现窗口的跳转。
但是隐藏窗口,会在后面浪费系统资源。
如果不再使用该窗口,应该彻底销毁(dispose()),释放系统资源
代码和注释
LoginFrame 类
import java.awt.*import java.awt.event.*
import javax.swing.*
//LoginFrame 登陆窗口 功能 实现用户登陆 ,如果成功就跳转
//LoginFrame 继承于窗口类, 并实现ActionListener接口
public class LoginFrame extends JFrame implements ActionListener {
// 申明需要的组件
JButton jb1, jb2//按钮
JTextField jtf1//文本框
JPasswordField jpf1//密码框
public LoginFrame() {
// 窗口属性的设置
setTitle("登陆窗")// 窗口标题
setSize(300, 180)// 窗口大小
setLocationRelativeTo(null)// 窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE)// 关闭窗口则退出虚拟机
setLayout(new FlowLayout())// 设置布局流式布局
JPanel jp = new JPanel(new GridLayout(4, 1))// 设置面板为表格布局4行1列
// 第一行
JPanel jp1 = new JPanel()
JLabel jl1 = new JLabel("账号 ")
jtf1 = new JTextField(12)
jp1.add(jl1)
jp1.add(jtf1)
jp.add(jp1)
// 第二行
JPanel jp2 = new JPanel()
JLabel jl2 = new JLabel("密码 ")
jpf1 = new JPasswordField(12)
jp2.add(jl2)
jp2.add(jpf1)
jp.add(jp2)
// 第三行
JPanel jp3 = new JPanel()
jb1 = new JButton("登陆")
jb1.addActionListener(this)// 添加动作响应器
jb2 = new JButton("重置")
jb2.addActionListener(this)// 添加动作响应器
jp3.add(jb1)
jp3.add(jb2)
jp.add(jp3)
// 第四行
JPanel jp4 = new JPanel()
JLabel jl3 = new JLabel("提示: 账号 admin 密码 123")
jl3.setForeground(Color.DARK_GRAY)
jp4.add(jl3)
jp.add(jp4)
add(jp)
}
// 动作响应
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand()// 根据动作命令,来进行分别处理
if (cmd.equals("登陆")) {
String id = jtf1.getText()// 取得用户名
String key = new String(jpf1.getPassword())// 取得密码
if (id.equals("admin") && key.equals("123")) {// 判断是否登录成功
// 如果登录成功
setVisible(false)// 本窗口隐藏,
new MainFrame(id).setVisible(true)// 新窗口显示
dispose()//本窗口销毁,释放内存资源
} else {
//如果登录失败 弹出提示
JOptionPane.showMessageDialog(this, "用户名或者密码错误.", "通知", JOptionPane.ERROR_MESSAGE)
clearText()//清空文本框 密码框的输入
}
} else if (cmd.equals("重置")) {
clearText()
}
}
private void clearText() {//清空文本框, 密码框的输入
jtf1.setText("")
jpf1.setText("")
}
//main方法, 程序的入口
public static void main(String[] args) {
new LoginFrame().setVisible(true)//创建登录窗口,并可见
}
}
MainFrame类
import java.awt.*import java.awt.event.*
import javax.swing.*
public class MainFrame extends JFrame {
public MainFrame(String name) {
setTitle("主窗口")//标题
setSize(300, 260)//大小
setLocationRelativeTo(null)//居中
setDefaultCloseOperation(EXIT_ON_CLOSE)
JPanel jp = new JPanel()
JLabel jl1 = new JLabel("欢迎")
JLabel jl2 = new JLabel(name)
jl2.setForeground(Color.BLUE)//设置文本颜色
JLabel jl3 = new JLabel("使用系统.")
jp.add(jl1)
jp.add(jl2)
jp.add(jl3)
add(jp, BorderLayout.NORTH)
JTextArea jta = new JTextArea()
jta.setLineWrap(true)
jta.append("消息提示")
jta.append(" 消息!通知~!")
JScrollPane jsp = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)
add(jsp)
JPanel jp2 = new JPanel()
JButton jb = new JButton("退出")
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0)//退出
}
})
jp2.add(jb)
add(jp2,BorderLayout.SOUTH)
}
}
运行效果图
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()
}
}
对于自定义窗体来说,最简单直接的做法就是让新窗体继承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),效果一样。但对于多个自定义的窗体类不在同一个类文件中时操作起来相对不友好。需进行监听操作以‘唤醒’窗体。