1
votes

Components are not displayed in my JFrame using Swing. Actually my aim is:

  1. Add Frame
  2. In the frame add panel
  3. Panel cantains 3 buttons

But it didn't show.

Here is my code

public class Panels
{
    JFrame frame;
    JPanel panel;
    private JButton addButton;
    private JButton modifyButton;
    private JButton deleteButton;

    Panels()
    {
        initGUI();
        launchFrame();
    }

    public void initGUI()
    {
        frame = new JFrame();
        panel = new JPanel();
        addButton = new JButton("Add");
        modifyButton = new JButton("Modify");
        deleteButton = new JButton("Delete");
    }

    public void launchFrame()
    {
        addButton.setBounds(130,50,225,25);
        addButton.setBounds(150,50,225,25);
        addButton.setBounds(170,50,225,25);
        addButton.setBounds(190,50,225,25);
        panel.add(addButton);
        panel.add(modifyButton);
        panel.add(deleteButton);
        panel.setLayout(null);
        panel.setBackground(Color.RED);

        frame.add(panel);
        frame.setTitle("My Frame with Panel");
        frame.setSize(600,400);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Here is main for calling Panels class

When run on main function Frame is shown without controllers (ie 3 buttons not shown)

public class Main
{
    public static void main(String[] args)
    {
        Panels obj_panel=new Panels();
    }
}
1
maybe try to call set layout null before setting bounds.Arvy
have any problem in this code?Symon Kt
1) Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or combinations of layout managers as well as layout padding and borders for white space. 2) Provide ASCII art or a simple drawing of how the GUI should appear at default size and (if resizable) with extra width/height.Andrew Thompson

1 Answers

3
votes

This is the main problem

frame.setLayout(null);

When you set the layout to null, that means that all of its components must have boundaries set. You try to add the panel, without any boundaries. You only set the boundaries for the buttons in the panel. If you remove the above line, it works.

Other Issues I'd really take a look at:

  • Don't use null layouts at all. Instead make use of layout managers, and let them handle the sizing and positioning for you. This results in a a much more manageable and flexible UI. Please take some time to learn the different layout managers. Start at Laying out Components Within a Container

  • All Swing apps should run on a special thread known as the Event Dispatch Thread (EDT). Please take some time to read Initial Threads to learn how you can accomplish this.

Here is a refactor (fixing the "Other issues") with no null layout, just using layout managers, margins, and borders, and the code in the main shows how to run the program on the Event Dispatch Thread

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                Panels obj_panel = new Panels();
            }
        });   
    }
}

class Panels {

    private JFrame frame;
    private JPanel panel;
    private JButton addButton;
    private JButton modifyButton;
    private JButton deleteButton;

    Panels() {
        initGUI();
        launchFrame();
    }

    private void initGUI() {
        frame = new JFrame();       // default layout manager is BorderLayout
        panel = new JPanel();       // default layout manager is FlowLayout
        addButton = new JButton("Add");
        modifyButton = new JButton("Modify");
        deleteButton = new JButton("Delete");
    }

    private void launchFrame() {
        JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 10, 10));
        buttonPanel.setBackground(Color.RED);
        buttonPanel.add(addButton);
        buttonPanel.add(modifyButton);
        // add margin to left and right of delete button
        // other buttons will follow suit because of GridLayout
        deleteButton.setMargin(new Insets(0, 50, 0, 50));
        buttonPanel.add(deleteButton);
        // create some space at the top for the buttonPanel
        buttonPanel.setBorder(new EmptyBorder(20, 0, 0, 0));

        panel.add(buttonPanel);
        panel.setBackground(Color.RED);

        frame.add(panel);
        frame.setTitle("My Frame with Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);   
    }
}