有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Eclipse未打开框架窗口

我在EclipseIndigo中运行了这段代码,没有任何帧作为输出打开,但当我在BLUEJ中运行相同的代码时,它工作正常,并且帧正在打开。请告诉我那只虫子

这是我的密码:

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;

public class MainFrame extends JFrame 
{

 void MainFrame()
{
    setTitle("Square's Root Finder");
    setSize(350,100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLookAndFeel();
    setVisible(true);
    JButton but1 = new JButton("Calculate");
    JLabel label1= new JLabel("Enter the number:");
    JTextField t = new JTextField();
    add(but1);
    add(label1);
    add(t);


}
 private void setLookAndFeel() {
      try {
      UIManager.setLookAndFeel(
      "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
      } catch (Exception exc) {
      // ignore error
      }
      }

public static void main(String[] args)
{
    MainFrame newFrame = new MainFrame();

}

      }

共 (1) 个答案

  1. # 1 楼答案

    Bug就在这一行:

     void MainFrame()
    

    将其更改为:

    public MainFrame()
    

    根据JAVA documentation,构造函数不应具有返回类型

    Constructor declarations look like method declarations—except that they use the name of the class and have no return type.