我正在尝试创建一个具有可互换外观组件的自定义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
将从域对象侦听属性更改事件,并相应地更新它的显示。
答案 0 :(得分:2)
按钮的边距和边框在JSkinnableButton
中占用了空间。
protected JSkinnableButton(){
super();
setLayout(new CardLayout()); // for interchangeability
setMargin(new Insets(0,0,0,0));
setBorder(BorderFactory.createEmptyBorder());
add(new JSkinComponent(), "Skin");
}
现在,特别是边框,是按钮看起来像按钮的一部分,但我想你已经有了一个计划......