gpt4 book ai didi

java - 更改 Java 小程序的外观

转载 作者:行者123 更新时间:2023-12-04 05:21:13 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com