我写了下面的代码,里面有一个文本字段和一个按钮。输入字符并按下按钮后,将创建一个标签,其标题与在字段中输入的标题相同。
可以使用相同的方式创建多个选项卡.....现在再次在新选项卡中,文本字段和按钮存在一个长文本窗格以显示结果....
我想在每个标签页的文本窗格中显示输入文本字段的文字......
现在请带领我学习如何以及将监听器放在标签按钮的位置....并推荐任何其他必需的听众(我认为应该有另一个听众指导我到焦点或选定的标签)。
应该提到的是,我已将这些选项卡添加到数组列表中以供任何重用,但我不知道我是否做得对或如何使用它?
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);
}
}
答案 0 :(得分: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;
}
答案 1 :(得分:4)
对显示为TabComponentsDemo
的here的修改显示了一种重命名标签的方法。它会在每个窗格上收听JButton
,但ActionListener
上的JTextField
也应该投放。
答案 2 :(得分:3)
您可以使用各种方法来提高用户界面的稳健性:
setVisible(true);
JComponent
正如你在代码中所做的那样,因为你做了
父级可见,所有子组件也将设置为可见。pack()/setVisible(true/false)
之类的电话上完成
EDT-Event Dispatch Thread,而不是从主要调用它们
方法。有关详细信息,请阅读Concurrency in 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);
}
});
}
}