2

好的,所以我制作了一个测试 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
        }
    }
}
4

1 回答 1

3

Oracle 网站上有关该主题的此页面表明 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 未安装的错误消息。

于 2012-12-02T15:09:10.497 回答