gpt4 book ai didi

java - 自定义滚动条箭头

转载 作者:行者123 更新时间:2023-11-30 04:11:09 25 4
gpt4 key购买 nike

我正在尝试使用下面的代码自定义 jScrollPane。它有效,它按照我想要的方式改变颜色,但隐藏箭头按钮。

我想要的是让它们再次可见并使用自定义图像更改它们。我尝试在这个论坛上搜索,但找不到任何相关信息。

我希望有人能帮助我。提前致谢!

    private Image imageThumb, imageTrack;
private JButton b = new JButton() {
@Override
public Dimension getPreferredSize() {
return new Dimension(0, 0);
}
};

public YourScrollbarUI () {
imageThumb = WrapImage .create(45, 45, new Color(46,218,163));
imageTrack = WrapImage .create(32, 32, new Color(90,90,90));
}

@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
g.setColor(Color.blue);
((Graphics2D) g).drawImage(imageThumb,
r.x, r.y, r.width, r.height, null);
}

@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
((Graphics2D) g).drawImage(imageTrack,
r.x, r.y, r.width, r.height, null);
}

@Override
protected JButton createDecreaseButton(int orientation) {
return b;
}

@Override
protected JButton createIncreaseButton(int orientation) {
return b;
}

private static class WrapImage {

static public Image create(int w, int h, Color c) {
BufferedImage bi = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setPaint(c);
g2d.fillRect(0, 0, w, h);
g2d.dispose();
return bi;}}

最佳答案

It changes the color the way I want it, but hides the arrowbuttons.

问题是这样的:

private JButton b = new JButton() {
@Override
public Dimension getPreferredSize() {
return new Dimension(0, 0); // why (0,0) ???
}
};

在代码中,b 按钮负责通过 createDecreaseButtoncreateIncreaseButton 方法绘制箭头。如果其首选大小为 (0,0) 那么从逻辑上讲它将不可见。

What I want is to make them visible again and change them with a custom image.

您需要修改 createDecreaseButtoncreateIncreaseButton 以使它们返回一个带有所需图标的新 JButton

更新

I already tried playing with the prefferedsize (making them the same size as the custom image), but the custom arrowimages are still not showing up. Im clueless

看看这个工作示例。 MyScrollbarUI 扩展自 BasicScrollBarUI就像你的类(class)一样。您会看到关键是重写按钮的 getPreferredSize() 方法并根据需要设置适当的图标。

在这方面我应该说BasicScrollBarUI.createDecreaseButton(int orientation)BasicScrollBarUI.createIncreaseButton(int orientation)方法的文档记录很少(没有 javadoc)。但是,如果您使用 IDE 深入研究此类,您将看到 orientation 参数可以采用以下值之一:SwingConstants.NORTH、SwingConstants.SOUTH、SwingConstants.EAST、SwingConstants.WEST。在查看 getAppropriateIcon(intorientation) 方法时请记住这一点。

这些是使用过的图标:Up arrow Down arrow Left arrow Right arrow

import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicScrollBarUI;

public class Demo {

private void initGUI(){
JScrollPane scrollPane = new JScrollPane(new JTextArea(10, 20));
scrollPane.getHorizontalScrollBar().setUI(new MyScrollbarUI());
scrollPane.getVerticalScrollBar().setUI(new MyScrollbarUI());

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().initGUI();
}
});
}

class MyScrollbarUI extends BasicScrollBarUI {

private ImageIcon downArrow, upArrow, leftArrow, rightArrow;

public MyScrollbarUI(){
try {
upArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-up-icon.png"));
downArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-down-icon.png"));
rightArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-right-icon.png"));
leftArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-left-icon.png"));
} catch (java.net.MalformedURLException ex) {
ex.printStackTrace();
}
}

@Override
protected JButton createDecreaseButton(int orientation) {
JButton decreaseButton = new JButton(getAppropriateIcon(orientation)){
@Override
public Dimension getPreferredSize() {
return new Dimension(22, 22);
}
};
return decreaseButton;
}

@Override
protected JButton createIncreaseButton(int orientation) {
JButton increaseButton = new JButton(getAppropriateIcon(orientation)){
@Override
public Dimension getPreferredSize() {
return new Dimension(22, 22);
}
};
return increaseButton;
}

private ImageIcon getAppropriateIcon(int orientation){
switch(orientation){
case SwingConstants.SOUTH: return downArrow;
case SwingConstants.NORTH: return upArrow;
case SwingConstants.EAST: return rightArrow;
default: return leftArrow;
}
}
}

}

屏幕截图

enter image description here

关于java - 自定义滚动条箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19577893/

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