Java 如何导入JFrame?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20413533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:46:57  来源:igfitidea点击:

How to import JFrame?

javaswingjframe

提问by ylun.ca

package main;

import javax.swing.JFrame;

public class Main extends JPanel{

    public Main() {
        add(new Panel());
        setName("NGame");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

The import javax.swing.JFrame;line isn't recognized as a class by eclipse and will not import. The two set methods are not recognised as the JFrame will not import. I have the latest JDK and have checked that the JFrame class is existant with the shift+ ctrl+ Tcommand. What is happening?

import javax.swing.JFrame;行不被 Eclipse 识别为类,并且不会导入。由于 JFrame 不会导入,因此无法识别这两个 set 方法。我有最新的JDK,并检查了JFrame类是existant与shift+ ctrl+T命令。怎么了?

采纳答案by liamslagle

You need to make Mainextend JFrame, not JPanel. Try something like this:

您需要Main扩展JFrame,而不是JPanel. 尝试这样的事情:

package main;

import javax.swing.*;

public class Main extends JFrame {

    public Main() {
       add(new JPanel());
       //...other code
    }

}

回答by giannis christofakis

That's because JPaneldoesn't have those methods. setNamedoesn't even exists.exists, but I don't think it's what you meant to do.

那是因为JPanel没有这些方法。集合名称甚至不存在。存在,但我认为这不是你想要做的。

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

public class Main extends JFrame {

    public Main() {
        //instead of setName()
        setTitle("NGame");
        add(new Panel());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //setVisible(true);
        //pack();
        //etc etc
    }
}

回答by MadProgrammer

Several things...

几件事...

  1. You are extending from JPanel, but not importing it.
  2. You are importing JFrame, but not actually using it.
  3. setDefaultCloseOperationis not a valid method for JPanel
  4. EXIT_ON_CLOSEis an unknown variable as it's not declared within JPanelor any of it's parent classes...
  5. You are mixing heavy and light weight components (Panelis a heavy weight component and JPanelis a light weight component, these components tend not to mix well)
  1. 您是从 扩展JPanel,但不是导入它。
  2. 您正在导入JFrame,但实际上并未使用它。
  3. setDefaultCloseOperation不是有效的方法 JPanel
  4. EXIT_ON_CLOSE是一个未知变量,因为它没有在JPanel其父类中或其任何父类中声明...
  5. 您正在混合重质和轻质成分(Panel是重质成分和JPanel轻质成分,这些成分往往不能很好地混合)

Instead, try extending from JFrameinstead...

相反,尝试从而JFrame不是扩展...

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

public class Main extends JFrame {

    public Main() {
        add(new JPanel());
        setName("NGame");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Now, having said that. It's generally not necessary to extend from JFrameas you rarely add any new functionality to it.

现在,话虽如此。通常不需要扩展,JFrame因为您很少向它添加任何新功能。

Instead, you should create an instance of JFrameand add your components to it, for example...

相反,您应该创建一个实例JFrame并将您的组件添加到其中,例如...

package main;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {
        public MainPane() {
            add(new Panel());
            setName("NGame");
        }
    }

}

回答by HydroFall

Right click on src, then build path, libraries, and remove the JRE System Library, add libraries and add it again. This helped it for me at least =)

右键src,然后build path,libraries,去掉JRE System Library,添加libraries再添加。这至少对我有帮助 =)