我写了下面的代码,它有一个文本字段和一个按钮。只要输入一个字符并按下按钮,就会创建一个选项卡,其标题与在字段中输入的内容相同。
可以以相同的方式创建多个选项卡......现在再次在新选项卡中,一个文本字段和一个按钮存在一个很长的文本窗格以显示结果......
我想在每个选项卡的文本窗格中显示输入到文本字段中的文本...
现在请引导我了解如何以及在何处放置选项卡按钮的侦听器......并推荐任何其他所需的侦听器(我认为应该有另一个侦听器将我引导到焦点或选定的选项卡)。
应该提到的是,我已将这些选项卡添加到数组列表中以供重复使用,但我不知道我是否做得对或如何使用它?
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
public class TestGUI extends JFrame {
    private JTextField jTextField1;
    private JButton jButton1;
    static ArrayList<JPanel> ary = new ArrayList<JPanel>();
    private int tabIndex = 0;
    static int index = 0;
    private JTabbedPane tabbedPane;
    /**
    * @param args
    */
    public TestGUI() {
        super("Testing Tab Frame");
        setLayout(null);
        Handler but1 = new Handler();
        jTextField1 = new JTextField();
        jTextField1.setVisible(true);
        jTextField1.setBounds(12, 12, 85, 30);
        add(jTextField1);
        jButton1 = new JButton("Button1");
        jButton1.setVisible(true);
        jButton1.setBounds(130, 12, 85, 30);
        add(jButton1);
        jButton1.addActionListener(but1);
        tabbedPane = new JTabbedPane();
        tabbedPane.setBounds(12, 54, 200, 150);
        tabbedPane.setVisible(false);
        add(tabbedPane);
        pack();
        setSize(250, 110);
        setLocationRelativeTo(null);
    }
    private class Handler implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            String input = jTextField1.getText();
            if (!input.isEmpty()) {
                setSize(250, 250);
                JPanel inst = createPanel(input);
                inst.setVisible(true);
                tabbedPane.addTab(Integer.toString(tabIndex), inst);
                tabbedPane.setVisible(true);
            }
        }
    }
    protected JPanel createPanel(String input) {
        JPanel inst = new JPanel();
        inst.setVisible(true);
        JTextField textField = new JTextField();
        textField.setVisible(true);
        textField.setBounds(12, 12, 80, 30);
        JButton button = new JButton();
        button.setVisible(true);
        button.setBounds(100, 12, 80, 30);
        JTextPane textPane = new JTextPane();
        textPane.setBounds(12, 54, 168, 40);
        inst.add(textPane);
        textPane.setVisible(true);
        inst.setLayout(null);
        inst.add(button);
        inst.add(textField);
        ary.add(inst);
        tabIndex = index;
        index++;
        return inst;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGUI inst = new TestGUI();
        inst.setVisible(true);
    }
}