Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

java - invalid method declaration, return type required, on a GUI

So im trying to make a simple GUI that goes up by one when you click a button in it. I get this error, however, when i try and run the test GUI: Error. Here is the code

import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.JPanel;


public class Main {

public GUI() {

  JFrame frame = new JFrame();
  JPanel panel = new JPanel();
  panel.setBorder(borderFactory.createEmptyBorder(30, 30, 10, 30));
  panel.setLayout(new GridLayout(0, 1));

  frame.add(panel, BorderLayout.CENTER);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setTitle("Clicks");
  frame.pack();
  frame.setVisible(true);

}

  public static void main(String[] args) {

    new GUI();

  }
}
question from:https://stackoverflow.com/questions/65908040/invalid-method-declaration-return-type-required-on-a-gui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public class Main {

public GUI() {

GUI seems intended as a constructor and the constructor must have the same name as the class. But neither GUI nor Main are good, descriptive names for a class / constructor. The name should be descriptive of that the class actually is. So here is something that might not only be better, but should work for this case.

public class ClicksGUI {

public ClicksGUI() {

Note that if changing the name of the class / constructor, the reference in the main method also needs to change to reflect that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...