Java 使用swing时出错
我正在尝试创建一个面板,其中包含一些按钮,可以启用或禁用另一个按钮 代码有什么问题?它编译得很好,但运行时显示错误。 我无法调试这个Java 使用swing时出错,java,swing,jbutton,runtime-error,imageicon,Java,Swing,Jbutton,Runtime Error,Imageicon,我正在尝试创建一个面板,其中包含一些按钮,可以启用或禁用另一个按钮 代码有什么问题?它编译得很好,但运行时显示错误。 我无法调试这个 Exception in thread "main" java.lang.NoClassDefFoundError: buttondemo (wrong nam e: components/buttondemo) at java.lang.ClassLoader.defineClass1(Native Method) at java
Exception in thread "main" java.lang.NoClassDefFoundError: buttondemo (wrong nam
e: components/buttondemo)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)
这是密码
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class buttondemo extends JPanel implements ActionListener {
private JButton b1, b2, b3;
public buttondemo() {
ImageIcon Left = createImageIcon("C:\\Users\\nco\\Desktop\\Swing\\components\\images\\left.png");
ImageIcon Right = createImageIcon("C:\\Users\\nco\\Desktop\\Swing\\components\\images\\right.jpg");
ImageIcon Middle = createImageIcon("C:\\Users\\nco\\Desktop\\Swing\\components\\images\\middle.jpg");
b1 = new JButton("Disable middle button", Left);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setMnemonic(KeyEvent.VK_D);// shortcut D
b1.setActionCommand("disable");
b2 = new JButton("middle button", Middle);
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setVerticalTextPosition(AbstractButton.CENTER);
b3.setMnemonic(KeyEvent.VK_M);// shortcut M
b3 = new JButton("Enable middle button", Right);
b3.setVerticalTextPosition(AbstractButton.RIGHT);
b3.setMnemonic(KeyEvent.VK_E);// shortcut E
b3.setActionCommand("enable");
b3.setEnabled(false);
b1.addActionListener(this);
b3.addActionListener(this);
b1.setToolTipText("click on the middle button to " + "disable middle");
b3.setToolTipText("click on the middle button to " + "enable middle");
b2.setToolTipText("click disable");
add(b1);
add(b2);
add(b3);
}
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
} else {
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
}
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL img = buttondemo.class.getResource(path);
if (img != null) {
return new ImageIcon(img);
} else {
System.err.println("could not find path" + path);
return null;
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Button demso");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttondemo Contentpane = new buttondemo();
Contentpane.setOpaque(true);
frame.setContentPane(Contentpane);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
是非初始化的b3.setMnemonic(KeyEvent.VK_M);//快捷键M
的代码行(在下一个代码行中初始化)JButton
- 并非所有,AbstractButton中没有例外
- 仅使用
- 但是必须使用适当的
来代替private
导致a.m.异常受保护的静态图像图标createImageIcon(字符串路径){
Java似乎希望该类驻留在components包中,但要么它没有,要么它没有从正确的上下文(路径位置)执行错误是什么时候发生的?在加载时还是在单击按钮时?在promptClass中运行时,文件也在组件包中抱歉,我只是忘记了在代码中粘贴代码时添加包组件。这是一个暗中操作:您是构建jar文件,还是只运行类文件?如果您运行类文件,则类文件和包是按预期命名的目录?Windows不应该考虑大写或小写,但我似乎记得某些场景中的问题(例如在网络共享上构建)删除完整的构建目录有帮助吗?也可以在<代码> ButoGoope<代码>中考虑<代码> JToggelButton <代码>,我也在使用Wi7,但是您的代码也显示了相同的错误。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class buttondemo extends JPanel implements ActionListener {
private JButton b1, b2, b3;
public buttondemo() {
Icon Left = UIManager.getIcon("OptionPane.errorIcon");
Icon Right = UIManager.getIcon("OptionPane.informationIcon");
Icon Middle = UIManager.getIcon("OptionPane.warningIcon");
b1 = new JButton("Disable middle button", Left);
b1.setVerticalTextPosition(SwingConstants.TOP);
b1.setHorizontalTextPosition(SwingConstants.CENTER);
b1.setMnemonic(KeyEvent.VK_D);//shortcut D
b1.setActionCommand("disable");
b2 = new JButton("middle button", Middle);
b2.setVerticalTextPosition(SwingConstants.BOTTOM);
b2.setHorizontalTextPosition(SwingConstants.LEFT);
b3 = new JButton("Enable middle button", Right);
b3.setMnemonic(KeyEvent.VK_M);//shortcut M
b2.setVerticalTextPosition(SwingConstants.EAST);
b2.setHorizontalTextPosition(SwingConstants.CENTER);
b3.setActionCommand("enable");
b3.setEnabled(false);
b1.addActionListener(this);
b3.addActionListener(this);
b1.setToolTipText("click on the middle button to " + "disable middle");
b3.setToolTipText("click on the middle button to " + "enable middle");
b2.setToolTipText("click disable");
add(b1);
add(b2);
add(b3);
}
@Override
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
} else {
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
}
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL img = buttondemo.class.getResource(path);
if (img != null) {
return new ImageIcon(img);
} else {
System.err.println("could not find path" + path);
return null;
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Button demso");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttondemo Contentpane = new buttondemo();
Contentpane.setOpaque(true);
frame.setContentPane(Contentpane);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
private ImageIcon createImageIcon(String path) {
java.net.URL img = ButtonDemo.class.getResource(path);
if (img != null) {
return new ImageIcon(img);
} else {
System.err.println("could not find path" + path);
return null;
}
}