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的簡單實現
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.