使用paintComponent()方法繪制的各種Button:
正常狀態:
獲得焦點狀態:
被按下狀態:
被釋放狀態:
實現代碼:
- package com.han;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.RadialGradientPaint;
- import java.awt.RenderingHints;
- import java.awt.Toolkit;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.font.LineMetrics;
- import java.awt.geom.Ellipse2D;
- import java.awt.geom.Point2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.geom.RoundRectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.UIManager;
- import javax.swing.UIManager.LookAndFeelInfo;
- import javax.swing.UnsupportedLookAndFeelException;
- @SuppressWarnings("serial")
- publicclass JButton_Bg extends JFrame {
- public JButton_Bg() {
- for (LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
- if (laf.getName().equals("Nimbus")) {
- try {
- UIManager.setLookAndFeel(laf.getClassName());
- } catch (ClassNotFoundException | InstantiationException
- | IllegalAccessException
- | UnsupportedLookAndFeelException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- // TODO Auto-generated constructor stub
- Container c = getContentPane();
- c.setLayout(new FlowLayout());
- final JButton button = new MyButton("button 2");
- c.add(button);
- JButton button2 = new JButton("button 2");
- c.add(button2);
- button2.setBackground(Color.blue);
- JButton button3 = new MyButton2("Cancel");
- c.add(button3);
- // 完全重繪的Button,其Text的HTML設置特性消失
- // JButton button4 = new
- // MyButton3("<html><font size=12>Sub</font></html>");
- JButton button4 = new MyButton3("Sub");
- // button4.setFont(new Font("Serif", Font.PLAIN, 14));
- c.add(button4);
- }
- privateclass MyButton extends JButton {
- private String text;
- private String state = "normal";
- // private String state = "focused";
- // private String state = "pressed";
- // private String state = "released";
- // 無參構造繼承時自動調用,而有參構造繼承時則需手動重寫
- MyButton(String text) {
- // super("<html><font size=5>" + text + "</font></html>");
- super(text);
- this.text = text;
- // 下 面的代碼塊若是放到下面的paintComponent()方法裡則Swing界面初始化時,
- // 布局管理器還是采用的是系統默認的PreferredSize。因為構造函數要優先於
- // paintComponent()方法執行。
- Dimension preferredSize = getPreferredSize();
- Dimension preferredSizeNew = new Dimension(preferredSize.width,
- preferredSize.width);
- setPreferredSize(preferredSizeNew);
- }
- @Override
- protectedvoid paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- int width = this.getPreferredSize().width;
- int height = this.getPreferredSize().height;
- // BufferedImage img;
- // try {
- // img = ImageIO.read(this.getClass().getResource(
- // "/images/icon.jpg"));
- // g2.drawImage(img, 0, 0, width, height, this);
- // } catch (IOException e1) {
- // // TODO Auto-generated catch block
- // e1.printStackTrace();
- // }
- if (state.equals("normal")) {
- // draw background pattern
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = height / 2;
- float[] dist = { 0.0f, 1.0f };
- Color[] colors = { new Color(0, 0, 0, 255),
- new Color(255, 255, 255, 0) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new Ellipse2D.Double(width / 2 - height / 2, 0, height,
- height));
- // draw string text
- g2.setColor(Color.RED);
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("focused")) {
- // draw background pattern
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = height / 2;
- float[] dist = { 0.2f, 1.0f };
- Color[] colors = { new Color(0, 0, 0, 255),
- new Color(255, 255, 255, 0) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new Ellipse2D.Double(width / 2 - height / 2, 0, height,
- height));
- // draw string text
- g2.setColor(Color.RED);
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("pressed")) {
- // draw background pattern
- int offsetCenter = 1;
- Point2D center = new Point2D.Float(width / 2 + offsetCenter,
- height / 2 + offsetCenter);
- float radius = height / 2;
- float[] dist = { 0.2f, 1.0f };
- Color[] colors = { new Color(0, 0, 0, 255),
- new Color(255, 255, 255, 0) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new Ellipse2D.Double(width / 2 - height / 2
- + offsetCenter, offsetCenter, height, height));
- // draw string text
- g2.setColor(Color.RED);
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2)
- + offsetCenter,
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent()))
- + offsetCenter);
- } elseif (state.equals("released")) {
- // draw background pattern
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = height / 2;
- float[] dist = { 0.2f, 1.0f };
- Color[] colors = { new Color(0, 0, 0, 255),
- new Color(255, 255, 255, 0) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new Ellipse2D.Double(width / 2 - height / 2, 0, height,
- height));
- // draw string text
- g2.setColor(Color.RED);
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- }
- addMouseListener(new MouseAdapter() {
- @Override
- publicvoid mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.println("光標移入組件");
- state = "focused";
- repaint();
- }
- @Override
- publicvoid mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.println("光標移出組件");
- state = "normal";
- repaint();
- }
- @Override
- publicvoid mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.print("鼠標按鍵被按下,");
- state = "pressed";
- repaint();
- }
- @Override
- publicvoid mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.print("鼠標按鍵被釋放,");
- state = "released";
- repaint();
- }
- });
- }
- }
- privateclass MyButton2 extends JButton {
- private String text;
- private String state = "normal";
- // private String state = "focused";
- // private String state = "pressed";
- // private String state = "released";
- // Initialize the size of the button according to the length and width
- // of the text string
- MyButton2(String text) {
- super(text);
- this.text = text;
- }
- @Override
- protectedvoid paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- int width = this.getWidth();
- int height = this.getHeight();
- if (state.equals("normal")) {
- // draw background pattern
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 0.8f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(0, 0, 0, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("focused")) {
- // draw background pattern
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 0.8f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(20, 20, 20, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("pressed")) {
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 1.0f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(20, 20, 20, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("released")) {
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 0.8f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(20, 20, 20, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- }
- addMouseListener(new MouseAdapter() {
- @Override
- publicvoid mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.println("光標移入組件");
- state = "focused";
- repaint();
- }
- @Override
- publicvoid mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.println("光標移出組件");
- state = "normal";
- repaint();
- }
- @Override
- publicvoid mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.print("鼠標按鍵被按下,");
- state = "pressed";
- repaint();
- }
- @Override
- publicvoid mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.print("鼠標按鍵被釋放,");
- state = "released";
- repaint();
- }
- });
- }
- }
- privateclass MyButton3 extends JButton {
- private String text;
- private String state = "normal";
- // private String state = "focused";
- // private String state = "pressed";
- // private String state = "released";
- // Initialize the size of the button according to the length and width
- // of the text string
- MyButton3(String text) {
- super(text);
- this.text = text;
- }
- @Override
- protectedvoid paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- int width = this.getWidth();
- int height = this.getHeight();
- if (state.equals("normal")) {
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 0.8f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(0, 0, 0, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("focused")) {
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 0.8f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(20, 20, 20, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("pressed")) {
- g2.setColor(new Color(0, 147, 255));
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 1.0f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(20, 20, 20, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- double borderWidth = 2.0f; // 2 pixels
- g2.fill(new RoundRectangle2D.Double(borderWidth, borderWidth,
- width - borderWidth * 2.0f,
- height - borderWidth * 2.0f, height - borderWidth
- * 2.0f, height - borderWidth * 2.0f));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- } elseif (state.equals("released")) {
- Point2D center = new Point2D.Float(width / 2, height / 2);
- float radius = width / 2;
- float[] dist = { 0.0f, 0.8f };
- Color[] colors = { new Color(255, 255, 255, 0),
- new Color(20, 20, 20, 255) };
- RadialGradientPaint paint = new RadialGradientPaint(center,
- radius, dist, colors);
- g2.setPaint(paint);
- g2.fill(new RoundRectangle2D.Double(0, 0, width, height,
- height, height));
- // draw string text
- Font defaultFont = getFont();
- g2.setFont(defaultFont);
- g2.setColor(Color.BLACK);
- Rectangle2D rect = defaultFont.getStringBounds(text,
- g2.getFontRenderContext());
- LineMetrics lineMetrics = defaultFont.getLineMetrics(text,
- g2.getFontRenderContext());
- g2.drawString(
- text,
- (float) (width / 2 - rect.getWidth() / 2),
- (float) ((height / 2) + ((lineMetrics.getAscent() + lineMetrics
- .getDescent()) / 2 - lineMetrics.getDescent())));
- }
- addMouseListener(new MouseAdapter() {
- @Override
- publicvoid mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.println("光標移入組件");
- state = "focused";
- repaint();
- }
- @Override
- publicvoid mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.println("光標移出組件");
- state = "normal";
- repaint();
- }
- @Override
- publicvoid mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.print("鼠標按鍵被按下,");
- state = "pressed";
- repaint();
- }
- @Override
- publicvoid mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- System.out.print("鼠標按鍵被釋放,");
- state = "released";
- repaint();
- }
- });
- }
- }
- /**
- * @param args
- */
- publicstaticvoid main(String[] args) {
- // TODO Auto-generated method stub
- JButton_Bg frame = new JButton_Bg();
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }