【问题标题】:Troubleshooting a Swing Radio Buttons Applet对 Swing 单选按钮小程序进行故障排除
【发布时间】:2013-08-13 06:29:35
【问题描述】:

我必须为学校作业创建一个 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。任何可以提供的帮助将不胜感激。

【问题讨论】:

  • 您的编程进程已关闭。如果您不能使用立即通知您编译错误的 IDE,那么您应该尽早编译您的代码,并且在您添加代码时经常编译,可能在每 1-2 行之后编译。这里的关键是在 all 编译错误得到修复之前不要添加任何新代码。否则,您可能会遇到像我们在这里看到的错误的老鼠窝。
  • 我没有为此使用IDE,我只是像野蛮人一样在记事本中输入它。这是我的第一个 Java 作业,所以我想我会在没有 IDE 的情况下尝试一下。
  • 不管怎样,过程都是一样的:1)尽早修复所有编译错误。 2) 永远不要将好的代码添加到坏的代码中。 3) 研究并从所有错误消息中学习。
  • "Swing、SWT 和 AWT" 专注于 Swing,忽略 SWT,除非它是明确要求,忽略所有 AWT 组件(是我的建议)。关于 AWT/Swing,请在 Swing extras over AWT 上查看此答案,因为有很多充分的理由放弃使用 AWT 组件。
  • 我明白了。我从来没有被指示在我前进的过程中进行编译(我想这对于 IDE 来说是不必要的)。在我的编程事业中,我所要做的就是我的在线学校课程,不幸的是,这些课程的指导很差。感谢您的输入,我会记住这一点;以及获得一个 IDE。

标签: java swing applet radio


【解决方案1】:

改变...

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

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

【讨论】:

  • 非常感谢,我的大部分错误似乎都是由于没有仔细阅读我的代码造成的。这使它能够成功编译,但是当我尝试运行它时,我现在遇到了这个问题:i.imgur.com/I7mNzhj.jpg
  • 因为类文件存在于包中,所以您需要执行它。将目录更改为上一级 (cd ..) 并尝试改用java components.OSButtons
  • 我更改了目录,因此它将是 C:\Users\Keith 而不是以前使用的 C:\Users\Keith\Desktop,然后运行您在上面放置的内容,我收到“错误:可以找不到或加载主类 components.OSButtons”。我编译不正确吗?我的编译方法是打开 cmd 并在 C:\Users\Keith\Desktop 目录中输入“javac OSButtons.java”。
  • 有什么可以感谢您的吗?有没有代表系统之类的?我是新人,所以我还不知道这个网站上的一切是如何运作的。
  • 你刚刚做到了。赞成票永远不会误入歧途,但是您已经接受答案是可以接受的,所以除了传递您所学的知识之外,仅此而已:D
猜你喜欢
  • 2022-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
相关资源
最近更新 更多