我想创建自己的外观和感觉,并派生了 MetalLookAndFeel。我已经设置了一个非常基本的配置,其中所有文件都在同一个目录中并且没有包声明:
MyLookAndFeel.java
import javax.swing.UIDefaults;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class MyLookAndFeel extends MetalLookAndFeel {
public MyLookAndFeel() {
setCurrentTheme(new DefaultMetalTheme());
}
@Override
protected void initClassDefaults(UIDefaults table) {
super.initClassDefaults(table);
table.put( "ButtonUI", "MyButtonUI");
}
@Override
public String getName() {
return "MyLookAndFeel";
}
@Override
public String getID() {
return "MyLookAndFeel";
}
@Override
public String getDescription() {
return "MyLookAndFeel";
}
@Override
public boolean isNativeLookAndFeel() {
return false;
}
@Override
public boolean isSupportedLookAndFeel() {
return true;
}
}
我们还有 MyButtonUI.java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.metal.MetalButtonUI;
public class MyButtonUI extends MetalButtonUI {
@Override
public void paint(Graphics g, JComponent c) {
}
@Override
protected void paintButtonPressed(Graphics g, AbstractButton b) {
g.fillRect(0, 0, 40, 40);
}
@Override
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
String text) {
}
}
使用上面的代码,当我使用此构造函数运行类时,我希望会绘制一个奇怪的按钮(或者根本没有按钮,因为该paint
方法是空的)TestUI JFrame
,但是该按钮看起来很正常:
public TestUI() throws UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(new MyLookAndFeel());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
setLayout(new FlowLayout());
add(new JButton("Press"));
add(new JLabel("Text"));
setVisible(true);
}
我忘记了什么重要的事情吗?