我使用以下代码来设置Java Applet的外观。这完全可以在Java应用程序中使用。
编辑
@Override
public void init() {
try {
//This sets the look and feel to NIMBUS.
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
initComponents();
//Calls the method showStartScreen()
startGame();
}
这可以在我的网页上随机运行。有什么建议么?
最佳答案
我是BIG Nimbus粉丝之一
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.*;
public class NimbusSizing implements Runnable, ItemListener {
private JFrame frame;
private JSpinner spinner;
private JComboBox combo;
private JRadioButton radio;
private JCheckBox check;
private JButton button;
public static void main(String[] args) {
SwingUtilities.invokeLater(new NimbusSizing());
}
@Override
public void run() {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
System.out.println("\t" + info);
if ("Nimbus".equals(info.getName())) {
try {
UIManager.setLookAndFeel(info.getClassName());
break;
} catch (Exception x) {
x.printStackTrace();
}
}
}
JPanel panel = new JPanel(null);
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
combo = new JComboBox(new Object[]{"mini", "small", "regular", "large"});
combo.setSelectedIndex(2);
combo.addItemListener(this);
spinner = new JSpinner();
radio = new JRadioButton("Radio");
check = new JCheckBox("Check");
button = new JButton("Button");
layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING, true).
addComponent(spinner).addComponent(radio).
addComponent(check).addComponent(button).addComponent(combo));
final int sz = GroupLayout.PREFERRED_SIZE;
layout.setVerticalGroup(layout.createSequentialGroup().addComponent(spinner, sz, sz, sz).
addComponent(radio, sz, sz, sz).addComponent(check, sz, sz, sz).
addComponent(button, sz, sz, sz).addComponent(combo, sz, sz, sz));
frame = new JFrame(getClass().getSimpleName());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
resize((String) combo.getSelectedItem());
}
}
private void resize(String value) {
System.out.println("resize(" + value + ")");
System.out.println("\t" + spinner.isFontSet() + " " + System.identityHashCode(spinner.getFont()) + " " + spinner.getFont());
spinner.putClientProperty("JComponent.sizeVariant", value);
combo.putClientProperty("JComponent.sizeVariant", value);
radio.putClientProperty("JComponent.sizeVariant", value);
check.putClientProperty("JComponent.sizeVariant", value);
button.putClientProperty("JComponent.sizeVariant", value);
spinner.setFont(null);
for (int i = spinner.getComponentCount(); --i >= 0;) {
spinner.getComponent(i).setFont(null);
}
radio.setFont(null);
check.setFont(null);
button.setFont(null);
combo.setFont(null);
SwingUtilities.updateComponentTreeUI(frame);
System.out.println("\t" + spinner.isFontSet() + " " + System.identityHashCode(spinner.getFont()) + " " + spinner.getFont());
}
}