java - 更改 Java 小程序的外观

标签 java swing look-and-feel japplet nimbus

好的,所以我制作了一个测试 java 小程序以确保我知道如何制作它们,但我的默认外观看起来真的很难看。所以我想切换它 Nimbus 但我的默认不起作用。我已经完成了大约一个小时的谷歌搜索,但没有任何结果,除了一个有问题的论坛,但没有我能理解的答案。有任何想法吗?如果这很难理解,我很抱歉。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

public class Websitetest extends JApplet implements ActionListener, Runnable {

    boolean i = false;
    Color red = new Color(255, 0, 0);
    Color lightgrey = new Color(200, 200, 200);
    Thread runner;    

    public void init() {
        setLookAndFeel();
        Button dp = new Button("Don't press");
        dp.addActionListener(this);
        FlowLayout flow = new FlowLayout();
        setLayout(flow);
        add(dp);
    }

    public void paint(Graphics screen) {
        Graphics2D screen2D = (Graphics2D) screen;
        if (i == false) {screen2D.setColor(lightgrey);}
        else {screen2D.setColor(red);}
        screen2D.fillRect(0, 0, getSize().width, getSize().height);
        screen2D.setColor(Color.black);
        if (i == false) {screen2D.drawString("", 5, 60);}
        else {screen2D.drawString("I SAID DON'T PRESS!", 90, 60);}
    }

    public void start() {
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }

    public void stop() {
        if (runner != null) {
            runner = null;
        }
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
    }

    public void actionPerformed(ActionEvent event) {
        i = true;
        repaint();
    }

    private void setLookAndFeel() {
        try {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception exc) {
            //ignore error
        }
    }
}

最佳答案

This page on Oracle's site关于该主题表明 Nimbus 可能不可用。他们使用以下片段切换到 Nimbus 如果可用

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    // If Nimbus is not available, you can set the GUI to another look and feel.
}

由于您在设置之前不检查 Nimbus 是否存在:您确定它在您的系统上真的可用吗?

我建议暂时删除 catch 子句,然后看看您是否收到有关未安装 Nimbus 的错误消息。

关于java - 更改 Java 小程序的外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13670510/

相关文章:

java - Thymeleaf + Spring(不是 Boot)- 如何显示来自 messageSource 的消息

java - 将字段添加到 Clojure 中的代理类

java - NullPointerException 与 getText()

java - 在 Windows LAF 下为每个组件的 JTextField 设置非 Activity 背景颜色

java - 检查 selenium rc 中的日期格式

java - 您知道是否可以在 Android 中(在 Android 手机中)编译 libgdx 而无需在 native JAVA 中编译它吗?

java - Cobertura 改变了 Sonar 违规行为

Java Swing : Extended jpanel class will not brighten upon repainting.

java - 未选择时如何更改 MacOS 中 Java JComboBox 的背景颜色

java - 更改 JFrame 的外观和感觉?