java - Swing 单选按钮小程序故障排除

标签 java swing radio-button applet

我必须为一项学校作业创建一个 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();
            }
        });
    }
}

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

enter image description here

我真的不知道如何从这里开始解决这些问题,这个任务有点丢给我,我必须完全自己研究 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

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

关于java - Swing 单选按钮小程序故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18178621/

相关文章:

java - 显示在事件分派(dispatch)线程中构建 GUI 的进度的启动屏幕

java - RazorSQL 不工作?

javascript - 创建可重用的 jQuery 函数

android - 带有 simple_list_item_single_choice 的文本颜色数组适配器

java - 在服务器上发布gradle android库

java - 我可以改进这个 Pig-Latin 转换器吗?

java - 打印不同的数组元素

java - 使用@Qualifier注释抛出 "NoUniqueBeanDefinitionException"(发现多个相同类型的bean)

java - 关于setMnemonic的一些问题

iphone - 在 Tableview 中添加单选按钮遇到一些问题