好的,所以我做了一个测试java applet,以确保我知道如何制作它们,但我的默认外观看起来真的很难看。所以我想把它改成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
}
}
}
答案 0 :(得分:3)
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的错误消息。