Swing是一個為Java設計的GUI工具包。
Swing是JAVA基礎類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平台運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統默認的特有主題),然而不是真的使用原生平台提供的設備,而是僅僅在表面上模仿它們。這意味著你可以在任意平台上使用JAVA支持的任意麵板。輕量級組件的缺點則是執行速度較慢,優點就是可以在所有平台上採用統一的行為。
下面看看實現按鈕的簡單實例:
import java.awt.*;import java.awt.geom.*;import javax.swing.*;class RButton extends JButton {public RButton(String label) {super(label);// 這些聲明把按鈕擴展為一個圓而不是一個橢圓。 Dimension size = getPreferredSize();size.width = size.height = Math.max(size.width, size.height);setPreferredSize(size);// 這個調用使JButton不畫背景,而允許我們畫一個圓的背景。 setContentAreaFilled(false);this.setBackground(Color.GRAY);}// 畫圓的背景和標籤protected void paintComponent(Graphics g) {if (getModel().isArmed()) {// 你可以選一個高亮的顏色作為圓形按鈕類的屬性g.setColor(Color.lightGray);} else {g.setColor(getBackground());}g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);// 這個調用會畫一個標籤和焦點矩形。 super.paintComponent(g);}// 用簡單的弧畫按鈕的邊界。 protected void paintBorder(Graphics g) {g.setColor(getForeground());g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);}// 偵測點擊事件Shape shape;public boolean contains(int x, int y) {// 如果按鈕改變大小,產生一個新的形狀對象。 if (shape == null || !shape.getBounds().equals(getBounds())) {shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());}return shape.contains(x, y);}// 測試程序public static void main(String[] args) {// 產生一個帶'Jackpot'標籤的按鈕。 JButton button = new RButton("Jackpot");ImageIcon ic = new ImageIcon("E://clientForMssql//Icons//item_group.gif");JButton button2 = new JButton(ic);button.setBackground(Color.GRAY);// 產生一個框架以顯示這個按鈕。 JFrame frame = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// frame.getContentPane().setBackground(Color.GRAY);frame.getContentPane().add(button);frame.getContentPane().add(button2);frame.getContentPane().setLayout(new FlowLayout());frame.setSize(200, 200);frame.setVisible(true);}}結果:
接著再分享一則實現簡單點擊事件的按鈕Java示例。
這是一個關於製作圓形Swing按鈕的技巧。事實上,這個技巧中的知識方便的適用於任何形狀的按鈕,但我們只作一個圓形的按鈕。當你製作一個圓形的按鈕時,需要做兩件事。第一件事是重載一個適當的繪畫方法以畫出一個圓形。第二件事是設置一些事件使得只有當你點擊圓形按鈕的範圍中的時侯按鈕才會作出響應(不是包含圓形按鈕的矩形的範圍中)。
import java.awt.*;import java.awt.geom.*;import javax.swing.*;public class RoundButton extends JButton {public RoundButton(String label) {super(label);// 這些聲明把按鈕擴展為一個圓而不是一個橢圓。 Dimension size = getPreferredSize();size.width = size.height = Math.max(size.width, size.height);setPreferredSize(size);// 這個調用使JButton不畫背景,而允許我們畫一個圓的背景。 setContentAreaFilled(false);}// 畫圓的背景和標籤protected void paintComponent(Graphics g) {if (getModel().isArmed()) {// 你可以選一個高亮的顏色作為圓形按鈕類的屬性g.setColor(Color.lightGray);} else {g.setColor(getBackground());}g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);// 這個調用會畫一個標籤和焦點矩形。 super.paintComponent(g);}// 用簡單的弧畫按鈕的邊界。 protected void paintBorder(Graphics g) {g.setColor(getForeground());g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);}// 偵測點擊事件Shape shape;public boolean contains(int x, int y) {// 如果按鈕改變大小,產生一個新的形狀對象。 if (shape == null || !shape.getBounds().equals(getBounds())) {shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());}return shape.contains(x, y);}// 測試程序public static void main(String[] args) {// 產生一個帶'Jackpot'標籤的按鈕。 JButton button = new RoundButton("Jackpot");button.setBackground(Color.green);// 產生一個框架以顯示這個按鈕。 JFrame frame = new JFrame();frame.getContentPane().setBackground(Color.yellow);frame.getContentPane().add(button);frame.getContentPane().setLayout(new FlowLayout());frame.setSize(150, 150);frame.setVisible(true);}}結果:
點擊事件:
由於我們想保留JButton的大部分功能,我們讓RoundButton類繼承了JButton類。在RoundButton的構造方法中,setContentAreaFilled()方法被調用。這就讓按鈕畫了一個矩形的焦點區,但不畫背景。
現在我們需要畫一個圓的背景。這是通過重載paintComponent()方法實現的。那個方法使用Graphics.fillOval()方法畫一個實心的圓。然後paintComponent()方法調用super.paintComponent()在這個實心圓的上面畫了一個標籤。
這個例子還重載了paintBorder()方法以在圓形按鈕的邊界上畫一個邊。如果你不想要邊框,你也可以不重載這個方法。這個方法調用了Graphics.drawOval()方法以在圓的邊界上畫一個細的邊框。
注意:在JDKTM1.2.2中,當你將鼠標拖進或拖出按鈕的範圍時,JButton的行為有一個小BUG。理論上,當你在圓形按鈕上點擊鼠標然後拖動鼠標離開按鈕的邊界時,按鈕應該改變它的外形。當你拖動鼠標進入按鈕的邊界內時,按鈕應回复它的外形。不幸的是,包含這個行為的代碼不能調用contains()方法。代替它的是只使用按鈕的'限制範圍'(這是包含按鈕的最小矩形範圍)注意,如果你在圓形邊界內輕微的拖動鼠標,也就是說離開圓形的範圍但不離開邊界,按鈕將不會改變它的外形。
總結
以上就是本文關於Java編程實現swing圓形按鈕實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!