0
我想运行一个图形用户界面我编程,并遇到一个错误,当我尝试运行我的程序在Eclipse中运行。以下消息显示在控制台中:“错误:无法找到或加载主类”。我的java文件是在默认的包中,并且我在网上有解决和搜索小问题的小问题。任何帮助将非常感激!谢谢。Java - 错误:无法找到或加载主类
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class myWindow implements ActionListener, MouseListener, MouseMotionListener, ChangeListener {
private int width, height;
private JFrame myWindow;
private JPanel panel1, panel2, panel3, panel4, panel5;
private JButton button1, button2, button3;
private JLabel label1;
private JCheckBox checkbox1;
private JRadioButton radiobutton1;
private JColorChooser colorChooser;
private boolean mousePressed;
private int old_x, old_y;
private Color color;
public static void main(String[] args) {
myWindow w = new myWindow();
}
public myWindow()
{
width = 600;
height = 400;
mousePressed = false;
old_x = 0;
old_y = 0;
color = new Color(0, 0, 0);
myWindow = new JFrame();
myWindow.setSize(width, height);
myWindow.setLocation(100, 100); // Sets location of the window
myWindow.setTitle("Window title");
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Terminates the program on close
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button3 = new JButton("Button 3");
checkbox1 = new JCheckBox("Check"); // Creates check box
radiobutton1 = new JRadioButton("Radio Button"); // Creates radio button
colorChooser = new JColorChooser();
panel1 = new JPanel(); // Creates panel
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(panel2); // Adds panel
panel1.add(panel3);
panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS)); // Changes to make buttons vertical
panel2.add(panel4);
panel2.add(panel5);
panel3.add(colorChooser);
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
panel4.add(button1); // Adds button to stated panel
panel4.add(button2);
panel4.add(button3);
panel4.add(checkbox1);
panel4.add(radiobutton1);
myWindow.add(panel1);
label1 = new JLabel(new ImageIcon("res/images/dota2.jpg")); // Gathers the image
panel5.add(label1);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
label1.addMouseListener(this);
label1.addMouseMotionListener(this);
colorChooser.getSelectionModel().addChangeListener(this);
myWindow.setVisible(true); //Makes window visible
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1)
{
System.out.println("Button 1 pressed.");
button1.setText("Button 1 pressed");
}
else if(e.getSource() == button2)
{
System.out.println("Button 2 pressed.");
button2.setText("Button 2 pressed");
}
else if(e.getSource() == button3)
{
System.out.println("Button 3 pressed.");
button3.setText("Button 3 pressed");
}
}
public void mouseClicked(MouseEvent e) {
if(e.getSource() == label1)
{
System.out.println("Mouse clicked.");
System.out.println("(x,y) = (" + e.getX() + ", " + e.getY() + ")");
Graphics g = label1.getGraphics();
g.drawOval(e.getX(), e.getY(), 3, 3);
}
}
public void mousePressed(MouseEvent e){
System.out.println("mousePressed = " + mousePressed);
Graphics g = label1.getGraphics();
g.setColor(color);
g.drawOval(e.getX(), e.getY(), 3, 3);
mousePressed = true;
old_x = e.getX();
old_y = e.getY();
}
public void mouseDragged(MouseEvent e) {
System.out.println("mousePressed = " + mousePressed);
if(mousePressed)
{
Graphics g = label1.getGraphics();
// g.drawOval(e.getX(), e.getY(), 3, 3);
g.setColor(color);
g.drawLine(old_x, old_y, e.getX(), e.getY());
old_x = e.getX();
old_y = e.getY();
}
}
public void mouseMoved(MouseEvent e) {
if(mousePressed)
{
Graphics g = label1.getGraphics();
g.setColor(color); //I DID THIS MYSELF
g.drawOval(e.getX(), e.getY(), 3, 3);
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
mousePressed = false;
Graphics g = label1.getGraphics();
g.setColor(color);
g.drawOval(e.getX(), e.getY(), 3, 3);
System.out.println(" Mouse released.");
}
public void stateChanged(ChangeEvent e) {
color = colorChooser.getColor();
}
}
你是否已将类添加到类路径中? http://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class –