【问题标题】:Images stacking on top of eachother despite flowlayout?尽管有flowlayout,图像仍然堆叠在一起?
【发布时间】: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


【解决方案1】:

处理图像的最佳方法是使用JLabelcreateImageIcon。处理图像的代码已经很多了,所以不要重新发明轮子。

【讨论】:

  • 感谢您的建议,我一直在玩弄 ImageIcons,现在它似乎可以工作了。非常感谢。
【解决方案2】:

这里有很多要评论的地方,但 JPanel 默认为小尺寸。这部分是由您使用的 LayoutManager 决定的。在这种情况下,通过不设置默认值或首选大小,它自己选择 10,10 像素。您需要指定尺寸,它不会根据图像的尺寸自行确定。

您可能想查看 Java SwingX 项目,它有一个 JXImagePanel,它的功能与您的 ImgPanel 基本相同。

我知道这只能解决部分问题,但就像垃圾神评论的那样,将其简化为更小的代码问题将有助于分别解决每个问题

【讨论】:

  • 感谢您的评论,但我想我会从 Devon_C_Miller 的建议开始,因为我是初学者,所以我会先做一些简单的事情。
猜你喜欢
  • 2015-03-21
  • 1970-01-01
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
相关资源
最近更新 更多