我编写了以下代码,但未显示选项卡的组件....事实上,当我在文本框中输入内容时,我想动态创建选项卡...创建的选项卡应包含一个新的文本字段和按钮. 此代码是一个示例,完成后我还有另一个问题。
请让我知道我在哪里犯了错误。
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
public class TestingTab 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 TestingTab(){
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, 220);
tabbedPane.setVisible(false);
add(tabbedPane);
pack();
setSize(250,110);
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent evt){
String input = jTextField1.getText();
setSize(250,330);
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);
inst.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED),input));
JTextField textField = new JTextField();
textField.setVisible(true);
JButton button = new JButton();
button.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
TestingTab inst = new TestingTab();
inst.setVisible(true);
}
}