开发者

JPanel not showing up

开发者 https://www.devze.com 2023-01-25 21:29 出处:网络
Why is the UI not showing up in my code below: public class GUI extends JPanel{ public GUI(String name, String address, List<String> reviews, Icon icon){

Why is the UI not showing up in my code below:

public class GUI extends JPanel{

        public GUI(String name, String address, List<String> reviews, Icon icon){
            setSize(600,600);
            setLayout(new BorderLayout());
            JLabel iconLabel = new JLabel(icon);
            JLabel nameLabel = new JLabel(name);
            JLabel addressLabel = new JLabel(address);
            JPanel southReviewPanel = new JPanel();
    开发者_如何学Python        southReviewPanel.setLayout(new BoxLayout(southReviewPanel, BoxLayout.Y_AXIS));
            for (String review: reviews) {
                southReviewPanel.add(new JTextArea(review));
            }
            add(southReviewPanel);
            add(iconLabel, BorderLayout.WEST);
            JPanel northPane = new JPanel();
            northPane.add(nameLabel);
            northPane.add(addressLabel);
            add(northPane, BorderLayout.NORTH);
        }


    public static void main(String[] args) {
        ImageIcon ic = new ImageIcon();
        List<String> list = new ArrayList<String>();
        list.add("review1");
        list.add("review2");
        list.add("review3");
        list.add("review4");
        GUI test = new GUI("test", "test", list, ic);

          test.setVisible(true);

    }

}


I guess JPanel cannot be a toplevel container. It has to be put inside a JFrame or JWindow to be shown

JFrame f=new JFrame();
f.add(test);
f.setVisible(true);


Panels just don't show up in Swing. They have to be added to windows. Create JFrame or JDialog and add your panel to it.


A JPanel isn't a top level container. You need to place that JPanel in a JDialog or JFrame. Make sure to add it to the content pane of that dialog or frame:

JFrame f = new JFrame();
f.getContentPane().add(test);


There is an issue I had where JPanels wouldn't show up in my frame. I think it may have been the order of the properties in JFrame subclass. So while not a direct answer to the supplied code it may help others with this issue:

import javax.swing.*;
public class BasicGUIMain {
    public static void main(String[] args) {
        TestFrame frame = new TestFrame(); // Displays frame
    }

————-

import javax.swing.*;

public class TestFrame extends JFrame {
    public TestFrame() {
        setSize(500,500);
        setTitle("Test Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(400,0);
        setVisible(true);

        TestPanel panel = new TestPanel();
        add(panel);
     }
}

————

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestPanel extends JPanel {
    public TestPanel() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        add(Box.createVerticalGlue());

         //add label
        JLabel label = new JLabel("Hello I am label");
        label.setMaximumSize(label.getPreferredSize());
        add(label);
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(Box.createVerticalGlue());

        // add a button
        JButton testButton1 = new JButton("Button 1");
        testButton1.setMaximumSize(testButton1.getPreferredSize());
        add(testButton1);
        testButton1.addActionListener(new TestButtonAction());
        testButton1.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(Box.createVerticalGlue());
    }
    private class TestButtonAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
        System.out.println("Button Pressed");
        }
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消