gpt4 book ai didi

java - 使用图像制作自定义 JScrollBar

转载 作者:搜寻专家 更新时间:2023-11-01 01:06:40 26 4
gpt4 key购买 nike

所以我使用 Java Swing 为我的应用程序构建 UI,并使用自定义图像来替换丑陋的 Java 图像,自定义图像具有样式并且很容易集成到 Java Swing 中。

现在我的问题是我需要在应用程序中使用 JScrollBarJScrollPane 而我真的不想使用默认的 Java 滚动条或甚至 native 操作系统滚动条。

我只是希望能够将自定义图像作为滚动条的背景和一个图像作为滚动条的缩略图。

如何使用图片制作自定义的JScrollBar?

最佳答案

我写了一个示例,展示了如何为滚动条的缩略图和背景(称为轨道)设置自定义图像。您需要两个文件 thumb.pngtrack.png 用于自定义图像,它们位于您的类文件所在的同一位置。我还对图像进行了一些缩放以适合滚动条。只需用这段代码试验一下。我更改了滚动条的宽度 (setPreferredSize) 以更好地查看图像。

要点是您必须创建自己的类 MyUi 扩展 BasicScrollBarUI 并覆盖 paintThumbpaintTrack,并使用 setUI(new MyUI) 个性化您的滚动条:

import java.awt.*;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;
import javax.swing.plaf.metal.MetalScrollBarUI;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.geom.AffineTransform;


public class CustomScrollbarUIExample {
public static void main(String[] args) {
JScrollPane before = makeExamplePane();
JScrollPane after = makeExamplePane();

JScrollBar sb=after.getVerticalScrollBar();
sb.setPreferredSize(new Dimension(50, Integer.MAX_VALUE));
sb.setUI(new MyScrollbarUI());

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = f.getContentPane();
c.setLayout(new GridLayout(2, 1, 0, 1));
c.add(before);
c.add(after);
f.setSize(450, 400);
f.setVisible(true);
}

private static JScrollPane makeExamplePane() {
String exampleText= "Lorem ipsum dolor sit amet,\n consetetur sadipscing elitr,\n sed diam nonumy eirmod \ntempor invidunt ut labore et dolore \nmagna aliquyam erat,\n sed diam voluptua. At vero eos et accusam et \njusto duo dolores et ea rebum. Stet clita\n kasd gubergren, no sea\n takimata sanctus est Lorem ipsum dolor sit amet.\n Lorem ipsum dolor sit amet,\n consetetur sadipscing elitr, sed diam\n nonumy eirmod tempor invidunt \nut labore et dolore\n magna aliquyam erat, sed diam voluptua.\n At vero eos et accusam et justo \nduo\n dolores et ea rebum. Stet clita kasd gubergren, no sea\n takimata sanctus est Lorem\n ipsum dolor sit amet. Lorem ipsum dolor\n sit amet, consetetur sadipscing elitr,\n sed diam nonumy\n eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos\n et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est \nLorem ipsum dolor sit amet.Duis\n autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel\n illum dolore eu feugiat nulla facilisis at vero eros et \naccumsan et iusto odio \ndignissim qui blandit praesent luptatum zzril delenit augue\n duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer\n adipiscing elit, sed diam nonummy nibh euismod \ntincidunt ut laoreet\n dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam,\n quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea\n commodo consequat. Duis autem vel eum iriure \ndolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla \nfacilisis at vero eros et accumsan et iusto odio dignissim qui blandit\n praesent luptatum zzril delenit augue duis dolore \nte feugait nulla facilisi.";
JTextArea text = new JTextArea(exampleText);
JScrollPane scroll = new JScrollPane(text);
return scroll;
}

static class MyScrollbarUI extends MetalScrollBarUI {
private Image imageThumb, imageTrack;
MyScrollbarUI() {
try {
imageThumb = ImageIO.read(new File("thumb.png"));
imageTrack = ImageIO.read(new File("track.png"));
} catch (IOException e){}
}

@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
g.translate(thumbBounds.x, thumbBounds.y);
g.setColor( Color.red );
g.drawRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 );
AffineTransform transform = AffineTransform.getScaleInstance((double)thumbBounds.width/imageThumb.getWidth(null),(double)thumbBounds.height/imageThumb.getHeight(null));
((Graphics2D)g).drawImage(imageThumb, transform, null);
g.translate( -thumbBounds.x, -thumbBounds.y );
}

@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
g.translate(trackBounds.x, trackBounds.y);
((Graphics2D)g).drawImage(imageTrack,AffineTransform.getScaleInstance(1,(double)trackBounds.height/imageTrack.getHeight(null)),null);
g.translate( -trackBounds.x, -trackBounds.y );
}

}
}

有关详细信息,请参阅源文件 MetalScrollBarUI.java

关于java - 使用图像制作自定义 JScrollBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12265740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com