import javax.swing.*;
import java.awt.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//顶级容器JFrame的实例化
public class container extends JFrame implements ActionListener {
JButton _model =new JButton("OPEN MODEL DIALOG");
JButton _nomodel =new JButton("CLOSE MODEL DIALOG");
JPanel _top=new JPanel();//实例化中间窗体 JPanel
public container()
{
super("JFrame TEST");
init();
}
public void init()
{
setSize(480,320);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);//定义窗口关闭按键设置
setLayout(new BorderLayout());//定义布局
_model.addActionListener(this);
_nomodel.addActionListener(this);
_top.add(_model);
_top.add(_nomodel);
add(_top,BorderLayout.NORTH);
setVisible(true);
}
//关联事件源
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(_model)){
new MyDialog(this,true,"Model Didlog");
}
if(e.getSource().equals(_nomodel)){
new MyDialog(this,true,"Nomodel Didlog");
}
}
public static void main(String args[])
{
container window =new container();
}
}
//实例化JDialog
class MyDialog extends JDialog implements ActionListener{
private JButton _ok =new JButton("YES");//定义按键文本
public MyDialog(JFrame x, boolean modal, String title)
{
super(x,title,modal);
init();
}
void init()
{
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);//关闭时隐藏窗口
setSize(300,200);
setLocationRelativeTo(null);
_ok.addActionListener(this);
add(_ok, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
dispose();//关闭窗体并释放资源
}
}
JFrame和JDialog的简单实现
最新推荐文章于 2023-12-21 01:45:21 发布