java swing 组件技术(上)
2019獨角獸企業重金招聘Python工程師標準>>>
? ? 第一次寫自己的博客,明知道寫得很水,還是努力去寫了,希望踏出第一步之后,能有著更好的發展。這幾天看了一些資料,總結了一下自己所學的關于java swing的知識。
一、??????? java swing 組件技術簡介
1.swing的特性
l? 是AWT的擴展,swing比AWT提供了更多的組件和外觀
l? 它是純java代碼編寫(除了JFrame、JDialog、JApplet),因此與平臺無關
2.AWT的缺點
????? 組件少而且是本地代碼實現(無法實現跨平臺)
3.swing 和 AWT相比
????? 一個重要的的改進在于swing把一個組件的處理分為圖形部分和數據處理部分(MVC模型)
l? 圖形部分由編程環境統一處理(View)
l? 數據部分由一個數據處理模型進行處理(Model)
4.swing編程應注意的問題
l? swing和AWT組件不要混合使用,可能會導致不能正常顯示的錯誤
l? 正確理解輕量級組件與重量級組件的不同
輕量級組件——swing組件,由純java代碼實現,占用系統資源少
重量級組件——AWT組件,有本地c代碼實現,占用系統資源多
(注:JFrame、JDialog、JApplet(頂層容器)是重量級組件)
?
二、??????? swing的“外觀和感覺”(LookAndFeel)
1.swing中常見的“LookAndFeel”
l? Metal風格 (默認)
?javax.swing.plaf.metal.MetalLookAndFeel
l? Windows風格
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
l? Windows Classic風格
com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
l? Motif風格
com.sun.java.swing.plaf.motif.MotifLookAndFeel
l? Mac風格 (需要在相關的操作系統上方可實現)
com.sun.java.swing.plaf.mac.MacLookAndFeel
l? GTK風格 (需要在相關的操作系統上方可實現)
com.sun.java.swing.plaf.gtk.GTKLookAndFeel
l? 可跨平臺的默認風格
UIManager.getCrossPlatformLookAndFeelClassName()
l? 當前系統的風格
UIManager.getSystemLookAndFeelClassName()
意義:通過LookAndFeel機制,我們可以是程序的設計者任意轉換程序的人機界面來對應不同的操作系統
????? 2.設置swing的LookAndFeel
????? 在swing中,采用UIManager類來管理swing界面的LookAndFeel,UIManager類提供靜態方法setLookAndFeel()來設置界面的LookAndFeel,該方法是一個重載方法,提供兩個重載方式:
????? setLookAndFeel(LookAndFeel newLookAndFeel)
????? setLookAndFeel( String className)
其中:參數newLookAndFeel表示組件的某種外觀,className表示組件某種外觀的名字
????? 2-1.設置方法(靜態設置,動態設置)
靜態設置——設計時指定LookAndFeel
eg1:
String lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
eg2:
String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
?
動態設置——運行時指定LookAndFeel
eg:
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
//運行時動態更新外觀
SwingUtilities.updateComponentTreeUI(this)
附:SwingUtilities.updateComponentTreeUI(Component c)
??? 對組件c重新設置外觀
注:由于JFrame、JDialog、JApplet(頂層容器)為重量級組件,因此他們的外觀只與操作系統平臺有關系,在相同的操作系統平臺下表現相同的外觀
2-2.程序代碼
import java.awt.*; import java.awt.event.*;import javax.swing.*;public class LookAndFeelDemo2 extends JFrame {private JRadioButton radio1 = new JRadioButton("Metal");private JRadioButton radio2 = new JRadioButton("Windows");private JRadioButton radio3 = new JRadioButton("Motif");private JPanel panelNorth = new JPanel();private JPanel panelSouth = new JPanel();private JTextArea area = new JTextArea(6, 20);private JButton btn = new JButton("button");private JCheckBox chk = new JCheckBox("checkBox");private JComboBox cmbLookAndFeel = new JComboBox(new String[] { "Metal","Widows", "Motif", "GTK" });public LookAndFeelDemo2(String title) {super(title);Container contentPane = this.getContentPane();ButtonGroup group = new ButtonGroup();group.add(radio1);group.add(radio2);group.add(radio3);panelNorth.add(radio1);panelNorth.add(radio2);panelNorth.add(radio3);panelSouth.add(btn);panelSouth.add(chk);panelSouth.add(cmbLookAndFeel);contentPane.add(panelNorth, BorderLayout.NORTH);contentPane.add(area, BorderLayout.CENTER);contentPane.add(panelSouth, BorderLayout.SOUTH);pack();setVisible(true);setSize(300, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 監聽Item事件radio1.addItemListener(new MyItemListener());radio2.addItemListener(new MyItemListener());radio3.addItemListener(new MyItemListener());}private class MyItemListener implements ItemListener {public void itemStateChanged(ItemEvent e) {// 取得點擊按鈕的名字String itemName = ((JRadioButton) e.getSource()).getText();changeLookAndFeel(itemName);}}// 設置外觀的private類型方法private void changeLookAndFeel(String name) {String lookAndFeel = "";if (name.equals("Metal")) {lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";} else if (name.equals("Windows")) {lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";} else if (name.equals("Motif")) {lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";}else{//取得默認的metal外觀lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";}try {UIManager.setLookAndFeel(lookAndFeel);// 運行時指定LookAndFeel,需要SwingUtilities.updateComponentTreeUI(Component// c)實現動態的更新SwingUtilities.updateComponentTreeUI(this);area.setText("當前外觀類名:\n" + lookAndFeel);} catch (Exception ex) {ex.printStackTrace();}}public static void main(String[] args) {new LookAndFeelDemo2("LookAndFeel");} }
三、??????? swing組件
1.swing組件的分類
2.往swing容器里添加組件
????? 對于swing頂層容器(JFrame,JDialog,JApplet),在添加組件時,不能直接調用容器的add()方法。
????? 往頂層容器添加組件可用以下方法:
a)???? 通過getContentPane()方法獲得當前容器的內容面板對象,在調用容器的add()方法加入各個組件
b)???? 先利用JPanel類生成一個內容面板對象panel,再將各個組件加入到panel中,然后調用的容器的setContentPane()方法,將panel加入到當前容器中
3.使用swing組件的基本規則
a)???? 把swing組件放入一個頂層容器中
b)???? 避免使用非swing的重量級組件
c)???? 往swing頂層容器添加組件時,不能直接調用add()方法
d)???? 內容面板缺省的布局策略是BorderLayout,不能對頂層容器進行布局
?
四.swing的事件處理
a)???? Swing組件可以產生AWT包中的事件
b)???? 有自己的事件包(javax.swing.event)
c)???? Component類的五種事件
d)???? 能激活Container類的ContainerEvent事件的Swing組件
Swing組件中的容器(如:JFrame, JPane)
復合組件(如:JComboBox)
轉載于:https://my.oschina.net/jingxintianya/blog/137443
總結
以上是生活随笔為你收集整理的java swing 组件技术(上)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入浅出 REST
- 下一篇: cisco 两个设备之间测试吞吐量的一个