选项卡内的面板未显示
Panel inside tab not displayed
我正在尝试使用 IntelliJ GUI 表单创建一个 UI。
我创建了一个扩展 JFrame 的 class,它只包含 JTabbedPane 和 2 个选项卡,每个选项卡都有一个 JPanel。
然后是另外 2 个扩展 JPanel 的 classes。
但是,启动时没有面板显示。
我已尝试使面板无效、重新绘制和重新生效
//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
有效使用 IntelliJ GUI 的解决方案是:
1) 创建主class
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) 为每个将 return 引用面板的选项卡创建一个 class
public class jA extends JFrame {
private JPanel panelA;
//Tab A
private JButton btAddA;
public JPanel getPanel() {
return panelA;
}
...
我正在尝试使用 IntelliJ GUI 表单创建一个 UI。 我创建了一个扩展 JFrame 的 class,它只包含 JTabbedPane 和 2 个选项卡,每个选项卡都有一个 JPanel。 然后是另外 2 个扩展 JPanel 的 classes。 但是,启动时没有面板显示。
我已尝试使面板无效、重新绘制和重新生效
//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
有效使用 IntelliJ GUI 的解决方案是:
1) 创建主class
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) 为每个将 return 引用面板的选项卡创建一个 class
public class jA extends JFrame {
private JPanel panelA;
//Tab A
private JButton btAddA;
public JPanel getPanel() {
return panelA;
}
...