向JTabbedPane添加组件

时间:2019-04-11 15:36:09

标签: java jtabbedpane

当我运行代码时,所有组件,JTable或按钮都不会显示在选项卡内,而是显示在选项卡窗口的侧面。有什么原因吗?

public void GUIcode() {


    setLayout(new GridBagLayout());

    setBounds(100, 100, 450, 300);

    panel = new JPanel();
    panel.setBorder(new EmptyBorder(10, 10, 10, 10));
    setContentPane(panel);
    panel.setLayout(null);

    JTabbedPane tabb = new JTabbedPane(JTabbedPane.TOP);
    tabb.setBounds(0, 0, 400, 300);
    panel.add(tabb);

    JPanel panel = new JPanel();
    tabb.addTab("vis vare", null, panel, null);
    panel.setLayout(null);

    tabellinnhold = new DefaultTableModel(defaulttabell,kolonnenavn);
    bytabell = new JTable(tabellinnhold);
    rullefelt = new JScrollPane(bytabell);

    panel.add(rullefelt);
    add(panel);

    koble = new JButton("koble til");
    lukke = new JButton("lukke");
    hente = new JButton("Hente data");
    avslutt = new JButton("Avslutt");
    // legger til knappepanel
    panel.setLayout(new GridLayout(1,4));
    panel.add(koble);
    panel.add(lukke);
    panel.add(hente);
    panel.add(avslutt);

    //action drit
    koble.addActionListener(this);
    lukke.addActionListener(this);
    hente.addActionListener(this);
    avslutt.addActionListener(this);

}

1 个答案:

答案 0 :(得分:1)

因为您将JTabbedPane添加到了panel然后写了

JPanel panel = new JPanel();

并添加了其他组件,包括JScrollPane及其按钮。 首先,代码无法以这种方式编译。其次,请更改添加顺序并将滚动窗格添加到选项卡