Java GUI之FlowLayout.CENTER


Java GUI之FlowLayout.CENTER

 
  1. package com.sinosuperman.driver;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.awt.FlowLayout;  
  5. import java.awt.Toolkit;  
  6.   
  7. import javax.swing.JButton;  
  8. import javax.swing.JFrame;  
  9. import javax.swing.WindowConstants;  
  10.   
  11. public class MainBench {  
  12.     public static void main(String[] args) {  
  13.         MyFrame frame = new MyFrame();  
  14.         frame.setVisible(true);  
  15.     }  
  16. }  
  17.   
  18. class MyFrame extends JFrame {  
  19.     private static final long serialVersionUID = 1L;  
  20.     private JButton okButton, exitButton;  
  21.     Toolkit tk = Toolkit.getDefaultToolkit();  
  22.     Dimension d = tk.getScreenSize();  
  23.     public MyFrame() {  
  24.         this.setLayout(new FlowLayout(FlowLayout.CENTER));  
  25.         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  
  26.         this.setSize((int) d.getWidth() / 2, (int) d.getHeight() / 2);  
  27.         this.setLocation((int) (d.getWidth() - this.getWidth()) / 2, (int) (d.getHeight() - this.getHeight()) / 2);  
  28.         okButton = new JButton();  
  29.         exitButton = new JButton();  
  30.         okButton.setText("OK");  
  31.         exitButton.setText("Exit");  
  32.         this.add(okButton);  
  33.         this.add(exitButton);  
  34.     }  
  35. }  

相关内容