Java GUI之FlowLayout.CENTER
- package com.sinosuperman.driver;
-
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Toolkit;
-
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.WindowConstants;
-
- public class MainBench {
- public static void main(String[] args) {
- MyFrame frame = new MyFrame();
- frame.setVisible(true);
- }
- }
-
- class MyFrame extends JFrame {
- private static final long serialVersionUID = 1L;
- private JButton okButton, exitButton;
- Toolkit tk = Toolkit.getDefaultToolkit();
- Dimension d = tk.getScreenSize();
- public MyFrame() {
- this.setLayout(new FlowLayout(FlowLayout.CENTER));
- this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- this.setSize((int) d.getWidth() / 2, (int) d.getHeight() / 2);
- this.setLocation((int) (d.getWidth() - this.getWidth()) / 2, (int) (d.getHeight() - this.getHeight()) / 2);
- okButton = new JButton();
- exitButton = new JButton();
- okButton.setText("OK");
- exitButton.setText("Exit");
- this.add(okButton);
- this.add(exitButton);
- }
- }