gpt4 book ai didi

java - 使用 Swing 将 Pane 分成两半

转载 作者:行者123 更新时间:2023-11-29 03:20:57 24 4
gpt4 key购买 nike

谁能建议我如何将我的 JTabbedPane 分成两个相等的水平部分?我的 Pane 中有三个选项卡。我想将第二个选项卡 Pane (选项卡 2)分成相等的两半?

标签面板代码

import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class Monitor{
public static void main(String[] args){
JFrame frame = new JFrame("WELCOME");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JButton button = new JButton("1");
tab.add("tab1", button);
button = new JButton("2");
tab.add("tab2", button);
button = new JButton("3");
tab.add("tab3", button);
frame.setSize(400,400);
frame.setVisible(true);

}
}

最佳答案

为放置在该选项卡中的 JPanel 使用单行 GridLayout。里面有两个组件,它们各占一半的空间。例如

enter image description here

import javax.swing.*;
import java.awt.*;

public class Monitor {

public static void main(String[] args){
Runnable r = new Runnable() {
public void run() {
JFrame frame = new JFrame("WELCOME");
// A better close operation..
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JButton button = new JButton("1");
tab.add("tab1", button);

// this GridLayout will create a single row of components,
// with equal space for each component
JPanel tab2Panel = new JPanel(new GridLayout(1,0));
button = new JButton("2");
tab2Panel.add(button);
tab2Panel.add(new JButton("long name to stretch frame"));
// add the panel containing two buttons to the tab
tab.add("tab2", tab2Panel);

button = new JButton("3");
tab.add("tab3", button);
// a better sizing method..
//frame.setSize(400,400);
frame.pack();
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - 使用 Swing 将 Pane 分成两半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23667819/

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