Java实现Swing组件定制Button示例

网友投稿 385 2023-02-25


Java实现Swing组件定制Button示例

本文实例讲述了java实现Swing组件定制Button。分享给大家供大家参考,具体如下:

先来看看运行效果图:

具体代码如下:

package themedemo;

import java.awt.BasicStroke;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.GridLayout;

import java.awt.RenderingHints;zkymPM

import java.util.Map;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Painter;

import javax.swing.SwingUtilities;

import javax.swing.UIDefaults;

import javax.swing.UIManager;

import javax.swing.WindowConstants;

public class ButtonSkinDemo {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

for (UIManager.LookAndFeelInfo laf : UIManager

.getInstalledLookAndFeels()) {

if ("Nimbus".equals(laf.getName())) {

try {

UIManager.setLookAndFeel(laf.getClassName());

} catch (Exception e) {

e.printStackTrace();

}

}

}

for (Map.Entry entry : UIManager

.getLookAndFeelDefaults().entrySet()) {

if ((entry.getKey().toString()).startsWith("Button")) {

System.out.println(entry.getKey() + " = "

+ entry.getValue());

}

}

JFrame frame = new JFrame("jb51.net - Button Skining Demo");

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(new BorderLayout());

JPanel panel = new JPanel(new GridLayout(0, 1, 20, 20));

panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

panel.setBackground(Color.darkGray);

UIDefaults buttonDefaults = new UIDefaults();

buttonDefaults.put("Button.backgroundPainter",

new Painter() {

public void paint(Graphics2D g, JComponent c,

int w, int h) {

g.setRenderingHint(

RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setStroke(new BasicStroke(2f));

g.setColor(Color.WHITE);

g.fillRoundRect(0, 0, w - 2, h - 2, 8, 8);

g.setColor(Color.LIGHT_GRAY);

g.drawRoundRect(0, 0, w - 2, h - 2, 8, 8);

}

});

buttonDefaults.put("Button[Pressed].backgroundPainter",

new Painter() {

public void paint(Graphics2D g, JComponent c,

int w, int h) {

g.setRenderingHint(

RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setStroke(new BasicStroke(2f));

g.setColor(Color.LIGHT_GRAY);

g.fillRoundRect(0, 0, w - 2, h - 2, 8, 8);

}

});

JButton button = new JButton("myButton");

panel.add(button);

button.putClientProperty("Nimbus.Overrides", buttonDefaults);

button.putClientProperty("Nimbus.Overrides.InheritDefaults",

false);

// Add a normal themed slider for comparison

JButton normalButton = new JButton("normalButton");

panel.add(normalButton);

frame.getContentPane().add(panel, BorderLayout.CzkymPMENTER);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue router嵌套路由在history模式下刷新无法渲染页面问题的解决方法
下一篇:vue通过路由实现页面刷新的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~