Linux教程網
Java實現一種簡單風格的按鈕:
- package com.han;
-
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.Graphics;
-
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.plaf.metal.MetalButtonUI;
- import javax.swing.AbstractButton;
-
-
- /**
- * 實現一種簡單風格的按鈕
- * @author HAN
- *
- */
- public class ButtonSimpleCustomized extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 4369682587606626801L;
-
- public ButtonSimpleCustomized() {
- Container container = getContentPane();
- container.setLayout(new FlowLayout());
- JButton buttonSimple = new JButton("buttonSimple");
- Color bgColor = container.getBackground();
- System.out.println(bgColor);
- int redBgColor = bgColor.getRed();
- int greenBgColor = bgColor.getGreen();
- int blueBgColor = bgColor.getBlue();
- System.out.println(redBgColor);
- System.out.println(greenBgColor);
- System.out.println(blueBgColor);
- Color colorNewOrganized = new Color(redBgColor, greenBgColor, blueBgColor);
- System.out.println(colorNewOrganized);
- System.out.println(Color.yellow);
- // buttonSimple.setBackground(Color.yellow);
- // buttonSimple.setBackground(bgColor);
- buttonSimple.setBackground(colorNewOrganized);
- buttonSimple.setBorderPainted(false);
- buttonSimple.setFocusPainted(false);
- MetalButtonUI ui = new MetalButtonUI() {
- protected void paintButtonPressed(Graphics g, AbstractButton b) {
- if ( b.isContentAreaFilled() ) {
- // Dimension size = b.getSize();
- g.setColor(getSelectColor());
- // g.fillRect(0, 0, size.width, size.height);
- }
- }
- };
- buttonSimple.setUI(ui);
- container.add(buttonSimple);
- }
-
- public static void main(String[] args) {
- ButtonSimpleCustomized frame = new ButtonSimpleCustomized();
- frame.setTitle("buttonSimple Test");
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setBounds(0, 0, 300, 100);
- }
-
- }
Copyright ©
Linux教程網 All Rights Reserved