Swing 是一個爲Java設計的GUI工具包。
Swing是JAVA基礎類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平臺運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統默認的特有主題),然而不是真的使用原生平臺提供的設備,而是僅僅在表面上模仿它們。這意味着你可以在任意平臺上使用JAVA支持的任意麪板。輕量級組件的缺點則是執行速度較慢,優點就是可以在所有平臺上採用統一的行爲。
1:性能不錯,因爲其底子Java2D還不錯,不是極其特別海量級應用基本都可勝任
2:bug並不是想象那麼多,這些年sun已修復很多,即使有也很容易到找workaround
3:組件市場談不上豐富,但該有的也都有了jidesoft,infonode,jfreechart,swinglabs等可以參考
4:大型的應用問題不在於Swing,選用任何其他GUI也都得考慮分業務,分模塊加載等問題,這方面NetBeansRCP和EclipseRCP都可以參考,我建議先不考慮引入RCP增加學習指數(包括引入更多風險),如果實在必要需用NetBeansRCP比較舒服,Eclipse的RCP需要考慮Swing和SWT的整合有不少問題恭候着你
Swing的缺點:
1:其MVC的設計框架還是很穩健,使的Swing這麼十幾年屹立不倒,不過還是顯示出了老態和缺陷,這個話題很大可以寫好幾篇文章,簡單說Swing的設計不如Flex的MVP,不如Silverlight/WPF的MVVM易於讓程序員編寫,測試包括IDE的提供
2:Swing的lookandfeel是很好的思想,但沒有設計好,這麼多年雖然也有不好官方,開源和商業的LAF出現,但除了官方常用的那幾個雖然醜但沒有太多bug還算能用,其他我基本不用除非應用不復雜
3:Swing有很多layout初衷很好,但同樣問題是衆多實現者中沒有幾款簡單易用的,我自己寫了個佈局這些年我自己一直用自己的這個佈局,公司很多其他人也是熟悉哪款可能一輩子就用它了,因爲沒時間去折騰研究其他款
4:組件的確不是相當豐富完善,一個DateChooser到現在都沒個官方的,JDK7只是maybe,確定的是在JDK8提供,當然這不嚴重前面提到你可以很容易找到其他開源或商業的組件解決,不過也能反映點問題
最後談點個人經驗,我們公司是重度Swing使用者,都是手工寫界面代碼,如果大型項目自然值得好好投入去積累組件和封裝,有了好的封裝的組件基本手工代碼還是非常簡單易讀方便維護,另外就像第一點提到Java2D真的不錯,如果你覺得那些地方實在太難用了,實在太差醜了,或者實在性能太差了,你完全可以隨便擴展個component自己去paint完全用Java2D重新個你滿意的組件,有點經驗後你會發現寫個組件不是那麼難,而且還挺有趣。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
/**
* swing基礎實例
* @author HZ20232
*
*/
public class Hello{
public static void main(String args[])throws Exception{
NewFrame frame1 = new NewFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//一定要設置關閉
frame1.setVisible(true);
}
}
class NewFrame extends JFrame{
private JLabel label1;
private JButton button1;
private JTextField text1;
private JComboBox box;
private JMenuBar menuBar;
private JSlider slider;
private JSpinner spinner;
private JToolBar toolBar;
public NewFrame(){
super();
this.setSize(300,500);
this.getContentPane().setLayout(null);//設置佈局控制器
// this.getContentPane().setLayout(new FlowLayout());//設置佈局控制器
// this.getContentPane().setLayout(new GridLayout(1,2));//設置佈局控制器,需要給出設定的行列數目
// this.getContentPane().setLayout(new BorderLayout());//設置佈局控制器,以North,South,West,East,來控制控件佈局
// this.getContentPane().setLayout(new GridBagLayout());//設置佈局控制器
this.add(this.getTextField(),null);//添加文本框
this.add(this.getButton(),null);//添加按鈕
this.add(this.getLabel(),null);//添加標籤
this.add(this.getBox(),null);//添加下拉列表框
this.setJMenuBar(this.getMenu());//添加菜單
this.add(this.getSlider(),null);
this.add(this.getSpinner(),null);
this.add(this.getToolBar(),null);
this.setTitle("Hello World!");//設置窗口標題
}
private JToolBar getToolBar(){
if(toolBar==null){
toolBar = new JToolBar();
toolBar.setBounds(103,260,71,20);
toolBar.setFloatable(true);
}
return toolBar;
}
private JSpinner getSpinner(){
if(spinner==null){
spinner = new JSpinner();
spinner.setBounds(103,220, 80,20);
spinner.setValue(100);
}
return spinner;
}
private JSlider getSlider(){
if(slider==null){
slider = new JSlider();
slider.setBounds(103,200,100, 20);
slider.setMaximum(100);
slider.setMinimum(0);
slider.setOrientation(0);
slider.setValue(0);
}
return slider;
}
/**
* 菜單的級別JMenuBar->JMenu->JMenuItem
* 三級都是1:n的關係
* 最後添加菜單用的SetJMenuBar方法
* @return 建立好的菜單
*/
private JMenuBar getMenu(){
if(menuBar==null){
menuBar = new JMenuBar();
JMenu m1 = new JMenu();
m1.setText("文件");
JMenu m2 = new JMenu();
m2.setText("編輯");
JMenu m3 = new JMenu();
m3.setText("幫助");
JMenuItem item11 = new JMenuItem();
item11.setText("打開");
JMenuItem item12 = new JMenuItem();
item12.setText("保存");
JMenuItem item13 = new JMenuItem();
item13.setText("退出");
JMenuItem item21 = new JMenuItem();
item21.setText("複製");
JMenuItem item22 = new JMenuItem();
item22.setText("拷貝");
JMenuItem item23 = new JMenuItem();
item23.setText("剪切");
JMenuItem item31 = new JMenuItem();
item31.setText("歡迎");
JMenuItem item32 = new JMenuItem();
item32.setText("搜索");
JMenuItem item33 = new JMenuItem();
item33.setText("版本信息");
m1.add(item11);
m1.add(item12);
m1.add(item13);
m2.add(item21);
m2.add(item22);
m2.add(item23);
m3.add(item31);
m3.add(item32);
m3.add(item33);
menuBar.add(m1);
menuBar.add(m2);
menuBar.add(m3);
}
return menuBar;
}
/**
* 設置下拉列表框
* @return
*/
private JComboBox getBox(){
if(box==null){
box = new JComboBox();
box.setBounds(103,140,71,27);
box.addItem("1");
box.addItem("2");
box.addActionListener(new comboxListener());//爲下拉列表框添加監聽器類
}
return box;
}
private class comboxListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Object o = e.getSource();
System.out.println(o.toString());
}
}
/**
* 設置標籤
* @return 設置好的標籤
*/
private JLabel getLabel(){
if(label1==null){
label1 = new JLabel();
label1.setBounds(34,49,53,18);
label1.setText("Name");
label1.setToolTipText("JLabel");
}
return label1;
}
/**
* 設置按鈕
* @return 設置好的按鈕
*/
private JButton getButton(){
if(button1==null){
button1 = new JButton();
button1.setBounds(103,110,71,27);
button1.setText("OK");
button1.setToolTipText("OK");
button1.addActionListener(new HelloButton());//添加監聽器類,其主要的響應都由監聽器類的方法實現
}
return button1;
}
/**
* 監聽器類實現ActionListener接口,主要實現actionPerformed方法
* @author HZ20232
*
*/
private class HelloButton implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("Hello world!");
}
}
/**
* 設定文本域
* @return
*/
private JTextField getTextField(){
if(text1==null){
text1 = new JTextField();
text1.setBounds(96,49,160,20);
}
return text1;
}
}