gpt4 book ai didi

java - 在 JSplitPane 上设置分隔线位置

转载 作者:行者123 更新时间:2023-11-29 07:30:46 24 4
gpt4 key购买 nike

我想要 3 个可水平调整大小的 JPanel。它工作正常,但我无法设置第一个 JSlitPane 的位置:sp.setDividerLocation(.3); 不起作用。

public class JSplitPanelProva extends JFrame {

public JSplitPanelProva() {
this.setLayout(new BorderLayout());

JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.BLUE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.CYAN);
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.GREEN);

JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, rightPanel);

sp.setOneTouchExpandable(true);
sp2.setOneTouchExpandable(true);


this.add(sp2, BorderLayout.CENTER);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(1000, 600);
this.setVisible(true);

sp.setDividerLocation(.3);
sp2.setDividerLocation(.6);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new JSplitPanelProva();

}

}

我明白了: enter image description here有人能帮我吗?谢谢。

最佳答案

改变:

        sp.setDividerLocation(.3);
sp2.setDividerLocation(.6);

收件人:

    sp2.setDividerLocation(.6);
ActionListener splitListener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
sp.setDividerLocation(.3);
}
};
Timer t = new Timer(200, splitListener);
t.setRepeats(false);
t.start();

它会按预期工作。延迟为 GUI 重新计算大小提供了时间。

关于java - 在 JSplitPane 上设置分隔线位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43155592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com