public class MySwingJDialog { |
public static void main(String[] args) |
{ |
EventQueue.invokeLater( new Runnable(){ |
@Override |
public void run() { |
// TODO Auto-generated method stub |
new MainFrame(); |
} |
|
}); |
} |
} |
class MainFrame |
{ |
private JFrame mainFrame; |
|
public MainFrame() |
{ |
mainFrame = new JFrame(); |
mainFrame.setTitle( "Main Frame" ); |
|
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
mainFrame.setSize(screenSize.width/ 2 ,screenSize.height/ 2 ); |
|
Dimension frameSize = mainFrame.getSize(); |
if (frameSize.width > screenSize.width) |
frameSize.width = screenSize.width; |
if (frameSize.height > screenSize.height) |
frameSize.height = screenSize.height; |
mainFrame.setLocation((screenSize.width-frameSize.width)/ 2 ,(screenSize.height-frameSize.height)/ 2 ); |
|
mainFrame.add(btn,BorderLayout.CENTER); |
btn.addActionListener( new BtnAction()); |
|
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
mainFrame.setVisible( true ); |
|
|
} |
|
private class BtnAction implements ActionListener |
{ |
@Override |
public void actionPerformed(ActionEvent e) { |
// TODO Auto-generated method stub |
new MyJDialog().dg.setVisible( true ); |
|
} |
|
class MyJDialog |
{ |
JDialog dg = new JDialog(mainFrame, true ); |
MyJDialog() |
{ |
dg.setTitle( "Dialog" ); |
dg.setSize( 150 , 150 ); |
|
JButton bt = new JButton( "退出" ); |
Container dg_c = dg.getContentPane(); |
bt.addActionListener( new BtAction()); |
dg_c.add(bt); |
|
} |
|
class BtAction implements ActionListener |
{ |
@Override |
public void actionPerformed(ActionEvent e) { |
// TODO Auto-generated method stub |
dg.dispose(); |
} |
|
} |
|
} |
|
} |
} |
高级设计师
by: 小蜜锋 发表于:2014-02-21 18:52:10 顶(0) | 踩(0) 回复
回复评论