如何在Java GUI中显示一行? [英] How do I display a line in a Java GUI?

查看:315
本文介绍了如何在Java GUI中显示一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java swing中画两个图像之间的一条线。

I would like to draw a line between 2 images in Java swing.

我找到了不同的绘制线条的方法,但没有我想要的方式我想也许我只需要在我想要的地方插入一行图像?

I have found different ways of drawing lines but none the way that I want to which makes me think maybe I just need to insert an image of a line where I want it?

无论如何我想要的东西

O---O---O---O

要显示'O是图像而破折号是我想要的行...

To be displayed where the 'O's are images and the dashes are where I want lines...

我希望这是明确的我不是白痴:P

I hope this is clear and I'm not being an idiot :P

非常感谢,

Alexander

Alexander

推荐答案

正如我在评论中指出的那样,你可以只绘制一条线,然后在设定的增量点上绘制线条上的图像

As I pointed out in my comment, you could just just draw a line, then draw the images over the line at set increment points

import java.awt.*;
import javax.swing.*;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final Image image = new ImageIcon(Test.class
                        .getResource("/resources/images/stackoverflow.png"))
                        .getImage();
                final BasicStroke stroke = new BasicStroke(5f);
                JPanel panel = new JPanel() {
                    @Override
                    protected void paintComponent(Graphics g) {
                        super.paintComponent(g);
                        Graphics2D g2 = (Graphics2D) g;
                        g2.setStroke(stroke);
                        g2.drawLine(10, 75, 290, 75);
                        for (int x = 10; x < 300; x += 50) {
                            g2.drawImage(image, x, 59, this);
                        }
                    }

                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(300, 150);
                    }
                };

                JOptionPane.showMessageDialog(null, panel, "Line With Images",
                        JOptionPane.PLAIN_MESSAGE);
            }
        });
    }
}

这篇关于如何在Java GUI中显示一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆