Java Applet 中的外观和感觉不起作用

发布于 2024-12-01 18:47:11 字数 454 浏览 3 评论 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();

}

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海拔太高太耀眼 2024-12-08 18:47:11

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

I'm one of BIG Nimbus fans

enter image description here

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());
    }
}
怪我鬧 2024-12-08 18:47:11

调用代码时出现什么异常?

如果没有例外,您是否总是在绘制任何内容之前调用外观和感觉更改?

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文