JPanel 未显示

发布于 2024-10-03 01:57:19 字数 1293 浏览 4 评论 0原文

为什么 UI 没有显示在我的下面的代码中:

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

    }

}

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

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

や莫失莫忘 2024-10-10 01:57:19

我猜 JPanel 不能是顶级容器。必须将其放入 JFrame 或 JWindow 中才能显示

JFrame f=new JFrame();
f.add(test);
f.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);
森林散布 2024-10-10 01:57:19

面板只是不显示在 Swing 中。必须将它们添加到窗口中。创建 JFrame 或 JDialog 并将面板添加到其中。

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.

初懵 2024-10-10 01:57:19

JPanel 不是顶级容器。您需要将该 JPanel 放置在 JDialog 或 JFrame 中。确保将其添加到该对话框或框架的内容窗格中:

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

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);
自找没趣 2024-10-10 01:57:19

我遇到一个问题,JPanels 不会出现在我的框架中。我认为这可能是 JFrame 子类中属性的顺序。因此,虽然不是对所提供代码的直接答案,但它可能会帮助其他人解决这个问题:

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

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");
        }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文