AWT AdjustmentListener 接口



简介

接口AdjustmentListener用于接收调整事件。处理调整事件的类需要实现此接口。

类声明

以下是对java.awt.event.AdjustmentListener接口的声明

public interface AdjustmentListener
extends EventListener

接口方法

序号方法 & 描述
1

void adjustmentValueChanged(AdjustmentEvent e)

当可调整项的值发生更改时调用。

继承的方法

此类继承以下接口中的方法

  • java.awt.event.EventListener

AdjustmentListener 示例

推荐在D:/ > AWT > com > tutorialspoint > gui > 中使用自己选择的任何编辑器创建以下 java 程序

AwtListenerDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtListenerDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtListenerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtListenerDemo  awtListenerDemo = new AwtListenerDemo();  
      awtListenerDemo.showAdjustmentListenerDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
   
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showAdjustmentListenerDemo(){
      headerLabel.setText("Listener in action: AdjustmentListener");      

      ScrollPane panel = new ScrollPane();      
      panel.setBackground(Color.magenta);
      panel.getHAdjustable().addAdjustmentListener(new CustomAdjustmentListener());

      Label msglabel = new Label();
      msglabel.setAlignment(Label.CENTER);   
      msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
      panel.add(msglabel);
      
      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   }

   class CustomAdjustmentListener implements AdjustmentListener {
      public void adjustmentValueChanged(AdjustmentEvent e) {
         statusLabel.setText("Adjustment value: "+Integer.toString(e.getValue()));
      }
   }
}

使用命令提示符编译程序。转到D:/ > AWT并键入以下命令。

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

如果没有出现错误,则表示已成功编译。使用以下命令运行该程序。

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

验证以下输出

AWT AdjustmentListener
awt_event_listeners.htm
广告