JPanel 未显示
为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜 JPanel 不能是顶级容器。必须将其放入 JFrame 或 JWindow 中才能显示
I guess JPanel cannot be a toplevel container. It has to be put inside a JFrame or JWindow to be shown
面板只是不显示在 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.
JPanel 不是顶级容器。您需要将该 JPanel 放置在 JDialog 或 JFrame 中。确保将其添加到该对话框或框架的内容窗格中:
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:
我遇到一个问题,JPanels 不会出现在我的框架中。我认为这可能是 JFrame 子类中属性的顺序。因此,虽然不是对所提供代码的直接答案,但它可能会帮助其他人解决这个问题:
—————-
————
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:
————-
————