3

Ok, So I made an test java applet to make sure I know how to make them but I the default look and feel looks really ugly. So I want to switch it Nimbus but my tacit isn't working. I've done about an hour worth of Google searching but nothing came up, Except one forum that had the question, But no answers I could understand. Any ideas? And if this is hard to understand, i'm sorry.

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
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jdman216
  • 55
  • 1
  • 10
  • Change `} catch (Exception exc) { //ignore error }` to `} catch (Exception exc) { exc.printStackTrace(); }` & copy/paste the result as an edit to the question. – Andrew Thompson Dec 02 '12 at 22:23

1 Answers1

3

This page on Oracle's site on the topic suggests that Nimbus may not be available. They use the following fragment to switch to Nimbus if it is available.

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.
}

Since you don't check for the presence of Nimbus before setting it: are you sure it is really available on your system?

I suggest remove the catch clause for a moment and seeing if you then get an error message about Nimbus not being installed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807