Java 在Swing应用程序中扩展系统外观
我想为Swing应用程序使用系统外观。所以我使用了Java 在Swing应用程序中扩展系统外观,java,swing,jpanel,jbutton,look-and-feel,Java,Swing,Jpanel,Jbutton,Look And Feel,我想为Swing应用程序使用系统外观。所以我使用了getSystemLookAndFeelClassName()方法,该方法工作得非常好 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 但是,现在我想更改应用程序的所有JButtons(在我所有的JFrames,JDialogs,JOptionPanes,jfilechooser,等等)并且只更改JButtons。因此,我想知道如何扩展系统的外观,使其适用
getSystemLookAndFeelClassName()
方法,该方法工作得非常好
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
但是,现在我想更改应用程序的所有JButtons
(在我所有的JFrames
,JDialogs
,JOptionPanes
,jfilechooser
,等等)并且只更改JButtons
。因此,我想知道如何扩展系统的外观,使其适用于所有组件,除了JButtons
(以及JPanels
,我想要灰色背景)
谢谢。您也可以创建自己的按钮类,例如CustomButton,它扩展了JButton,进行所有更改并使用它代替标准JButton。通过这种方式,您可以同时使用自定义按钮和标准按钮—如果您改变主意并希望在某处添加标准JButton;) 您还可以创建自己的按钮类,例如CustomButton,它扩展了JButton,进行所有更改并使用它来代替标准的JButton。通过这种方式,您可以同时使用自定义按钮和标准按钮—如果您改变主意并希望在某处添加标准JButton;) 最后,我扩展了系统的外观和感觉,如下所示:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.put("ButtonUI", "com.my.package.MyButtonUI");
UIManager.put("Panel.background", Color.green);
使用MyButtonUI
:
public class MyButtonUI extends BasicButtonUI {
public static final int BUTTON_HEIGHT = 24;
private static final MyButtonUI INSTANCE = new MyButtonUI ();
public static ComponentUI createUI(JComponent b) {
return INSTANCE;
}
@Override
public void paint(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton) c;
Graphics2D g2d = (Graphics2D) g;
final int buttonWidth = button.getWidth();
if (button.getModel().isRollover()) {
// Rollover
GradientPaint gp = new GradientPaint(0, 0, Color.green, 0, BUTTON_HEIGHT * 0.6f, Color.red, true);
g2d.setPaint(gp);
} else if (button.isEnabled()) {
// Enabled
GradientPaint gp = new GradientPaint(0, 0, Color.red, 0, BUTTON_HEIGHT * 0.6f, Color.gray, true);
g2d.setPaint(gp);
} else {
// Disabled
GradientPaint gp = new GradientPaint(0, 0, Color.black, 0, BUTTON_HEIGHT * 0.6f, Color.blue, true);
g2d.setPaint(gp);
}
g2d.fillRect(0, 0, buttonWidth, BUTTON_HEIGHT);
super.paint(g, button);
}
@Override
public void update(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton) c;
if (isInToolBar(button)) {
// Toolbar button
button.setOpaque(false);
super.paint(g, button);
} else if (button.isOpaque()) {
// Other opaque button
button.setRolloverEnabled(true);
button.setForeground(Color.white);
paint(g, button);
} else {
// Other non-opaque button
super.paint(g, button);
}
}
private boolean isInToolBar(AbstractButton button) {
return SwingUtilities.getAncestorOfClass(JToolBar.class, button) != null;
}
}
注意:我强烈推荐这个链接:(来自Robin)
谢谢。最后,我扩展了系统的外观和感觉,如下所示:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.put("ButtonUI", "com.my.package.MyButtonUI");
UIManager.put("Panel.background", Color.green);
使用MyButtonUI
:
public class MyButtonUI extends BasicButtonUI {
public static final int BUTTON_HEIGHT = 24;
private static final MyButtonUI INSTANCE = new MyButtonUI ();
public static ComponentUI createUI(JComponent b) {
return INSTANCE;
}
@Override
public void paint(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton) c;
Graphics2D g2d = (Graphics2D) g;
final int buttonWidth = button.getWidth();
if (button.getModel().isRollover()) {
// Rollover
GradientPaint gp = new GradientPaint(0, 0, Color.green, 0, BUTTON_HEIGHT * 0.6f, Color.red, true);
g2d.setPaint(gp);
} else if (button.isEnabled()) {
// Enabled
GradientPaint gp = new GradientPaint(0, 0, Color.red, 0, BUTTON_HEIGHT * 0.6f, Color.gray, true);
g2d.setPaint(gp);
} else {
// Disabled
GradientPaint gp = new GradientPaint(0, 0, Color.black, 0, BUTTON_HEIGHT * 0.6f, Color.blue, true);
g2d.setPaint(gp);
}
g2d.fillRect(0, 0, buttonWidth, BUTTON_HEIGHT);
super.paint(g, button);
}
@Override
public void update(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton) c;
if (isInToolBar(button)) {
// Toolbar button
button.setOpaque(false);
super.paint(g, button);
} else if (button.isOpaque()) {
// Other opaque button
button.setRolloverEnabled(true);
button.setForeground(Color.white);
paint(g, button);
} else {
// Other non-opaque button
super.paint(g, button);
}
}
private boolean isInToolBar(AbstractButton button) {
return SwingUtilities.getAncestorOfClass(JToolBar.class, button) != null;
}
}
注意:我强烈推荐这个链接:(来自Robin)
谢谢。另见引文。请尽量集中你的问题;A会有帮助的。谢谢你的指点。过几个小时我去看看。抱歉,没有SSCCE可提供。我的问题是一般性的,因为我真的不知道该怎么做。请尽量集中你的问题;A会有帮助的。谢谢你的指点。过几个小时我去看看。抱歉,没有SSCCE可提供。我的问题是一般性的,因为我真的不知道该怎么做。我可以。但是如何在
JOptionPane.showMessageDialog()
中使用此CustomButtom
?如果使用UIManager,则自定义按钮将显示在消息对话框中。如果您使用CustomButton类,请忘记showMessageDialog(),只需制作自己的JDialog来显示自己的按钮和任何您想要的内容。我如何为自定义按钮和常规按钮添加颜色?事实上,我使用的是一个标签,但是你也指定了什么属性的颜色,因为属性中只有标签。前景你如何设置自定义classI的属性。但是如何在JOptionPane.showMessageDialog()
中使用此CustomButtom
?如果使用UIManager,则自定义按钮将显示在消息对话框中。如果您使用CustomButton类,请忘记showMessageDialog(),只需制作自己的JDialog来显示自己的按钮和任何您想要的内容。我如何为自定义按钮和常规按钮添加颜色?事实上,我使用的是一个标签,但是你也指定了什么属性的颜色,因为所有属性都是Label.foreground你如何为自定义类设置属性