java - JTabbedPane 不显示添加的其他选项卡

标签 java swing jpanel jtabbedpane

我一直在四处寻找 JTabbedPane 的相关信息,但似乎找不到任何解决方案。

我使用 GridBagLayout 作为管理器,因为我对此更熟悉(我是 Java 编程的新手)。

我创建了这个类,并用 JPanel 对其进行了扩展,并将所有类添加到另一个类中,当我运行程序时,我看到的只是空白屏幕,我将发布代码以便您查看。

(测试类/GUI类)

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class test1 extends JPanel {

    JTextField ID, Name, Address1, Address2,
            Post_Code, Landline, Mobile, Card_Name,
            Card_Number, Expiry_Date, Security_Number,
            Identification = new JTextField();
    ResultSet rs1;
    Connection conn;
    PreparedStatement pst;
    //JFrame frame = new JFrame("Test");
    String items[] = {"Yes", "No"};
    JComboBox box = new JComboBox(items);

    public test1() {
        JPanel jp2 = new JPanel(new GridBagLayout());
        GridBagConstraints c1 = new GridBagConstraints();
        c1.insets = new Insets(5, 5, 5, 5);
        c1.gridx = 1;
        c1.gridy = 0;
        c1.anchor = GridBagConstraints.CENTER;
        jp2.add(new JLabel("Customer Registration"), c1);
        c1.gridx = 0;
        c1.gridy = 1;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("ID"), c1);
        c1.gridx = 1;
        c1.gridy = 1;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(ID = new JTextField(10), c1);
        c1.gridx = 0;
        c1.gridy = 2;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Name"), c1);
        c1.gridx = 1;
        c1.gridy = 2;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Name = new JTextField(10), c1);
        c1.gridx = 0;
        c1.gridy = 3;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Address"), c1);
        c1.gridx = 1;
        c1.gridy = 3;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Address1 = new JTextField(15), c1);
        c1.gridx = 1;
        c1.gridy = 4;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Address2 = new JTextField(15), c1);
        c1.gridx = 0;
        c1.gridy = 5;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Post Code"), c1);
        c1.gridx = 1;
        c1.gridy = 5;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Post_Code = new JTextField(10), c1);
        c1.gridx = 0;
        c1.gridy = 6;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Landline"), c1);
        c1.gridx = 1;
        c1.gridy = 6;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Landline = new JTextField(10), c1);
    }
}

将所有 JTabbedPane 组合在一起的类:

导入javax.swing.JFrame; 导入 javax.swing.JTabbedPane; 导入 javax.swing.SwingUtilities;

公共(public)类 tabadd 扩展 JFrame {

JTabbedPane tab = new JTabbedPane();
Customer_Registration CR = new Customer_Registration();
test1 g = new test1();

public tabadd() {
    tab.add("Customer Registration", CR);
    tab.add("Equipment Registration", g);
    getContentPane().add(tab);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            tabadd m = new tabadd();
            m.setTitle("Test");
            m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            m.setSize(1280, 720);
            m.setLocationByPlatform(true);
            m.setVisible(true);
        }
    });
}

将有多个类,因此有 Customer_Registration 类。

再次感谢您的帮助。

最佳答案

您没有在任何地方添加 jp2 面板。在 test1 类构造函数的末尾添加以下行。

add(jp2);

另一种选择是将 GridBagLayout 设置为 test1 JPanel 的布局管理器,并将所有组件直接添加到其中。这样您就可以避免使用额外的 jp2 面板。

并且请使用标准的 Java 类命名,例如 test1 变为 Test1

关于java - JTabbedPane 不显示添加的其他选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15010356/

相关文章:

Java HotSpot(TM) 64 位服务器虚拟机警告 : ignoring option MaxPermSize

java - 从包含 # 的 url 在 java servlet 之间重定向

java - 使用 documentFilter 过滤掉制表符

java - 如何获取 JPanel 中某个点的颜色?

java内部类函数通过按钮监听器调用

java - 在库中使用 realm.io 时出现 NoSuchMethodError

java - BlueJ 中与 Math.min 不兼容的类型

java - JTextField只显示绝对值,没有负号?

java - 从 EDT 外部获取 Swing 组件的值

java - 我无法将 ImageIcon 添加到 JPanel