Java 使用Swing将窗格拆分为两半
有人能建议我如何将Java 使用Swing将窗格拆分为两半,java,swing,jpanel,layout-manager,jtabbedpane,Java,Swing,Jpanel,Layout Manager,Jtabbedpane,有人能建议我如何将JTabbedPane划分为两个相等的水平部分吗?我的窗格中有三个选项卡。我想将第二个选项卡窗格(选项卡2)分成两个相等的部分 选项卡式窗格的代码 对该选项卡中的JPanel使用单行GridLayout。其中有两个组件,每个组件将有一半的空间。例如 import javax.swing.*; import java.awt.*; public class Monitor { public static void main(String[] args){
JTabbedPane
划分为两个相等的水平部分吗?我的窗格中有三个选项卡。我想将第二个选项卡窗格(选项卡2)分成两个相等的部分
选项卡式窗格的代码
对该选项卡中的
JPanel
使用单行GridLayout
。其中有两个组件,每个组件将有一半的空间。例如
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);
}
}
对该选项卡中的
JPanel
使用单行GridLayout
。其中有两个组件,每个组件将有一半的空间。例如
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);
}
}
你能为我的上述代码分享一些代码片段示例吗?@Dojo_user看一看示例Hanks Andrew!!这是可行的,但我想水平分割窗户,这是垂直分割。我需要更改哪个参数才能在同一代码中水平划分窗口,我认为应该是JPanel tab2Panel=newjpanel(newgridlayout(0,1));对于水平选项。如果您选中,您可能会看到
新的GridLayout(1,0)
将是新的GridLayout(0,1)
;)你能为我的上述代码分享一些代码片段示例吗?@Dojo_user看一看示例Hanks Andrew!!这是可行的,但我想水平分割窗户,这是垂直分割。我需要更改哪个参数才能在同一代码中水平划分窗口,我认为应该是JPanel tab2Panel=newjpanel(newgridlayout(0,1));对于水平选项。如果您选中,您可能会看到新的GridLayout(1,0)
将是新的GridLayout(0,1)
;)