Java 带GridBagLayout的JTabbedPane
我理解JTabbedPane和GridBagLayout背后的概念。然而,当我试图同时使用两者时,我失败了。也就是说,当我使用GBLayout时,我的其他选项卡(每个选项卡具有不同的功能)不会显示。请帮忙。多谢各位 这是我的密码:Java 带GridBagLayout的JTabbedPane,java,swing,applet,jtabbedpane,gridbaglayout,Java,Swing,Applet,Jtabbedpane,Gridbaglayout,我理解JTabbedPane和GridBagLayout背后的概念。然而,当我试图同时使用两者时,我失败了。也就是说,当我使用GBLayout时,我的其他选项卡(每个选项卡具有不同的功能)不会显示。请帮忙。多谢各位 这是我的密码: import java.awt.*; import javax.swing.*; public class Tryout extends JFrame { private static final long serialVersio
import java.awt.*;
import javax.swing.*;
public class Tryout extends JFrame {
private static final long serialVersionUID = 1L;
private JTabbedPane tabbedPane;
private JPanel panel1;
private JPanel breakfast;
public Tryout()
{
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
createPage1(); //Tab1
createPage2(); //Tab2
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Input Form", panel1 );
tabbedPane.addTab( "Breakfast", breakfast );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
public void createPage1()
{
/* Works fine when I un-comment this
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(new JLabel("Hi"), BorderLayout.NORTH);
*/
//Tabs not getting displayed if I add the code below with GBLayout
JPanel panel = new JPanel(new GridBagLayout());
this.getContentPane().add(panel);
JLabel label = new JLabel("Form");
JPanel tableButtonPanel = new JPanel();
tableButtonPanel.add(new JButton("Add Thing"));
tableButtonPanel.add(new JRadioButton("Delete Thing"));
tableButtonPanel.add(new JButton("Modify Thing"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(label, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(tableButtonPanel, gbc);
}
public void createPage2()
{
breakfast = new JPanel();
breakfast.setLayout( new BorderLayout() );
breakfast.add( new JButton( "North" ), BorderLayout.NORTH );
breakfast.add( new JButton( "South" ), BorderLayout.SOUTH );
breakfast.add( new JButton( "East" ), BorderLayout.EAST );
breakfast.add( new JButton( "West" ), BorderLayout.WEST );
breakfast.add( new JButton( "Center" ), BorderLayout.CENTER );
}
public static void main(String args[]) {
Tryout ex = new Tryout();
ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex.setSize(750,750);
ex.setVisible(true);
ex.setTitle( "Recipe Tracker" );
ex.setBackground( Color.gray );
}
}
问题在于,在
createPage1
中,您正在向JFrame
添加一个新的JPanel
:
this.getContentPane().add(panel);
替换位于JFrame
的边框布局.中间位置的topPanel
(其中包含JTabbedPane
)。因此,没有出现JTabbedPane
您只需返回已创建的新JPanel
,并将其添加到JTabbedPane
:
public JPanel createPage1() {
JPanel panel = new JPanel(new GridBagLayout());
// this.getContentPane().add(panel); don't do this...
...
return panel;
}
并加上:
tabbedPane.addTab("Input Form", createPage1());
问题在于,在createPage1
中,您正在向JFrame
添加一个新的JPanel
:
this.getContentPane().add(panel);
替换位于JFrame
的边框布局.中间位置的topPanel
(其中包含JTabbedPane
)。因此,没有出现JTabbedPane
您只需返回已创建的新JPanel
,并将其添加到JTabbedPane
:
public JPanel createPage1() {
JPanel panel = new JPanel(new GridBagLayout());
// this.getContentPane().add(panel); don't do this...
...
return panel;
}
并加上:
tabbedPane.addTab("Input Form", createPage1());
保释我出来。谢谢。然而,我现在几乎没有其他问题了。他救了我出来。谢谢。然而,我现在几乎没有其他问题。