简体   繁体   中英

JFrame is not showing up in ubuntu

When I run this code in ubuntu, the window is minimized. When I click on it's icon, it stays minimized. But it's working fine in Windows.

Here's my code.

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);
    }
}

Please help.

Put f.setSize(500, 500); between JFrame f = new JFrame("TestFrame"); and f.add(p); . You can't call f.setVisible(true); before the JFrame is fully builded and initialized. Also I recommend to use a LayoutManager in a JPanel/JFrame such as 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM