2017-01-08 16 views
0

jScrollbar가 있습니다. 스크롤 할 때 (0에서 100까지의 값이 있습니다) 텍스트 필드에 값을 표시하고 싶습니다. 이하는 JScrollBar입력 값에 JScrollBar 값을 설정하십시오.

AdjustmentListener adjListener; 
adjListener = new AdjustmentListener() { 
    public void adjustmentValueChanged(AdjustmentEvent evt) { 
     System.out.println(evt.getValue()); 
    } 
}; 

에서 값을 얻을하지만 cannot make static reference to non-static 오류로 내가 입력에 넣을 수없는 방법이다.

도움이 될 것입니다!

답변

1

범위 또는 클래스 속성에서 변수를 사용할 수 있습니다.

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(); 
    } 
} 
+0

대단히 감사합니다. –

+0

당신을 진심으로 환영합니다! ;-) – Aubin