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
How to import JFrame?
提问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 Main
extend 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...
几件事...
- You are extending from
JPanel
, but not importing it. - You are importing
JFrame
, but not actually using it. setDefaultCloseOperation
is not a valid method forJPanel
EXIT_ON_CLOSE
is an unknown variable as it's not declared withinJPanel
or any of it's parent classes...- You are mixing heavy and light weight components (
Panel
is a heavy weight component andJPanel
is a light weight component, these components tend not to mix well)
- 您是从 扩展
JPanel
,但不是导入它。 - 您正在导入
JFrame
,但实际上并未使用它。 setDefaultCloseOperation
不是有效的方法JPanel
EXIT_ON_CLOSE
是一个未知变量,因为它没有在JPanel
其父类中或其任何父类中声明...- 您正在混合重质和轻质成分(
Panel
是重质成分和JPanel
轻质成分,这些成分往往不能很好地混合)
Instead, try extending from JFrame
instead...
相反,尝试从而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 JFrame
as you rarely add any new functionality to it.
现在,话虽如此。通常不需要扩展,JFrame
因为您很少向它添加任何新功能。
Instead, you should create an instance of JFrame
and 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再添加。这至少对我有帮助 =)