最简单的方法(我会这样做)是在JPanel上使用Vertical BoxLayout.然后,每个推文将是其自己的JPanel(TweetPanel扩展了JPanel),带有BorderLayout,图像在WEST上,而推文文本在CENTER中.
以下是我如何布置餐厅面板之一的方法.
public ResturantPanel extends JPanel {
public ResturantPanel(String name, String address, List reviews, Icon icon){
setLayout(new BorderLayout());
JLabel iconLabel = new JLabel(theIcon);
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.setLayout(new BoxLayout(northPane, BoxLayout.Y_AXIS));
northPane.add(nameLabel);
northPane.add(addressLabel);
add(northPane, BorderLayout.North);
}
}
注意,这是完全在此编辑器窗口中编写的.它将有一些错别字.同样,您将不得不使用图标的大小,添加到southReviewPanel和southReviewPanel中的文本区域来获得想要的一切.
然后,将一堆这些放置在JScrollPane的JPanel上,就可以了.