我有一个jScrollbar,当我滚动它时(它的值从0到100),我想在textfield中显示这个值。这是如何从jScrollBar中获取值
AdjustmentListener adjListener;
adjListener = new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent evt) {
System.out.println(evt.getValue());
}
};但是我不能把它放在输入中,因为我得到了cannot make static reference to non-static错误。
任何帮助都将不胜感激!
发布于 2017-01-08 17:40:56
您可以在范围属性或类属性中使用变量之间进行选择。
public class Main extends JFrame {
// Attibute version
// private final JTextField textfield = new JTextField( "0000" );
Main() {
super( "Hello, scrollbars!" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ));
// this variable may be defined as attribute
final JTextField textfield = new JTextField( "0000" );
add( textfield );
final JScrollPane scrollPane =
new JScrollPane(
new JList<>(
new String[]{
"Hello", "Scrollbars",
"Hello", "Scrollbars",
"Hello", "Scrollbars",
"Hello", "Scrollbars",
"Hello", "Scrollbars",
}));
scrollPane.getVerticalScrollBar().addAdjustmentListener(
e -> textfield.setText( String.format( "%04d", e.getValue())));
add( scrollPane );
pack();
setLocationRelativeTo( null );
setVisible( true );
}
public static void main( String[] args ) {
new Main();
}
}https://stackoverflow.com/questions/41535387
复制相似问题