我对java swing很新。
我想做一个类照片显示这样的事情:
这是一张照片
这是一个描述
我不知道我是否应该让我的班级Photo延伸JPanel或者什么。 另外我不知道是否使用什么布局。
如果我有可能使用HTML来显示标题,图片和描述,那就太棒了。
感谢您的任何建议。
注意:从另一个类(JFrame)我将添加此Photo类。
答案 0 :(得分:1)
我将这个Java Swing应用程序编写为单选按钮示例,但它显示带有标题和描述的图像,因此它可以执行您想要的操作。这些图像来自互联网,因此您无需任何修改即可运行代码,但有些图像可能会随着时间的推移而消失。
这是GUI的屏幕截图。
图像信息保存在列表中。 ImageInformation类是一个模型类。
图像面板是在方法中创建的,而不是在类中创建的。你可以把方法拉成一个类,但它对于一个方法来说很简单。
这是代码。
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class ImageDisplay implements Runnable {
private ItemListener listener;
private JFrame frame;
private JLabel titleLabel;
private JLabel imageLabel;
private JLabel descriptionLabel;
private List<ImageInformation> images;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ImageDisplay());
}
public ImageDisplay() {
this.images = setImageInformation();
}
@Override
public void run() {
frame = new JFrame("Image Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createControlPanel(), BorderLayout.WEST);
frame.add(createImagePanel(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private JPanel createImagePanel() {
JPanel imagePanel = new JPanel();
imagePanel.setLayout(new BorderLayout());
ImageInformation defaultImageInformation = images.get(0);
titleLabel = new JLabel(defaultImageInformation.getTitle());
titleLabel.setHorizontalAlignment(JLabel.CENTER);
imagePanel.add(titleLabel, BorderLayout.NORTH);
imageLabel = new JLabel(new ImageIcon(
defaultImageInformation.getImage()));
JScrollPane scrollPane = new JScrollPane(imageLabel);
imagePanel.add(scrollPane, BorderLayout.CENTER);
descriptionLabel = new JLabel(defaultImageInformation.getDescription());
descriptionLabel.setHorizontalAlignment(JLabel.CENTER);
imagePanel.add(descriptionLabel, BorderLayout.SOUTH);
return imagePanel;
}
private JPanel createControlPanel() {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(new EtchedBorder(), "Images"));
panel.setLayout(new GridLayout(0, 1));
ButtonGroup group = new ButtonGroup();
listener = new ImageListener();
for (int i = 0; i < images.size(); i++) {
ImageInformation imageInformation = images.get(i);
JRadioButton button = new JRadioButton(imageInformation.getTitle());
if (i == 0) {
button.setSelected(true);
}
button.addItemListener(listener);
group.add(button);
panel.add(button);
}
return panel;
}
private List<ImageInformation> setImageInformation() {
List<ImageInformation> images = new ArrayList<ImageInformation>();
// Here, you would get your images
Image image1 = getImage("http://4.bp.blogspot.com/-vfRL5DamWFs/"
+ "T2nn6D_EUfI/AAAAAAAABB8/Kc9Y33qYWJo/s1600/People-Power.jpg");
Image image2 = getImage("http://www.jeffjonesillustration.com/images/"
+ "illustration/00601-group-of-people.jpg");
Image image3 = getImage("http://img1.loadtr.com/b-404076-People.jpg");
Image image4 = getImage("http://www.druginfo.sl.nsw.gov.au/images/teens.jpg");
Image image5 = getImage("http://www.pesconsulting.co.uk/wp-content/uploads/"
+ "2013/03/kevin-thom-2010-people-collage.jpg");
Image image6 = getImage("http://www.emcdda.europa.eu/attachements.cfm/"
+ "att_77302_EN_young-people-480px.jpg");
images.add(new ImageInformation(image1, "Image 1",
"Image 1 Description"));
images.add(new ImageInformation(image2, "Image 2",
"Image 2 Description"));
images.add(new ImageInformation(image3, "Image 3",
"Image 3 Description"));
images.add(new ImageInformation(image4, "Image 4",
"Image 4 Description"));
images.add(new ImageInformation(image5, "Image 5",
"Image 5 Description"));
images.add(new ImageInformation(image6, "Image 6",
"Image 6 Description"));
return images;
}
private Image getImage(String fileName) {
try {
return ImageIO.read(new URL(fileName));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private ImageInformation getImageInformation(String title) {
for (ImageInformation imageInformation : images) {
if (title.equals(imageInformation.getTitle())) {
return imageInformation;
}
}
return null;
}
public class ImageListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
JRadioButton button = (JRadioButton) event.getItem();
String labelText = button.getText();
ImageInformation imageInformation = getImageInformation(labelText);
if (imageInformation != null) {
titleLabel.setText(imageInformation.getTitle());
imageLabel.setIcon(new ImageIcon(imageInformation
.getImage()));
descriptionLabel.setText(imageInformation.getDescription());
}
}
}
}
public class ImageInformation {
private final Image image;
private final String title;
private final String description;
public ImageInformation(Image image, String title, String description) {
this.image = image;
this.title = title;
this.description = description;
}
public Image getImage() {
return image;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
}
}
答案 1 :(得分:0)
如果你想让它成为纯粹的摇摆,你应该自己开发它。请参阅并测试以下示例代码:
pid
2 | 4 | 9 | 11 |
---+-----+-----+----+
10 | 30 | 0 | 90 |
10 | 40 | 0 | 0 |
0 | 50 | 0 | 0 |
0 | 60 | 0 | 10 |
0 | 70 | 0 | 0 |
20 | 0 | 60 | 0 |
0 | 0 | 0 | 20 |
使用上面的示例代码:
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Photo extends JPanel{
private static final long serialVersionUID = 1L;
private String title;
private ImageIcon image;
private String description;
private JLabel titleLabel;
private JLabel imageLabel;
private JLabel descLabel;
public Photo(String title, ImageIcon image, String description) {
setLayout(new BorderLayout(5,5));
//
setBorder(BorderFactory.createLoweredSoftBevelBorder());
//
this.title = title;
this.titleLabel = new JLabel(title);
Font f = this.titleLabel.getFont();
f = f.deriveFont(Font.BOLD);
f = f.deriveFont(18f);
this.titleLabel.setFont(f);
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.add(titleLabel, BorderLayout.CENTER);
this.add(northPanel, BorderLayout.NORTH);
//
this.image = image;
this.imageLabel = new JLabel(image);
this.add(imageLabel, BorderLayout.CENTER);
//
this.description = description;
this.descLabel = new JLabel(description);
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(descLabel, BorderLayout.CENTER);
this.add(southPanel, BorderLayout.SOUTH);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
repaint();
}
public ImageIcon getImage() {
return image;
}
public void setImage(ImageIcon image) {
this.image = image;
repaint();
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
repaint();
}
}
祝你好运。