[英]Error while using swing
I'm trying to create panel with some button which would enables disable the other button What's the problem with the codes its compiled perfectly but while running shows the error. 我正在尝试使用一些按钮创建面板,这将启用禁用其他按钮它编译的代码有什么问题,但运行时显示错误。 I'm not able to debug this. 我无法调试这个。
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)
Here's the code 这是代码
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);// shortcut M
is code line to the non_initalized JButton
(initialized in the next code line) b3.setMnemonic(KeyEvent.VK_M);// shortcut M
是non_initalized JButton
代码行(在下一个代码行中初始化)
not all combinations for Text and Icon can be matched together , without an exceptions from AbstractButton 并非Text和Icon的所有组合都可以匹配在一起 ,而AbstractButton没有例外
use only constants for BorderLayout 仅使用BorderLayout的常量
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();
}
});
}
}
EDIT 编辑
Java6 on WinXP caused the with same exception WinXP上的Java6导致了同样的异常
java.lang.NoClassDefFoundError: JButton/ButtonDemo Caused by: java.lang.ClassNotFoundException: JButton.ButtonDemo at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: JButton.ButtonDemo. java.lang.NoClassDefFoundError:JButton / ButtonDemo由以下引起:java.lang.ClassNotFoundException:java.net.URLClassLoader $ 1.run(URLClassLoader.java:202)中的JButton.ButtonDemo,位于java.security.AccessController.doPrivileged(Native Method)at Java.net.URLClassLoader.findClass(URLClassLoader.java:190)at java.lang.ClassLoader.loadClass(ClassLoader.java:307)at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)at java.lang .ClassLoader.loadClass(ClassLoader.java:248)找不到主类:JButton.ButtonDemo。 Program will exit. 程序将会退出。 Exception in thread "main" Java Result: 1 线程“main”Java结果中的异常:1
private
to use instead of protected static ImageIcon createImageIcon(String path) {
caused am exception 但必须使用正确的private
来代替protected static ImageIcon createImageIcon(String path) {
引起异常 @see @看到
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.