面板内部选项卡未显示

时间:2019-03-29 18:10:10

标签: java swing jframe jpanel jtabbedpane

我正在尝试使用IntelliJ GUI表单创建UI。 我创建了一个扩展JFrame的类,该类仅包含JTabbedPane,它带有2个选项卡,每个选项卡都有一个JPanel。 然后是另外两个扩展JPanel的类。 但是,启动时不会显示任何面板。

我试图使面板无效,重新粉刷和重新验证

//main class
import javax.swing.*;

public class jumpManMain {
    public static void main (String[] args) {
        SwingUtilities.invokeLater(() -> {
            final JFrame frame =  new frmTabs();
        });
    }
}

// tabbedpanel class
public class frmTabs extends JFrame implements constants{
    private JTabbedPane tabbedPane;
    private JPanel panelTab1;
    private JPanel panelTab2;

    public frmTabs()
    {
        setContentPane(tabbedPane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(FORM_LEFT, FORM_TOP, FORM_WIDTH, FORM_HEIGHT);
        panelTab1 = new panel1();
        panelTab2 = new panel2();
        pack();
        setVisible(true);
        setSize(FORM_WIDTH,FORM_HEIGHT);


        ChangeListener changeListener = new ChangeListener() {
            public void stateChanged(ChangeEvent changeEvent) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
                int index = sourceTabbedPane.getSelectedIndex();
                System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
                panelTab1.revalidate();
                panelTab1.repaint();
                panelTab2.revalidate();
                panelTab2.repaint();
            }
        };
        tabbedPane.addChangeListener(changeListener);
    }

}
//tabs form generated by IntelliJ
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="frmTabs">
  <tabbedpane id="78766" binding="tabbedPane">
    <constraints>
      <xy x="20" y="20" width="900" height="560"/>
    </constraints>
    <properties>
      <minimumSize width="900" height="560"/>
      <preferredSize width="900" height="560"/>
    </properties>
    <border type="none"/>
    <children>
      <grid id="35c5e" binding="panelTab1" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
        <margin top="0" left="0" bottom="0" right="0"/>
        <constraints>
          <tabbedpane title="Jumps arrived"/>
        </constraints>
        <properties>
          <maximumSize width="1000" height="1000"/>
          <minimumSize width="900" height="560"/>
          <preferredSize width="900" height="560"/>
        </properties>
        <border type="none"/>
        <children/>
      </grid>
      <grid id="4323" binding="panelTab2" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
        <margin top="0" left="0" bottom="0" right="0"/>
        <constraints>
          <tabbedpane title="Jumps created"/>
        </constraints>
        <properties>
          <maximumSize width="1000" height="1000"/>
          <minimumSize width="900" height="560"/>
          <preferredSize width="900" height="560"/>
        </properties>
        <border type="none"/>
        <children/>
      </grid>
    </children>
  </tabbedpane>
</form>

//panel1 XML
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="panel1">
  <grid id="27dc6" binding="panelMain" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
    <margin top="5" left="5" bottom="5" right="5"/>
    <constraints>
      <xy x="21" y="20" width="900" height="560"/>
    </constraints>
    <properties>
      <minimumSize width="900" height="560"/>
      <preferredSize width="900" height="560"/>
      <requestFocusEnabled value="true"/>
    </properties>
    <border type="bevel-raised" title="Jump Ticket Manager">
      <font name="Arial Black" style="1"/>
      <title-color color="-4451918"/>
    </border>
    <children>
....
    </children>
  </grid>
</form>
...

2 个答案:

答案 0 :(得分:1)

您缺少以下步骤

1. JTabbedPane初始化

  tabbedPane = new JTabbedPane();
  1. 将面板添加到您的JTabbedPane

    tabbedPane.add(panelTab1);
    tabbedPane.add(panelTab2);
    

因此,您的最终代码将如下所示

 public class Main {
     public static void main(String[] args) {
         SwingUtilities.invokeLater(() -> {
            final JFrame frame =  new frmTabs();
       });
   }
 }

class frmTabs extends JFrame implements constants{
private JTabbedPane tabbedPane;
private JPanel panelTab1;
private JPanel panelTab2;

public frmTabs()
{
    tabbedPane = new JTabbedPane();
    setContentPane(tabbedPane);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(FORM_LEFT, FORM_TOP, FORM_WIDTH, FORM_HEIGHT);
    panelTab1 = new JPanel();
    panelTab2 = new JPanel();
    panelTab1.setName("first Tab");   //To add titles to your tabs
    panelTab2.setName("Second Tab");  //to add titles to your tabs
    tabbedPane.add(panelTab1);
    tabbedPane.add(panelTab2);
    pack();
    setVisible(true);
    setSize(FORM_WIDTH,FORM_HEIGHT);


    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
            int index = sourceTabbedPane.getSelectedIndex();
            System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
            panelTab1.revalidate();
            panelTab1.repaint();
            panelTab2.revalidate();
            panelTab2.repaint();
        }
    };
    tabbedPane.addChangeListener(changeListener);
    }
  }

* NB以下行将标题添加到选项卡式面板

  panelTab1.setName("first Tab");  
  panelTab2.setName("Second Tab");

输出

  Tab changed to: Second Tab
  Tab changed to: first Tab

  Process finished with exit code 0

see screen shot below

答案 1 :(得分:0)

有效使用IntelliJ GUI的解决方案是这样的:

1)创建主类

public class jManagerMainUI extends JFrame {
private JPanel panelTab1;
private JPanel panelTab2;
private JTabbedPane tabbedPane;

public jManagerMainUI(String param) {
    setContentPane(tabbedPane);
    tabbedPane.setBounds(FORM_LEFT, FORM_TOP, FORM_WIDTH, FORM_HEIGHT);
    setDefaultCloseOperation(3);
    setBounds(FORM_LEFT, FORM_TOP, FORM_WIDTH, FORM_HEIGHT);
    tabbedPane.setComponentAt(0,new jA(param).getPanel());
    tabbedPane.setComponentAt(1,new jC(param).getPanel());
    pack();
    setVisible(true);
    setSize(FORM_WIDTH, FORM_HEIGHT);
}
}

2)为每个选项卡创建一个类,该类会将引用返回给面板

public class jA extends JFrame {
    private JPanel panelA;
    //Tab A
    private JButton btAddA;

    public JPanel getPanel() {
        return panelA;
    }
...