当我在 ubuntu 中运行此代码时,窗口被最小化。当我点击它的图标时,它保持最小化。
但它在 Windows 中运行良好。
这是我的代码。
import javax.swing.*;
public class FrameTest
{
public static void main(String args[])
{
JPanel p = new JPanel();
JButton b = new JButton("Button 1");
p.add(b);
JFrame f = new JFrame("TestFrame");
f.add(p);
f.setVisible(true);
f.setSize(500,500);
}
}
请帮忙。
最佳答案
把 f.setSize(500, 500);
JFrame f = new JFrame("TestFrame");
之间和 f.add(p);
.您不能调用f.setVisible(true);
在完全构建和初始化 JFrame 之前。我还建议在 JPanel/JFrame 中使用 LayoutManager,例如 BorderLayout。
import java.awt.BorderLayout;
import javax.swing.*;
public class FrameTest
{
public static void main(String args[])
{
JPanel p = new JPanel();
BorderLayout bl = new BorderLayout(0, 0);
p.setLayout(bl);
JButton b = new JButton("Button 1");
p.add(b, BorderLayout.CENTER);
JFrame f = new JFrame("TestFrame");
f.setSize(500,500);
f.getContentPane().add(p);
f.setVisible(true);
}
}
关于java - JFrame 没有出现在 ubuntu 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977194/