3

テキストフィールドとボタンを持つ以下のコードを書きました。文字が入力されてボタンが押されるとすぐに、フィールドに入力されたものと同じタイトルのタブが作成されます。

いくつかのタブを同じ方法で作成できます.....新しいタブには、テキスト フィールドとボタンが存在し、結果を表示するテキスト ペインが表示されます....

各タブのテキストペインのテキストフィールドに入力されたテキストを表示したい...

次に、タブのボタンのリスナーを配置する方法と場所を教えてください....そして、他の必要なリスナーをお勧めします(フォーカスまたは選択されたタブに誘導する別のリスナーが必要だと思います)。

再利用のためにこれらのタブを配列リストに追加したことは言及する必要がありますが、正しいかどうか、またはどのように使用できるかわかりませんか?

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);
    }

}
4

3 に答える 3

6

ActionListenerメソッド内のボタンに を追加しますcreatePanel。したがって、あなたの方法は次のようになります(テキストが明確ではないため、実際にテキストで何をしたいのかについていくつかの仮定を立てます):

protected JPanel createPanel(String input) {
    JPanel inst = new JPanel();
    inst.setVisible(true);
    final 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);
    final JTextPane textPane = new JTextPane();
    textPane.setBounds(12, 54, 168, 40);
    inst.add(textPane);
    textPane.setVisible(true);

    button.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            textPane.setText(textPane.getText() + textField.getText());
        }});

    inst.setLayout(null);
    inst.add(button);
    inst.add(textField);
    ary.add(inst);
    tabIndex = index;
    index++;
    return inst;
}
于 2012-08-09T00:49:59.717 に答える
4

ここTabComponentsDemoに示す変更は、タブの名前を変更する 1 つの方法を示しています。各ペインで をリッスンしますが、 で も機能する必要があります。JButtonActionListenerJTextField

于 2012-08-09T00:20:34.470 に答える
3

ユーザーインターフェイスをより堅牢にするために使用できるさまざまなポイント:

  • AbsolutePositioningから始めるのは本当に良い考えではありません。リンクの最初の段落を読んで、 LayoutManagersを使用するよりもこのアプローチが推奨されない理由の詳細を確認してください 。
  • 親を表示すると、すべての子コンポーネントが同様に表示されるように設定されるため、コードで行っているようsetVisible(true);に、さまざまな を明示的に使用する必要はありません。JComponent
  • のような呼び出しpack()/setVisible(true/false)は、mainメソッドから呼び出すのではなく、EDT-EventDispatchThreadで実行する必要があります。詳細については、Swingの同時実行性をお読みください。

あなたのこの修正されたコードを見てください、そしてこれについてもっと洞察が必要かどうか尋ねてください:

import java.awt.BorderLayout;
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;
import javax.swing.SwingUtilities;

public class TestGUI extends JFrame {

    private JPanel contentPane;
    private JTextField jTextField1;
    private JButton jButton1;
    static ArrayList<JPanel> ary = new ArrayList<JPanel>();
    private int tabIndex = 0;
    static int index = 0;
    private JTabbedPane tabbedPane;

    public TestGUI() {

        super("Testing Tab Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        Handler but1 = new Handler();
        JPanel footerPanel = new JPanel();

        jTextField1 = new JTextField(10);
        footerPanel.add(jTextField1);

        jButton1 = new JButton("Create TAB");
        footerPanel.add(jButton1);
        jButton1.addActionListener(but1);

        tabbedPane = new JTabbedPane();

        contentPane.add(tabbedPane, BorderLayout.CENTER);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);   

        setContentPane(contentPane);    
        setSize(300, 300);
        setLocationRelativeTo(null);
    }

    private class Handler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent evt) {
            String input = jTextField1.getText();
            if (!input.isEmpty()) {

                JPanel inst = createPanel();                
                tabbedPane.addTab(input, inst);
                ary.add(inst);              

                jTextField1.setText("");
                contentPane.revalidate();
                contentPane.repaint();              
            }
        }
    }

    protected JPanel createPanel() {

        JPanel inst = new JPanel();
        inst.setLayout(new BorderLayout(5, 5));

        final JTextPane textPane = new JTextPane();

        JPanel footerPanel = new JPanel();
        final JTextField textField = new JTextField(10);
        JButton button = new JButton("SHOW");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (textField.getDocument().getLength() > 0)
                    textPane.setText(textField.getText());
                textField.setText("");  
            }
        });
        footerPanel.add(textField);
        footerPanel.add(button);

        inst.add(textPane, BorderLayout.CENTER);
        inst.add(footerPanel, BorderLayout.PAGE_END);    

        return inst;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TestGUI inst = new TestGUI();
                inst.setVisible(true);
            }
        });
    }

}
于 2012-08-09T06:24:44.340 に答える