gpt4 book ai didi

java - 选项卡内的面板不显示

转载 作者:行者123 更新时间:2023-12-02 10:04:18 25 4
gpt4 key购买 nike

我正在尝试使用 IntelliJ GUI 表单创建 UI。我创建了一个扩展 JFrame 的类,并且仅包含 JTabbedPane,其中有 2 个选项卡,每个选项卡都有一个 JPanel。然后是另外 2 个扩展 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>
...

最佳答案

您缺少以下步骤

1.JTabbedPane初始化

  tabbedPane = new JTabbedPane();
  • 将面板添加到 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);
    }
    }

    *注意以下几行将为您的选项卡式面板添加标题

      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

    关于java - 选项卡内的面板不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423317/

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