我寫了下面的代碼,其中有一個文本字段和一個按鈕。只要輸入一個字符並按下該按鈕,就會創建一個標籤,其標題與字段中輸入的內容相同。如何以及在哪裏添加ActionListener在我的代碼中?
幾個選項卡可以創造同樣的方式.....現在又新標籤,文本字段和按鈕存在長期使用文本窗格中顯示結果....
我想在每個標籤的文本窗格中顯示輸入到文本字段中的文本...
現在請帶領我瞭解如何以及在哪裏放置了標籤按鈕的偵聽器....並且推薦任何其他必需的聽衆(我認爲應該有另一個聽衆來引導我到焦點或選定的標籤)。
應該提到的是,我已經將這些標籤添加到數組列表中以供重用,但我不知道我是否正確或可以如何使用它?
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);
}
}
請設置您的職位,以一個具體的問題。 – Keppil 2012-08-08 23:30:50
@ MadProgramer&Keppil:我刪除了關於體系結構的問題......請僅考慮聽衆......;) – msc87 2012-08-08 23:42:27
@ msc87你是什麼意思?「我想顯示輸入到其文本字段的文本每個選項卡中的文本窗格......「 - 哪個文本字段?主框架上還是標籤中的主框架? – MadProgrammer 2012-08-08 23:44:25