下面的程式可以將文字框添加到捲軸中。
import java.awt.BorderLayout;<br />import java.awt.event.ActionEvent;<br />import java.awt.event.ActionListener;<br />import javax.swing.BoundedRangeModel;<br />import javax.swing.BoxLayout;<br />import javax.swing.JFrame;<br />import javax.swing.JPanel;<br />import javax.swing.JScrollBar;<br />import javax.swing.JTextField;<br />public class Main {<br /> public static void main(String[] args) {<br /> // TODO code application logic here<br /> JFrame frame = new JFrame("Text Slider");<br /> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br /> final JTextField textField = new JTextField();<br /> JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);<br /> JPanel panel = new JPanel();<br /> panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));<br /> BoundedRangeModel brm = textField.getHorizontalVisibility();<br /> scrollBar.setModel(brm);<br /> panel.add(textField);<br /> panel.add(scrollBar);<br /> // final TextSlider ts = new TextSlider();<br /> textField.addActionListener(new ActionListener(){<br /> public void actionPerformed(ActionEvent e){<br /> System.out.println("Text:"+textField.getText());<br /> }<br /> });<br /> frame.add(panel,BorderLayout.NORTH);<br /> frame.setSize(300,100);<br /> frame.setVisible(true);<br /> }<br />}
程式執行效果