1

I have to create a Swing applet for a school assignment, and was given a link (http://java.sun.com/docs/books/tutorial/uiswing/components/index.html) to look at various Swing tutorials and use one of them to create a unique Java applet. I chose to follow the code for the How To Use Radio Buttons tutorial. I read through the code and typed it up while changing things so that they would match my pictures. The code I have is

package components;

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

public class OSButtons extends JPanel implements ActionListener {

    static String windowsString = "Windows";
    static String linuxString = "Linux";
    static String macString = "Mac";

    JLabel picture;

    public OSButtons() {
        super(new BorderLayout());

        JRadioButton windowsButton = new JRadioButton(windowsString);
        windowsButton.setMnemonic(KeyEvent.VK_W);
        windowsButton.setActionCommand(windowsString);
        windowsButton.setSelected(true);

        JRadioButton linuxButton = new JRadioButton(linuxString);
        linuxButton.setMnemonic(KeyEvent.VK_L);
        linuxButton.setActionCommand(linuxString);

        JRadioButton macButton = new JRadioButton(macString);
        macButton.setMnemonic(KeyEvent.VK_M);
        macButton.setActionCommand(macString);

        ButtonGroup group = new ButtonGroup();
        group.add(windowsButton);
        group.add(linuxButton);
        group.add(macButton);

        windowsButton.addActionListener(this);
        linuxButton.addActionListener(this);
        macButton.addActionListener(this);

        picture = new JLabel(createImageIcon("images/" + windowsString + ".gif"));
        picture.setPreferredSize(new Dimension(200, 150));

        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(windowsButton);
        radioPanel.add(linuxButton);
        radioPanel.add(macButton);

        add(radioPanel, BorderLayout.LINE_START);
        add(picture, BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    }

    public void actionPerformed(ActionEvent e) {
        picture.setIcon(createImageIcon("images/" + e.getActionCommand() + ".gif"));
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = OSButtons.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("OSButtons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent newContentPane = new RadioButtonDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I hope that's readable. Anyways, I compiled the code and it came up with these errors:

enter image description here

I really have no idea how to proceed from here as far as fixing these, this assignment was kind of thrown onto me and I had to research Swing, SWT, and AWT completely on my own. Any help that could be offered would be greatly appreciated.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Programming Noob
  • 25
  • 2
  • 2
  • 5
  • Your process for programming is off. If you can't use an IDE that notifies you immediately of compilation errors, then you should compile your code early and often as you add to it, perhaps after each 1-2 new lines. The key here is to not add any new code until ***all*** compilation errors are fixed. Otherwise you risk ending up with a rat's nest of errors like we see here. – Hovercraft Full Of Eels Aug 12 '13 at 02:15
  • I didn't use an IDE for this, I just typed it up in notepad like a barbarian. This is my first Java assignment so I figured I would try it out without an IDE. – Programming Noob Aug 12 '13 at 02:31
  • 1
    Regardless, the process is the same: 1) fix all compilation errors early. 2) **Never** add good code to bad. 3) Study and learn from all error messages. – Hovercraft Full Of Eels Aug 12 '13 at 02:43
  • *"Swing, SWT, and AWT"* Concentrate on Swing, ignore SWT unless it is an explicit requirement, ignore all the AWT **components** (is my advice). Re AWT/Swing, see this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Aug 12 '13 at 02:46
  • 1
    I see. I was never instructed to compile as I go along (which I suppose wouldn't be necessary with an IDE). All I have to go along with my programming ventures is my online school lessons, which are quite poorly instructed unfortunately. Thank you for the input, I'll keep this in mind; as well as get an IDE. – Programming Noob Aug 12 '13 at 02:46
  • Swing is what I'll be programming in, I suppose I worded that poorly. I'll be coding with Swing, but had to research and learn about SWT and AWT. – Programming Noob Aug 12 '13 at 02:48
  • BTW - Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Aug 13 '13 at 02:22
  • Yes, it was the instructions that I was given by the teacher. I'll direct them to that article! – Programming Noob Aug 13 '13 at 04:53

1 Answers1

1

Change...

picture = newJLabel(createImageIcon("images/"+ windowsString + ".gif"));

to...

picture = new JLabel(createImageIcon("images/"+ windowsString + ".gif"));

Change

radiopanel.add(macButton);

to...

radioPanel.add(macButton);

Java is case sensitve, variable names case must match

This...

JComponent newContentPane = new RadioButtonDemo();

I suspect is a copy/paste error. You change the class name of the original code, but forgot to change any references to it.

Try...

JComponent newContentPane = new OSButtons();

Instead

Update

Okay. Let's assume that you have your source files in C:\Users\Keith\Desktop\components.

At the command prompt your would compile them by using something like...

C:\> cd C:\Users\Keith\Desktop
C:\Users\Keith\Desktop> javac components.OSButtons.java
C:\Users\Keith\Desktop> java components.OSButtons

There is a direct coalition between the package name and the expected directory of the class files.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks so much, it seems like most of my errors here were caused by not going over and re-reading my code. This allowed it to successfully compile, but I'm now running into this when I try to run it: http://i.imgur.com/I7mNzhj.jpg – Programming Noob Aug 12 '13 at 02:30
  • Because the class file lives in package you need execute it as such. Change directories one level up (`cd ..`) and try `java components.OSButtons` instead – MadProgrammer Aug 12 '13 at 02:38
  • I changed directories so it would be C:\Users\Keith rather than the previously used C:\Users\Keith\Desktop, and ran what you put above, and I am receiving "Error: Could not find or load main class components.OSButtons". Am I compiling this incorrectly? My method for compiling was opening cmd and typing 'javac OSButtons.java' within the C:\Users\Keith\Desktop directory. – Programming Noob Aug 12 '13 at 02:43
  • Is there anything I can do to thank you? Is there a rep system or something? I'm new so I don't know how everything on this site works yet. – Programming Noob Aug 12 '13 at 03:21
  • You just did. An up-vote never goes astray, but you've accepted the answer as being acceptable so other then passing on what you have learned, that's about it :D – MadProgrammer Aug 12 '13 at 03:22
  • Looks like I need 15 rep to upvote.. But thank you very much, you are a gentleman and a scholar, good sir! – Programming Noob Aug 12 '13 at 03:24