admin管理员组

文章数量:1291736

The panel is showing up blank and even when I setContentPane(null), the words on the panel wont show up how I want them to with set Bounds.

I tried changing the order of my configurations for the JFrame as well and setContentPane to null.

    //Create button group
    ButtonGroup group = new ButtonGroup();
    group.add(kendrick);
    group.add(drake);
    group.add(kanye);
    group.add(cudi);

    //Create soup panel
    buttonPanel.add(kendrick);
    buttonPanel.add(drake);
    buttonPanel.add(kanye);
    buttonPanel.add(cudi);

    // Create main panel
    panel.add(buttonPanel);

    //Set Bounds
    kendrick.setBounds(20,200,30,50);
    
    //Configure JFrame
    panel.setLayout(null);
    setContentPane(panel);
    setTitle("Album Covers");
    setSize(600,400);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

The panel is showing up blank and even when I setContentPane(null), the words on the panel wont show up how I want them to with set Bounds.

I tried changing the order of my configurations for the JFrame as well and setContentPane to null.

    //Create button group
    ButtonGroup group = new ButtonGroup();
    group.add(kendrick);
    group.add(drake);
    group.add(kanye);
    group.add(cudi);

    //Create soup panel
    buttonPanel.add(kendrick);
    buttonPanel.add(drake);
    buttonPanel.add(kanye);
    buttonPanel.add(cudi);

    // Create main panel
    panel.add(buttonPanel);

    //Set Bounds
    kendrick.setBounds(20,200,30,50);
    
    //Configure JFrame
    panel.setLayout(null);
    setContentPane(panel);
    setTitle("Album Covers");
    setSize(600,400);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
Share Improve this question edited Feb 13 at 12:45 Aejt asked Feb 13 at 12:45 AejtAejt 12 bronze badges 3
  • 4 Avoid null layouts in Swing – g00se Commented Feb 13 at 12:46
  • The issue seems to occur when layout managers are mixed with absolute positioning. When you use setLayout(null) for absolute positioning with setBounds(), you must: 1. Set the Layout to null before adding the components. 2. Set bounds for all components, not just for one. 2. Also, set bounds for container panels. Try this and see if it works. – Prashant Kumar Commented Feb 13 at 13:00
  • 4 Or ... employ your efforts much more productively by learning how to use layout managers to your advantage instead of fighting against them – g00se Commented Feb 13 at 13:25
Add a comment  | 

1 Answer 1

Reset to default 1

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

Swing uses layout managers to lay the Swing components out. Generally, for a group of JRadioButtons, the GridLayout is the most appropriate.

Generally, you place Swing components on one or more JPanels and place the JPanels on the JFrame. The JFrame has a default BorderLayout.

The code posted in the question was incomplete. Here's the GUI I created.

And here's the complete, runnable code.

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class ExampleButtonPanel implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ExampleButtonPanel());

    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Album Covers");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createMainPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 100, 20, 100));
        ButtonGroup group = new ButtonGroup();

        JRadioButton kendrick = new JRadioButton("Kendrick");
        group.add(kendrick);
        panel.add(kendrick);

        JRadioButton drake = new JRadioButton("Drake");
        group.add(drake);
        panel.add(drake);

        JRadioButton kanye = new JRadioButton("Kanye");
        group.add(kanye);
        panel.add(kanye);

        JRadioButton cudi = new JRadioButton("Cudi");
        group.add(cudi);
        panel.add(cudi);

        return panel;
    }

}

本文标签: javaThe content will not appear how I want it to with setBoundsStack Overflow