Reputation: 25
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:
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.
Upvotes: 1
Views: 132
Reputation: 347194
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.
Upvotes: 1