编程语言
首页 > 编程语言> > java – JButton的可互换JComponent外观

java – JButton的可互换JComponent外观

作者:互联网

我正在尝试创建一个具有可互换外观组件的自定义JButton.使用CardLayout作为切换机制,我很难将JComponent(即皮肤组件)放置在JButton上.

例如,

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public final class SkinsDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(new JSkinnableButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JSkinnableButton extends JButton{
        private static final long serialVersionUID = -5167346969674067012L;

        protected JSkinnableButton(){
            super();
            setLayout(new CardLayout()); // for interchangeability 
            add(new JSkinComponent(), "Skin");
        }
    }

    private static final class JSkinComponent extends JComponent{
        private static final long serialVersionUID = 2172542865655802012L;

        protected JSkinComponent(){
            super();
            setOpaque(true);
            setLayout(new FlowLayout()); // need layout manager
            setBackground(Color.CYAN);
            add(new JLabel("Skin"));
        }

        @Override
        protected void paintComponent(Graphics g){
            Graphics gCopy = g.create();
            gCopy.setColor(getBackground());
            gCopy.fillRect(0, 0, getWidth(), getHeight());
            gCopy.dispose();
        }
    }
}

这是一个非常粗糙的例子,但我认为这清楚地传达了我的意图.

这个JButton将从域对象中侦听属性更改事件,并相应地更新它的显示.

解决方法:

按钮的边距和边框在你的JSkinnableButton中占用了空间.

protected JSkinnableButton(){
    super();
    setLayout(new CardLayout()); // for interchangeability
    setMargin(new Insets(0,0,0,0));
    setBorder(BorderFactory.createEmptyBorder());
    add(new JSkinComponent(), "Skin");
}

现在,特别是边界,是按钮看起来像按钮的一部分,但我想你已经有了一个计划……

标签:java,swing,jbutton,jcomponent,skinning
来源: https://codeday.me/bug/20190704/1378543.html