【发布时间】:2011-06-09 19:52:40
【问题描述】:
首先我承认我在使用 Swing 编写 GUI 方面非常陌生,所以请记住这一点。
我制作了一个 GUI,它由顶部的 JPanel 组成,它应该显示图像,底部的一个包含几个组成聊天的 GUI 组件。我已经处理了所有聊天(尽管欢迎您指出错误和改进)。我现在关心的是上部 JPanel 中图像的处理。
我的理解是,在处理图像时,将 JPanel 扩展为一个将图像绘制到 GUI 的新类是明智的。我已经根据我在这个网站上找到的其他答案编写了这样一个类:
class ImgPanel extends JPanel
{
private URL rUrl;
private BufferedImage img;
public ImgPanel(String filename) {
super();
try {
rUrl = getClass().getResource(filename);
if (rUrl != null) {
img = ImageIO.read(rUrl);
}
} catch (IOException ex) {
System.out.println("Couldn't find image file: " + filename);
}
}
@Override
protected void paintComponent(Graphics g) {
this.setSize(240, 320); //If I do not set the size of the ImgPanel manually
//it for some reason gets the dimension (10, 10) and
//all the images are shrunk to fit it. Without this
//line though, the images do not stack and are
//displayed as would be expected from flowLayout.
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
从上面可以看出,我已经评论了我怀疑是问题的部分。我的 GUI 类如下所示:
public class ClientGUI extends JFrame{
private JTextArea chatwindow;
private JList users;
private JTextField enterChat;
private JPanel draftMonitor;
private JPanel chatMonitor;
private JPanel chatLeft;
private DefaultListModel listModel;
public ClientGUI(){
super("Client");
setLayout(new BorderLayout());
chatwindow = new JTextArea();
chatwindow.setEditable(false);
chatwindow.setRows(15);
chatwindow.setWrapStyleWord(true);
DefaultCaret caret = (DefaultCaret)chatwindow.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
listModel = new DefaultListModel();
users = new JList(listModel);
users.setVisibleRowCount(15);
users.setPrototypeCellValue("AAAAAAAAAAAAAAAAAAAA");
//The above line is what I used to set the width of the JList,
//it has nothing to do with the question at hand, but I know
//there must be plenty of ways to improve this rather crude
//piece of coding.
enterChat = new JTextField();
chatLeft = new JPanel();
chatLeft.setLayout(new BorderLayout());
chatLeft.add(enterChat,"South");
chatLeft.add(new JScrollPane(chatwindow),"Center");
chatMonitor = new JPanel();
chatMonitor.setLayout(new BorderLayout());
chatMonitor.add(new JScrollPane(users),"East");
chatMonitor.add(chatLeft,"Center");
draftMonitor = new JPanel();
draftMonitor.setLayout(new FlowLayout());
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
draftMonitor.add(new ImgPanel("exampleimage.jpg"));
getContentPane().add(chatMonitor,"South");
getContentPane().add(draftMonitor,"Center");
}
public JTextArea getChatWindow(){
return chatwindow;
}
public JTextField getEnterChat(){
return enterChat;
}
public JList getUsers(){
return users;
}
public DefaultListModel getListModel(){
return listModel;
}
}
这会生成示例图像的九个副本,每个副本都比另一个稍微靠右(十像素?),但堆叠在一起,最左边的一个在顶部。它们没有居中,略微偏右(明显如此)。
没有 ImgPanel 类中的可疑行,它会产生我所期望的,九个图像在一个间隔的行中,居中。但是它们比源图像的实际大小要小得多。
感谢我能得到的所有帮助,这个 GUI 编码是一项棘手的工作!
【问题讨论】:
-
这个问题需要sscce。
-
感谢trashgod,这是我第一次不得不求助于帮助,所以我不确定要减少多少代码。虽然保持简短和简单是有道理的,但我将来一定会记住它。
标签: java image swing user-interface jpanel