主要記錄Java Swing編程,JFrame控制的使用以及內容面板(JPanel)的常用布局的使用。
Java Swing中的透明窗體(Windows中消息提示框的制作) http://www.linuxidc.com/Linux/2014-04/99622.htm
用Java Swing編寫簡單的測試小工具界面(源碼) http://www.linuxidc.com/Linux/2012-11/73566.htm
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 exception
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
MyJFrame frame = new MyJFrame();
frame.setVisible(true);
// frame.AbsoluteLayoutDemo();
// frame.FlowLayoutDemo();
// frame.BorderLayoutDemo();
frame.GirdLayoutDemo();
} catch (Exception e) {
// TODO: handle exception
e.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);// 設置布局管理器為null
JButton 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);
}
}
}
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-05/101049p2.htm