Scrollbar scrollbar = new Scrollbar(); scrollbar.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent evt) { label.setText(String.valueOf(scrollbar.getValue())); } });
package com.java.myapp; import java.awt.EventQueue; import java.awt.Frame; import java.awt.Label; import java.awt.Scrollbar; import java.awt.event.WindowEvent; import java.awt.event.AdjustmentListener; import java.awt.event.AdjustmentEvent; public class MyForm extends Frame { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm form = new MyForm(); form.setVisible(true); } }); } public MyForm() { // Create Form Frame super("ThaiCreate.Com Java GUI Tutorial"); setSize(434, 285); setLocation(500, 280); setLayout(null); // Label final Label label = new Label(); label.setAlignment(java.awt.Label.CENTER); label.setText("Result"); label.setBounds(140, 150, 150, 20); add(label); // Scrollbar final Scrollbar scrollbar = new Scrollbar(); scrollbar.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent evt) { label.setText(String.valueOf(scrollbar.getValue())); } }); scrollbar.setOrientation(Scrollbar.HORIZONTAL); scrollbar.setMaximum(0); scrollbar.setMaximum(100); scrollbar.setBounds(110, 100, 200, 16); add(scrollbar); // Close addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท