java代码一行显示,如何在Java GUI中显示一行?

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?

in any case I want something along the lines of

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

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

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

Thanks a lot,

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

FjhmZ.png

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);

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值