我必须为学校作业创建一个Swing小程序,并获得了链接(http://java.sun.com/docs/books/tutorial/uiswing/components/index.html),以查看各种Swing教程并使用其中的一个来创建独特的Java小程序。我选择遵循“如何使用单选按钮”教程的代码。我通读代码并在更改内容时将其键入,以便它们与我的图片匹配。我的代码是

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();
            }
        });
    }
}


我希望这是可读的。无论如何,我编译了代码,并提出了以下错误:



我真的不知道如何从这里着手解决这些问题,这项任务有点丢给我了,我不得不独自研究Swing,SWT和AWT。可以提供的任何帮助将不胜感激。

最佳答案

更改...

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


至...

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


更改

radiopanel.add(macButton);


至...

radioPanel.add(macButton);


Java区分大小写,变量名大小写必须匹配

这个...

JComponent newContentPane = new RadioButtonDemo();


我怀疑是复制/粘贴错误。您更改了原始代码的class名称,但是忘记更改对其的任何引用。

尝试...

JComponent newContentPane = new OSButtons();


代替

更新资料

好的。假设您的源文件位于C:\Users\Keith\Desktop\components中。

在命令提示符下,您将使用类似...的方式编译它们。

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


程序包名称和类文件的预期目录之间存在直接的联盟。

10-07 19:09
查看更多