gpt4 book ai didi

java - 带有自定义 LabelTable 的 JSlider 渲染

转载 作者:搜寻专家 更新时间:2023-10-31 19:47:32 26 4
gpt4 key购买 nike

我想使用 JSlider 来浏览一些按时间顺序排列的事件,因此我设置了一个自定义 LabelTable,以便它显示一些日期而不是默认的整数值。我的代码是这样的:

JSlider slider = new JSlider();
...
Date[] dates = getDates();
slider.setModel(new DefaultBoundedRangeModel(0, 0, 0, dates.length - 1));
Hashtable<Integer, JLabel> ht = new Hashtable<Integer, JLabel>();
for (int i = 0; i < dates.length; ++i) {
JLabel label = new JLabel(DateFormat.getDateInstance().format(dates[i]));
ht.put(i, label);
}
slider.setLabelTable(ht);
slider.setPaintLabels(true);
slider.setInverted(true);

如您所见,这工作正常: enter image description here

但是,如果我想更改 slider 的方向,请说 slider.setOrientation(JSlider.HORIZONTAL);,结果如下: enter image description here

如果我想每隔一个日期在 slider 上方/下方显示一个日期怎么办?另外,我可以在垂直 slider 上显示 slider 左侧的标签吗?

问候,雷米

最佳答案

更改标签面(简单地应用 RTL 方向):

public static void main ( String[] args )
{
try
{
UIManager.setLookAndFeel ( new NimbusLookAndFeel () );
}
catch ( UnsupportedLookAndFeelException e )
{
e.printStackTrace ();
}


JFrame frame = new JFrame ();

JSlider slider = new JSlider ( SwingConstants.VERTICAL );
slider.setPaintLabels ( true );
slider.setComponentOrientation ( ComponentOrientation.RIGHT_TO_LEFT );

Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel> ();
table.put ( 0, new JLabel ( "May 2, 2000" ) );
table.put ( 25, new JLabel ( "May 3, 2001" ) );
table.put ( 50, new JLabel ( "May 4, 2002" ) );
table.put ( 75, new JLabel ( "May 5, 2003" ) );
table.put ( 100, new JLabel ( "May 6, 2004" ) );
slider.setLabelTable ( table );

frame.add ( slider );

frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}

结果:

enter image description here

要改变标签位置,您必须手动修改它们。这可以通过以下方式完成:

public static void main ( String[] args )
{
try
{
UIManager.setLookAndFeel ( new NimbusLookAndFeel () );
}
catch ( UnsupportedLookAndFeelException e )
{
e.printStackTrace ();
}


JFrame frame = new JFrame ();
frame.getRootPane ().setBorder ( BorderFactory.createEmptyBorder ( 5, 5, 5, 5 ) );

JSlider slider = new JSlider ( SwingConstants.HORIZONTAL );
slider.setPaintLabels ( true );

Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel> ();
table.put ( 0, new JLabel ( "May 2, 2000" ) );
JLabel l2 = new JLabel ( "May 3, 2001" );
l2.setBorder ( BorderFactory.createEmptyBorder ( 20, 0, 0, 0 ) );
table.put ( 25, l2 );
table.put ( 50, new JLabel ( "May 4, 2002" ) );
JLabel l3 = new JLabel ( "May 5, 2003" );
l3.setBorder ( BorderFactory.createEmptyBorder ( 20, 0, 0, 0 ) );
table.put ( 75, l3 );
table.put ( 100, new JLabel ( "May 6, 2004" ) );
slider.setLabelTable ( table );

frame.add ( slider );

frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}

结果:

enter image description here

当然,您可以改进该代码,并在每次甚至运行的标签创建周期中根据之前的标签首选高度(取自首选大小)添加边框。

关于java - 带有自定义 LabelTable 的 JSlider 渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808473/

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