在 Java 中设置框架
Setting up a frame in Java
我是 Java 的新手,我一直在尝试设置一个框架,但我的代码不起作用,要么是因为代码有误,要么是因为我的软件有问题。我正在使用 Eclipse。
所以这是我的代码:
package Frame;
import javax.swing.JFrame;
public class App {
class FrameApp extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("FirstFrame");
frame.setTitle("MFF");
frame.setSize(300, 700);
frame.setVisible(true);
}
}
}
它returns
The method main cannot be declared static; static methods can only be
declared in a static or top level type
问题是您在内部 class 中声明了 main 方法。这就是抛出的异常的意思。
这对你有用。
package Frame;
import javax.swing.JFrame;
class FrameApp extends JFrame{
public FrameApp(String name){
super(name);
}
}
public class App {
public static void main(String[] args) {
FrameApp frame = new FrameApp("FirstFrame");
frame.setTitle("MFF");
frame.setSize(300, 700);
frame.setVisible(true);
}
}
尽情享受吧! :)
你可以这样做,这不是扩展JFrame。
import javax.swing.JFrame;
public class App{
public App() {
JFrame frame = new JFrame("FirstFrame"); // This sets the title
frame.setTitle("MFF"); // Then you're resetting the title
frame.setSize(300, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new App();
}
}
Or you can extend it
import javax.swing.JFrame;
public class App extends JFrame{
public App() {
// When you extend JFrame you're essentially creating a JFrame
// This gets into OOP (which you of course need to learn)
// Since you've extended the JFrame you can directly access its methods
// like setTitle, setSize....
setTitle("MFF");
setSize(300, 700);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new App();
}
}
I'm definitely not very experienced with Swing, but when choosing one approach
over the other. I choose not to extend the Frame. And to answer your
question, there's no reason to put another class within the App class
when just creating this simple JFrame
that is why I changed to the
above.
我是 Java 的新手,我一直在尝试设置一个框架,但我的代码不起作用,要么是因为代码有误,要么是因为我的软件有问题。我正在使用 Eclipse。
所以这是我的代码:
package Frame;
import javax.swing.JFrame;
public class App {
class FrameApp extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("FirstFrame");
frame.setTitle("MFF");
frame.setSize(300, 700);
frame.setVisible(true);
}
}
}
它returns
The method main cannot be declared static; static methods can only be declared in a static or top level type
问题是您在内部 class 中声明了 main 方法。这就是抛出的异常的意思。 这对你有用。
package Frame;
import javax.swing.JFrame;
class FrameApp extends JFrame{
public FrameApp(String name){
super(name);
}
}
public class App {
public static void main(String[] args) {
FrameApp frame = new FrameApp("FirstFrame");
frame.setTitle("MFF");
frame.setSize(300, 700);
frame.setVisible(true);
}
}
尽情享受吧! :)
你可以这样做,这不是扩展JFrame。
import javax.swing.JFrame;
public class App{
public App() {
JFrame frame = new JFrame("FirstFrame"); // This sets the title
frame.setTitle("MFF"); // Then you're resetting the title
frame.setSize(300, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new App();
}
}
Or you can extend it
import javax.swing.JFrame;
public class App extends JFrame{
public App() {
// When you extend JFrame you're essentially creating a JFrame
// This gets into OOP (which you of course need to learn)
// Since you've extended the JFrame you can directly access its methods
// like setTitle, setSize....
setTitle("MFF");
setSize(300, 700);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new App();
}
}
I'm definitely not very experienced with Swing, but when choosing one approach over the other. I choose not to extend the Frame. And to answer your question, there's no reason to put another class within the App class when just creating this simple
JFrame
that is why I changed to the above.