开发者

Look And Feel in Java Applet Doesn't Work

开发者 https://www.devze.com 2023-03-30 21:42 出处:网络
I use the following code to set the look and feel for my Java Applet. This completely works inside a Java Application.

I use the following code to set the look and feel for my Java Applet. This completely works inside a Java Application.

EDIT

@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();

}

This works at random times on my web page. Any suggestions?


I'm one of BIG Nimbus fans

Look And Feel in Java Applet Doesn't Work

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());
    }
}


What exception are you getting when calling the code?

If there is no exception, do you always call the look and feel change before ANYTHING is painted?

And last, could it be the user doesn't have that look and feel setup/installed?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号