案例分析题 阅读下列程序,请写出该程序的功能。 import javax.swing.*; import java.awt.*; import java.awt.event.*; class MyScrollBar extends JScrollBar { public MyScrollBar(int init, int len, int low, int high){ super(JScrollBar.HORIZONTAL, init, len, low, high); } public Dimension getPreferredSize(){ return new Dimension(125, 20); } } class MyWindow extends JFrame implements AdjustmentListener{ private JTextField text; MyWindow(String s){ super(s); MyScrollBar tempBar = new MyScrollBar(l0, 10, 0, 255); Container con = this.getContentPane(); con.setLayout(new GridLayout(2,1)); this.setSize(250, 100); this.setLocation(100, 100); tempBar.addA djustmentListener(this); text= new JTextField("移动滚动条的滑块",20); con.add(text); con.add(tempBar); this.setVisible(true); this.pack(); } public void adjustmentValueChanged(AdjustmentEvent e){ MyScrollBar myBar=(MyScrO11Bar)e.getAdjustable(); text.setText("选择的值是:"+myBar.getValue()); } } public class Test35 { public static void main(String[] args) { new MyWindow("滚动条示意程序"); } }
【正确答案】拖动水平滚动条上的滑块,在文本框内显示滑块的对应值。
【答案解析】