[英]Invalid Method Declaration: Return type missing
Ok so I updated the code and now it compiles. 好的,所以我更新了代码,现在可以编译了。 But when I run this, it does not have the window pop up with the Shape Stamper.
但是当我运行它时,它没有用Shape Stamper弹出窗口。 I do not know why it it not coming up.
我不知道为什么它没有出现。 Is it because I have a line that says:
public void StamperFrame()
? 是因为我有一句话说:
public void StamperFrame()
吗?
I tried removing the void and it gives an error when I do that. 我尝试删除了空白,这样做时出现了错误。 It says invalid method declaration: Return type missing.
它说无效的方法声明:缺少返回类型。 The code still compiles and says successful, but it will not.
该代码仍然可以编译并表示成功,但是不会成功。
Here is the code now: 现在是下面的代码:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.JComponent;
public class ShapeStamper extends JFrame {
public static void main(String[] args){
ShapeStamper s = new ShapeStamper();
s.StamperFrame();
}
private JButton circleButton, squareButton, rectButton,ovalButton;
private int buttonValue = 0;
public void StamperFrame() {
setTitle("Shape Stamper");
setSize(500, 500);
JPanel buttonPanel = new JPanel();
circleButton = new JButton("Circle");
ovalButton = new JButton("Oval");
squareButton = new JButton("Square");
rectButton = new JButton("Rectangle");
buttonPanel.add(circleButton);
buttonPanel.add(squareButton);
buttonPanel.add(rectButton);
buttonPanel.add(ovalButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
circleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 1;
System.out.println(buttonValue);
}
});
ovalButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 2;
System.out.println(buttonValue);
}
});
squareButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 3;
System.out.println(buttonValue);
}
});
rectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 4;
System.out.println(buttonValue);
}
});
getContentPane().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (buttonValue == 1) {
System.out.println("Circle added at: " + e.getX() + "," + e.getY());
} else if (buttonValue == 2) {
System.out.println("Oval added at: " + e.getX() + "," + e.getY());
} else if (buttonValue == 3) {
System.out.println("Square added at: " + e.getX() + "," + e.getY());
} else if (buttonValue == 4) {
System.out.println("Rectangle added at: " + e.getX() + "," + e.getY());
}
}
});
}
}
public class ShapeStamper extends JFrame {
public static void main(String[] args){
StamperFrame(); //Add this line
}
AND don't forget the return value of stamperframe method 并且不要忘记Stamperframe方法的返回值
public VOID StamperFrame(){…} 公开VOID StamperFrame(){…}
EDIT Try this 编辑尝试一下
public class ShapeStamper extends JFrame {
public static void main(String[] args){
ShapeStamper s = new ShapeStamper(); //Add this line
s.StamperFrame(); //Add this line
}
EDIT you should make the frame visible 编辑你应该使框架可见
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(500, 500);
this.setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.