[英]GUI doesn't show up on a Mac?
I bought a Mac, II download netbeans for my java. 我买了Mac,为我的Java下载了netbeans。
package gui;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
public void Gui(){
setTitle("Gui");
setSize(640,320);
setVisible(true);
}
public static void main(String[] args) {
new Gui();
}
}
It is very easy code and I didn't find any problem with it, but somehow the GUI is not showing up. 这是非常简单的代码,我没有发现任何问题,但是以某种方式未显示GUI。
is GUI no suppose to show up on a Mac? GUI是否不能显示在Mac上?
Somehow, the program didn't go through the Gui method, I tried 我以某种方式,该程序没有通过Gui方法
System.out.println("Hello");
didn't show up. 没有出现。
You think you're using a constructor but you are not! 您认为您正在使用构造函数,但事实并非如此! The constructor is what makes the app become a JFrame. 构造函数使应用程序成为JFrame。 This line: 这行:
public void Gui() {
should be: 应该:
public Gui() {
Also, nice to add a setMinimumSize(new Dimension(640,320));
另外,很高兴添加setMinimumSize(new Dimension(640,320));
I think the problem is you have a empty container, but I make an example for you: 我认为问题是您有一个空的容器,但我为您提供了一个示例:
import javax.swing.*;
public class Main {
private static JPanel panel1;
private static JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame( "Main");
panel1 = new JPanel();
button = new JButton("Button");
panel1.add(button);
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setTitle("Gui");
frame.setSize(640,320);
frame.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.