我認爲setBorder(null);
在/復位默認Borders
從UIMananager
來抓GTK大號& F或爲JTextField
編輯將該值設置爲UIMananager
直接:
你必須改變BorderUIResource,以同樣的方式作爲demonstrated here for FontUIResource
EDIT2:
和在1,5秒
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.plaf.BorderUIResource;
public class SystemFontDisplayer extends JFrame {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
private javax.swing.Timer timer = null;
private JTextField testTextField = new JTextField("testTextField");
private JButton testButton = new JButton("testButton");
public SystemFontDisplayer() {
frame.setLayout(new GridLayout(4, 0, 20, 20));
testTextField.setBorder(null);
frame.add(testTextField);
frame.add(testButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(200, 105);
frame.pack();
frame.setVisible(true);
start();
}
private void start() {
timer = new javax.swing.Timer(1500, updateCol());
timer.setRepeats(false);
timer.start();
}
public Action updateCol() {
return new AbstractAction("text load action") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
Border border = new LineBorder(Color.red, 2);
final BorderUIResource res = new BorderUIResource(border);
UIManager.getLookAndFeelDefaults().put("TextField.border", new BorderUIResource(border));
UIManager.getLookAndFeelDefaults().put("Button.border", new BorderUIResource(border));
SwingUtilities.updateComponentTreeUI(frame);
/*Border border = new LineBorder(Color.red, 2);
try {
LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
final BorderUIResource res = new BorderUIResource(border);
UIDefaults uiDefaults = lnf.getDefaults();
uiDefaults.put("TextField.border", res);
uiDefaults.put("Label.border", res);
uiDefaults.put("Button.border", res);
UIManager.getLookAndFeel().uninitialize();
UIManager.setLookAndFeel(lnf);
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
UIDefaults defaults = UIManager.getDefaults();
BorderUIResource res = new BorderUIResource(border);
defaults.put("TextField.border", res);
defaults.put("Label.border", res);
defaults.put("Button.border", res);
SwingUtilities.updateComponentTreeUI(frame);*/
}
};
}
public static void main(String arg[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
}
});
}
}
出於興趣,你運行的是哪個版本的Ubuntu/Java?我剛剛使用OpenJDK 1.6.0_24在Ubuntu 12.04機器上運行此代碼,並且按預期運行;它看起來就像一個標籤。 – Jamie
@Jamie Ubuntu 10.04和java 1.6.0.22。你有沒有設置UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())? – Ivan
啊,這確實打破了......不好意思,我以爲默認會使用GTK LookAndFeel,但顯然不是! – Jamie