Swing - JTabbedPane - 面板不会显示两个选项卡

时间:2015-05-27 11:12:41

标签: java swing jpanel jtabbedpane

为什么没有南面板显示第一个标签 - 仅针对第二个标签?

如果我改为为每个标签调用southPanel(),是的 - 南面板显示但是 - 听众不起作用(我认为这是因为新的内存地址分配gui对象,如按钮两次)。

所以 - 无论我做什么都有问题 - 无论是南面板都没有出现,或者它出现了但是回调并不适用于第一个标签。

任何解决方案

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

public class MyApp extends JFrame implements ActionListener, ItemListener {

private JTabbedPane pane;
private JPanel tab1Panel, tab2Panel, centerPanel, southPanel, inputPanel, checkPanel;;
private JScrollPane scrollPane1, scrollPane2, scrollPane3;
private JTextArea area1, area2;
private JButton quit, ok;
private JCheckBox check;
private JTextField input;
private JLabel input_lbl, check_lbl;

public static void main(String[] args) {

    new MyApp().setVisible(true);
}

public MyApp() {

    initComp();
    setListeners();
}

private void initComp() {

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(dim.width / 2, dim.height / 2);
    setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

    southPanel();

    tab1();
    tab2();

    pane = new JTabbedPane();
    pane.addTab("info om xml", tab1Panel);
    pane.addTab("rita", tab2Panel);

    Container contentPane = getContentPane();
    contentPane.add(pane);

    setDefaultCloseOperation(EXIT_ON_CLOSE);    
}

private void tab1() {

    area1 = new JTextArea();
    area2 = new JTextArea();

    scrollPane1 = new JScrollPane(area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane2 = new JScrollPane(area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    centerPanel = new JPanel(new GridLayout(1, 2));
    centerPanel.add(scrollPane1);
    centerPanel.add(scrollPane2);

    tab1Panel = new JPanel(new BorderLayout());
    tab1Panel.add(centerPanel, BorderLayout.CENTER);
    tab1Panel.add(southPanel, BorderLayout.SOUTH);
}

private void tab2() {

    centerPanel = new JPanel();
    centerPanel.add(new JLabel("this is for drawing"));

    tab2Panel = new JPanel(new BorderLayout());
    tab2Panel.add(centerPanel, BorderLayout.CENTER);
    tab2Panel.add(southPanel, BorderLayout.SOUTH);
}

private void southPanel() {

    ok = new JButton("Ok");
    quit = new JButton("Avsluta");

    check_lbl = new JLabel("Change");
    input_lbl = new JLabel("Document");

    input = new JTextField("");
    input.setEditable(false);

    check = new JCheckBox();

    inputPanel = new JPanel(new BorderLayout());
    inputPanel.add(input_lbl, BorderLayout.WEST);
    inputPanel.add(input, BorderLayout.CENTER);

    checkPanel = new JPanel(new BorderLayout());
    checkPanel.add(check_lbl, BorderLayout.WEST);
    checkPanel.add(check, BorderLayout.CENTER);

    southPanel = new JPanel(new GridLayout(1, 4));
    southPanel.add(quit);
    southPanel.add(ok);
    southPanel.add(inputPanel);
    southPanel.add(checkPanel);
}

@Override
public void actionPerformed(ActionEvent aE) {

    String theEvent = aE.getActionCommand();

    if (theEvent.equals("Avsluta")) {
        System.exit(0);
    } else if (theEvent.equals("Ok")) {

        System.out.println("ok!");
    }   
}

@Override
public void itemStateChanged(ItemEvent iE) {

    if (iE.getStateChange() == iE.SELECTED) {
        input.setEditable(true);
    } else {
        input.setEditable(false);
    }
}

private void setListeners() {

    check.addItemListener(this);
    ok.addActionListener(this);
    quit.addActionListener(this);
}   

}

编辑:也许我应该以另一种方式提出问题 - 我怎么能让这两个标签共享相同的SouthPanel?

1 个答案:

答案 0 :(得分:2)

  

我怎样才能让这两个标签共享同一个南面板?

简短的回答是,你做不到。组件只能驻留在单个容器中。您需要为每个标签

创建southPanel的两个不同实例

更好的解决方案是将组件分离为可重用的组件(即使southPanel创建自己的组件)并包含其中的功能。这样,您可以简单地重新创建它,甚至修改它它是通过继承的功能。