Java 学习之 Swing 编程

来源:互联网 发布:matlab怎么读取usb数据 编辑:程序博客网 时间:2024/05/21 09:16

主要记录java Swing编程,JFrame控制的使用以及内容面板(JPanel)的常用布局的使用。


import java.awt.BorderLayout;import java.awt.Color;import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.UIManager;import javax.swing.border.EmptyBorder;public class MyJFrame extends JFrame {/** *  */private JPanel contentPane;private static final long serialVersionUID = -4948295198859299320L;public static void main(String []args){try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");// 修改窗体的外观为Nimbus} catch (Throwable e) {// TODO: handle exceptione.printStackTrace();}EventQueue.invokeLater(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {MyJFrame frame = new MyJFrame();frame.setVisible(true);//frame.AbsoluteLayoutDemo();//frame.FlowLayoutDemo();//frame.BorderLayoutDemo();frame.GirdLayoutDemo();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}});}public MyJFrame(){setTitle("最简单的窗体");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,250,100);}// 绝对布局管理public void AbsoluteLayoutDemo(){setTitle("绝对布局");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置应用退出模式,退出时关闭setBounds(100, 100, 250, 100); //  设置窗体的大小和位置contentPane = new JPanel();  // 创建内容面板contentPane.setBorder(new EmptyBorder(5,5,5,5)); // 设置内容面板边框setContentPane(contentPane); // 使用内容面板contentPane.setLayout(null);// 设置布局管理器为nullJButton btn1 = new JButton("按钮-1");btn1.setBounds(6, 9, 90, 30);contentPane.add(btn1);btn1.setBackground(Color.GREEN);JButton btn2 = new JButton("按钮-2");btn2.setBounds(96, 39, 90, 30);btn2.setBackground(Color.RED);contentPane.add(btn2);}// 流式布局管理,根据内容面板的变化,排列也发生变化public void FlowLayoutDemo(){setTitle("流式布局");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 300, 150);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(new FlowLayout()); //设置为流式布局for (int i = 1; i < 11; i++) {JButton btn = new JButton("按钮"+i);contentPane.add(btn);}}// 边界布局管理,指定各自的方位public void BorderLayoutDemo(){setTitle("边界布局");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,450,300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));contentPane.setLayout(new BorderLayout());// 设置内容面板的布局管理器为边界布局setContentPane(contentPane);// 应用内容面板JButton westBtn = new JButton("西部");contentPane.add(westBtn, BorderLayout.WEST);JButton eastBtn = new JButton("东部");contentPane.add(eastBtn, BorderLayout.EAST);JButton northBtn = new JButton("北部");contentPane.add(northBtn, BorderLayout.NORTH);JButton southBtn = new JButton("南部");contentPane.add(southBtn, BorderLayout.SOUTH);JButton centerBtn = new JButton("中部");contentPane.add(centerBtn, BorderLayout.CENTER);}// 网格布局管理器,按照从左到右,从上到下 的 “行*列”布局一次排列public void GirdLayoutDemo(){setTitle("网格布局");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,450,300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);//contentPane.setLayout(new GridLayout());// 设置网格布局,默认是单行多列contentPane.setLayout(new GridLayout(3, 3, 10, 10));// 设置网格布局 3行3列for (int i = 0; i < 8; i++) {JButton btn = new JButton("btn"+(i+1));contentPane.add(btn);}}}


0 1