How can I create an interface similar like the fol开发者_如何学Clowing in Java (tweetie)?
I was thinking of using a JTable with one columns and customized cell that has an image in it...not sure how to do it though.
If you are only going to have one column, than you can just use JList
and it will be a little easier. But to answer your question, you need to create a cell renderer that can be used to represent the object in the list. The renderer would have a method (getListCellRendererComponent
) which would return a Component
that can be used to represent each item.
The simplest way (I would do it) would be to use a Vertical BoxLayout
on a JPanel
. Each tweet would then be its own JPanel
(TweetPanel extends JPanel
) with a BorderLayout
where the image is on the WEST, and the tweet text is in the CENTER.
The following is how I would go about laying out one of the restaurant panels.
public ResturantPanel extends JPanel {
public ResturantPanel(String name, String address, List<String> 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);
}
}
Note, this was written entirely in this editor window. It will have some typos. Also, you will have to play with the sizing of the icon, the text areas added to the southReviewPanel
, and southReviewPanel
to get everything how you want it to look.
You would then place a bunch of these on a JPanel
in a JScrollPane
, and you are good to go.
精彩评论