3

目前,当您尝试手动调整 TopComponent 的大小时,它将显示一条黑线,表示 TopComponent 的未来大小。

在此处输入图像描述

我想知道JComponent这条黑线的背后是什么?我怎样才能访问它?


编辑

我的问题的第一部分得到了回答。附图中显示的组件是JSplitPane.

现在,我希望能够访问它(即获取JSplitPane用于TopComponent在 netbeans 平台中调整大小的 ' 实例。

4

1 回答 1

5
  • JSplitPane

  • 但不确定 Netbeans 是否用 Java 编译(与 Eclipse 相比)

    编辑I'd like to add a listener on it.

  • 将 PropertyChangeListener 添加到 JSplitPane,

  • 有几个有用的方法if (propertyName.equals(JSplitPane.XxxXxx))

  • 嵌套 JSplitPane 的通知,必须分别为每个 JSplitPanes 添加侦听器

  • 例如

.

import java.awt.Dimension;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

public class JSplitPaneToy {

    public static void main(String[] args) {
        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), makePanel());
        JPanel pnl = new JPanel();
        pnl.setLayout(new GridLayout(4, 1, 10, 10));
        pnl.add(makePanel());
        pnl.add(makePanel());
        pnl.add(makePanel());
        pnl.add(makePanel());
        PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent changeEvent) {
                JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
                String propertyName = changeEvent.getPropertyName();
                if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
                    int current = sourceSplitPane.getDividerLocation();
                    System.out.println("Current: " + current);
                    Integer last = (Integer) changeEvent.getNewValue();
                    System.out.println("Last: " + last);
                    Integer priorLast = (Integer) changeEvent.getOldValue();
                    System.out.println("Prior last: " + priorLast);
                }else if (propertyName.equals(JSplitPane.RESIZE_WEIGHT_PROPERTY)) {
                    int current = sourceSplitPane.getDividerLocation();
                    System.out.println("Current: " + current);
                    Integer last = (Integer) changeEvent.getNewValue();
                    System.out.println("Last: " + last);
                    Integer priorLast = (Integer) changeEvent.getOldValue();
                    System.out.println("Prior last: " + priorLast);
                }
            }
        };
        sp.addPropertyChangeListener(propertyChangeListener);
        JFrame frame = new JFrame("JSplitPane Toy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0, 2, 10, 10));
        frame.add(sp);
        frame.add(pnl);
        frame.pack();
        frame.setVisible(true);
    }

    private static JScrollPane makePanel() {
        JScrollPane pane = new JScrollPane(new JTable(
                new Object[][]{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}}, new Object[]{1, 2, 3}));
        pane.setPreferredSize(new Dimension(200, 100));
        return pane;
    }
}
于 2012-10-30T08:25:21.987 回答