从 JScrollPane 中的 JScrollBar 中删除箭头按钮



JButton:

public class noButton extends JButton
{
//tried getPreferredSize() and getSize() too
@Override
public Dimension getMaximumSize() 
{          
return new Dimension();
}
}

自定义用户界面:

public class barUI extends BasicScrollBarUI {
barUI()
{
}
protected JButton createIncreaseButton(int i) {
return new noButton();
}
protected JButton createDecreaseButton(int i){
return new noButton();
}
@Override
protected void installComponents() {
switch(this.scrollbar.getOrientation()) {
case 0:
if (this.scrollbar.getComponentOrientation().isLeftToRight()) {
this.incrButton = this.createIncreaseButton(3);
this.decrButton = this.createDecreaseButton(7);
} else {
this.incrButton = this.createIncreaseButton(7);
this.decrButton = this.createDecreaseButton(3);
}
break;
case 1:
this.incrButton = this.createIncreaseButton(5);
this.decrButton = this.createDecreaseButton(1);
}
this.incrButton=new noButton();
this.decrButton=new noButton();
this.decrButton.setSize(0,0);
this.scrollbar.add(incrButton);
this.scrollbar.add(decrButton);
System.out.println(incrButton.getParent());
System.out.println(incrButton.getSize());
this.scrollbar.setEnabled(this.scrollbar.isEnabled());
}
}

滚动窗格:

scroll=new JScrollPane(){
@Override
public void updateUI(){
super.updateUI();
getVerticalScrollBar().setUI(new barUI());
getHorizontalScrollBar().setUI(new barUI());
}
};

我一直在尝试先删除一个按钮。上面的代码在各种线程中被建议,但它绝对没有做任何事情。我只是让默认外观按钮按vertical.setPreferredSize(new Dimension(10,10));调整大小

还有另一个建议是覆盖添加按钮并删除相关行的方法......这也什么也没做。

更新:在问题中添加了"箭头"以澄清 更新:对代码的更改

还有另一个建议是重写添加按钮的方法并删除相关行。

似乎可以像这样工作正常:

import java.awt.*;
import java.util.Collections;
import javax.swing.*;
import javax.swing.plaf.metal.MetalScrollBarUI;
public class RemoveArrowButtonsTest {
private Component makeUI() {
UIManager.put("ScrollBar.incrementButtonGap", 0);
UIManager.put("ScrollBar.decrementButtonGap", 0);
String txt = String.join("n", Collections.nCopies(100, "1234567890"));
JPanel p = new JPanel(new GridLayout(1, 0));
p.add(new JScrollPane(new JTextArea(txt)));
p.add(new JScrollPane(new JTextArea(txt)) {
@Override public void updateUI() {
super.updateUI();
getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
}
});
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new RemoveArrowButtonsTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class ZeroSizeButton extends JButton {
@Override public Dimension getPreferredSize() {
return new Dimension();
}
}
class WithoutArrowButtonScrollBarUI extends MetalScrollBarUI {
@Override protected JButton createDecreaseButton(int orientation) {
return new ZeroSizeButton();
}
@Override protected JButton createIncreaseButton(int orientation) {
return new ZeroSizeButton();
}
}

相关内容

  • 没有找到相关文章

最新更新