テキストフィールドとボタンを持つ以下のコードを書きました。文字が入力されてボタンが押されるとすぐに、フィールドに入力されたものと同じタイトルのタブが作成されます。
いくつかのタブを同じ方法で作成できます.....新しいタブには、テキスト フィールドとボタンが存在し、結果を表示するテキスト ペインが表示されます....
各タブのテキストペインのテキストフィールドに入力されたテキストを表示したい...
次に、タブのボタンのリスナーを配置する方法と場所を教えてください....そして、他の必要なリスナーをお勧めします(フォーカスまたは選択されたタブに誘導する別のリスナーが必要だと思います)。
再利用のためにこれらのタブを配列リストに追加したことは言及する必要がありますが、正しいかどうか、またはどのように使用できるかわかりませんか?
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);
}
}