【问题标题】:How to add few the same image in one program by using JLabel?如何使用 JLabel 在一个程序中添加几个相同的图像?
【发布时间】:2014-05-17 18:12:22
【问题描述】:

我正在使用 Swing,我正在尝试在程序中添加一些图片。

field = new JFormattedTextField(formatter);

ImageIcon icon = new ImageIcon("background.png"),
          icon1 = new ImageIcon("1.png");

JLabel background = new JLabel(icon); 
JLabel firstIcon = new JLabel(icon1);

JPanel center = new JPanel(new GridLayout(0, 1));


    public void initComponents() {
          this.getContentPane().add(center, BorderLayout.CENTER);

center.add(background);

field.setBounds(50,50);
background.add(field);
background.add(fristIcon);
}

使用这段代码一切正常,但是当我尝试添加相同的图片时“background.add(fristIcon);”再次,我没有看到先添加的图像。每个新图像都在删除最后一个图标。

【问题讨论】:

  • 你在这里做什么background.add(fristIcon);
  • @Braj:阅读 GridLayout API 以了解这意味着什么(可变行数)。
  • background.add(fristIcon); - 我想在图标上添加图标。我写 0 是因为我认为如果程序需要更多空间,它只会变得更大。

标签: java swing layout jlabel null-layout-manager


【解决方案1】:

background 是一个 JLabel,您通常不会将一个 JLabel 添加到另一个 JLabel。但是,如果您必须这样做,请务必为充当容器的 JLabel 提供一个体面的布局管理器,以便它可以以智能的方式显示添加的组件。默认情况下,JLabels 没有布局(空布局),添加的任何组件都需要指定其大小和要显示的位置。虽然你可以这样做——指定添加的所有组件的边界,但我建议你不要这样做,因为这会导致非常不灵活的 GUI,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕上看起来很糟糕决议,并且很难更新和维护。相反,您需要学习和学习布局管理器,然后嵌套 JPanel 或其他组件,每个组件都使用自己的布局管理器来创建在所有操作系统上看起来都不错的令人愉悦且复杂的 GUI。

考虑使用基本的 FlowLayout 来了解我的意思:

background.setLayout(new FlowLayout());

请注意

【讨论】:

  • 如果我使用 FlowLayout 设置背景,它将作为我的新面板工作吗?坐标(0,0)将从图片的开头开始?
【解决方案2】:

我想在图标上添加图标。

这是使用Graphics.drawImage() 在覆盖paintComponent() 方法中JLabel 的现有Graphics 中绘制图像的代码。

有关更多信息,请阅读内联 cmets。

示例代码:

// back ground image
URL url1 = new URL(
        "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQu3FFM1kR-aeYFjJIqebgusVZNM6uG-a0F4Z_0IPopEqfSZwzBBA");
final BufferedImage bg = ImageIO.read(url1);

// foreground image
URL url2 = new URL("https://cdn1.iconfinder.com/data/icons/supermariopack/Mario.png");
final BufferedImage fg = ImageIO.read(url2);

// re size the image
final BufferedImage scaled = new BufferedImage(fg.getWidth() / 2, fg.getHeight() / 2,
        BufferedImage.TYPE_INT_RGB);
Graphics g = scaled.getGraphics();
g.drawImage(fg, 0, 0, scaled.getWidth(), scaled.getHeight(), null);
g.dispose();

// create a JLabel with back ground image
final JLabel label = new JLabel(new ImageIcon(bg)) {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // draw the foreground image
        g.drawImage(scaled, 50, 50, null);
    }
};

JOptionPane.showMessageDialog(null, label);

以上代码为本帖How to draw an image over another image?修改后的代码。

截图:

【讨论】:

    【解决方案3】:

    你不能多次添加同一个组件。

    我建议你这样做:

    for(int i=0;i<N;i++){
       JLabel img=new JLabel(icon1);
       switch(i){
       case 0:
          img.setBounds(x,y,w,h);
          break;
       case 1:
          img.setBounds(x,y,w,h);
          break;
       default:
          break;
       }
       background.add();
    }
    

    N 等于要显示的图标数

    但不建议将 JLabel 作为容器。为此,我建议您使用 JPanel 作为标签的容器

    【讨论】:

    • 我使用了 JPanel,但在那里我添加了 JLabel 背景,然后我遇到了问题,当我在同一个 JPanel 上添加其他东西时,它会向左或向右移动。
    • 这个话题应该回答你的问题:Centering a JLabel on a JPanel
    • 但是如果我使用“for”,我可以设置图像应该放置的位置?
    • 你可以使用变量 i 来管理不同的元素(看我的回答;))
    • "But JLabel is not a container" -- 你 100% 确定这个说法吗?如果您检查 JLabel API,您会看到它继承自 Container,这意味着它将作为容器正常运行。您是否尝试将其用作 GUI 中的容器?我有,很多次,正如预期的那样,如果做得好,它工作得很好。做对了,这意味着与大多数容器一样,应该给出适当的布局。抱歉,但为了本网站的未来访问者的利益,我必须给你的答案 -1 票,直到你纠正有问题的陈述。完成更正后请通知我。
    猜你喜欢
    • 2012-10-09
    • 1970-01-01
    • 2016-08-06
    • 2017-06-24
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2021-12-11
    相关资源
    最近更新 更多