介绍

The interfaceAdjustmentListener is used for receiving adjustment events. The class that process adjustment events needs to implements this interface.

类声明

Following is the declaration for java.awt.event.AdjustmentListener interface:

  1. public interface AdjustmentListener
  2. extends EventListener

Interface methods

S.N.Method & Description
1void adjustmentValueChanged(AdjustmentEvent e) Invoked when the value of the adjustable has changed.

方法继承

This class inherits methods from the following interfaces:

  • java.awt.event.EventListener

AdjustmentListener Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >AwtListenerDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtListenerDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11.  
  12. public AwtListenerDemo(){
  13. prepareGUI();
  14. }
  15.  
  16. public static void main(String[] args){
  17. AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
  18. awtListenerDemo.showAdjustmentListenerDemo();
  19. }
  20.  
  21. private void prepareGUI(){
  22. mainFrame = new Frame("Java AWT Examples");
  23. mainFrame.setSize(400,400);
  24. mainFrame.setLayout(new GridLayout(3, 1));
  25. mainFrame.addWindowListener(new WindowAdapter() {
  26. public void windowClosing(WindowEvent windowEvent){
  27. System.exit(0);
  28. }
  29. });
  30.  
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showAdjustmentListenerDemo(){
  47. headerLabel.setText("Listener in action: AdjustmentListener");
  48.  
  49. ScrollPane panel = new ScrollPane();
  50. panel.setBackground(Color.magenta);
  51. panel.getHAdjustable().addAdjustmentListener(new CustomAdjustmentListener());
  52.  
  53. Label msglabel = new Label();
  54. msglabel.setAlignment(Label.CENTER);
  55. msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
  56. panel.add(msglabel);
  57.  
  58. controlPanel.add(panel);
  59. mainFrame.setVisible(true);
  60. }
  61.  
  62. class CustomAdjustmentListener implements AdjustmentListener {
  63. public void adjustmentValueChanged(AdjustmentEvent e) {
  64. statusLabel.setText("Adjustment value: "+Integer.toString(e.getValue()));
  65. }
  66. }
  67. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verify the following output

awt_adjustmentlistener.jpg