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

时间:2015-05-16 23:05:36

标签: java

好的,所以我更新了代码,现在它编译了。但是当我运行它时,它没有弹出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());
                    }
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

public class ShapeStamper extends JFrame {

    public static void main(String[] args){

        StamperFrame(); //Add this line
}

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

公开 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);