繁体   English   中英

无效的方法声明:缺少返回类型

[英]Invalid Method Declaration: Return type missing

好的,所以我更新了代码,现在可以编译了。 但是当我运行它时,它没有用Shape Stamper弹出窗口。 我不知道为什么它没有出现。 是因为我有一句话说: public void StamperFrame()吗?

我尝试删除了空白,这样做时出现了错误。 它说无效的方法声明:缺少返回类型。 该代码仍然可以编译并表示成功,但是不会成功。

现在是下面的代码:

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
}

并且不要忘记Stamperframe方法的返回值

公开VOID StamperFrame(){…}

编辑尝试一下

public class ShapeStamper extends JFrame {

    public static void main(String[] args){
         ShapeStamper s = new ShapeStamper(); //Add this line
         s.StamperFrame(); //Add this line
}

编辑你应该使框架可见

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);

this.setSize(500, 500);
this.setVisible(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM