0

我使用以下代码来设置我的 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();

}

这在我的网页上随机运行。有什么建议么?

4

2 回答 2

1

我是 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());
    }
}
于 2011-08-26T21:04:14.730 回答
0

调用代码时遇到什么异常?

如果没有例外,您是否总是在绘制任何东西之前调用外观和感觉变化?

最后,可能是用户没有设置/安装那种外观和感觉吗?

于 2011-08-26T20:22:25.187 回答