Windows风格:
Swing默认就是这种风格,与WinForm有过之而不及
OS X风格:
可以看到JTextField边框有蓝色闪光,具有立体感;JButton四角变得圆润有光泽,确实漂亮了不少。代码很简单,在Frame窗口new
之前加入如下语句:
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
完整代码:
public class CSDN1 {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CSDN1 window = new CSDN1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CSDN1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(57, 102, 153, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Start");
btnNewButton.setBounds(245, 101, 84, 23);
frame.getContentPane().add(btnNewButton);
}
}