App下載
首頁javabasicJava Swing - 如何句柄調(diào)整大小事件

Java Swing - 如何句柄調(diào)整大小事件

我們想知道如何句柄調(diào)整大小事件。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Main {
  public static void main(String[] args) {
    JPanel panel = new JPanel() {
      @Override
      public Dimension getPreferredSize() {
        return new Dimension(200, 200);
      }
    };
    panel.addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        System.out.println("Resized to " + e.getComponent().getSize());
      }
      @Override
      public void componentMoved(ComponentEvent e) {
        System.out.println("Moved to " + e.getComponent().getLocation());
      }
    });
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("test", panel);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(tabbedPane);
    frame.pack();
   
    frame.setVisible(true);
  }
}