Java 我应该如何以及在哪里在代码中添加ActionListener?
我已经写了下面的代码,它有一个文本字段和一个按钮。一旦输入字符并按下按钮,就会创建一个选项卡,其标题与在字段中输入的标题相同 可以以相同的方式创建多个选项卡…..现在,在新选项卡中,存在一个文本字段和一个按钮,并带有一个文本窗格以显示结果 我想在每个选项卡的文本窗格中显示输入到文本字段的文本 现在请引导我学习如何以及在哪里放置选项卡按钮的侦听器。。。。并推荐任何其他需要的监听器(我认为应该有另一个监听器将我引导到焦点或所选选项卡) 应该提到的是,我已经将这些选项卡添加到了一个数组列表中以供重用,但我不知道我是否做对了,或者我如何使用它Java 我应该如何以及在哪里在代码中添加ActionListener?,java,swing,actionlistener,jtabbedpane,Java,Swing,Actionlistener,Jtabbedpane,我已经写了下面的代码,它有一个文本字段和一个按钮。一旦输入字符并按下按钮,就会创建一个选项卡,其标题与在字段中输入的标题相同 可以以相同的方式创建多个选项卡…..现在,在新选项卡中,存在一个文本字段和一个按钮,并带有一个文本窗格以显示结果 我想在每个选项卡的文本窗格中显示输入到文本字段的文本 现在请引导我学习如何以及在哪里放置选项卡按钮的侦听器。。。。并推荐任何其他需要的监听器(我认为应该有另一个监听器将我引导到焦点或所选选项卡) 应该提到的是,我已经将这些选项卡添加到了一个数组列表中以供重用,
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);
}
}
封装测试;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JTabbedPane;
导入javax.swing.JTextField;
导入javax.swing.JTextPane;
公共类TestGUI扩展了JFrame{
专用JTextField jTextField1;
私有JButton jButton1;
静态ArrayList ary=新ArrayList();
私有int tabIndex=0;
静态int指数=0;
私有JTabbedPane选项卡窗格;
/**
*@param args
*/
公共TestGUI(){
超级(“测试标签框架”);
setLayout(空);
Handler but1=新的Handler();
jTextField1=新的JTextField();
jTextField1.setVisible(true);
jTextField1.挫折(12,12,85,30);
添加(jTextField1);
jButton1=新JButton(“Button1”);
jButton1.setVisible(true);
jButton1.立根(130,12,85,30);
添加(jButton1);
jButton1.addActionListener(but1);
tabbedPane=新的JTabbedPane();
tabbedPane.setBounds(12,54,200,150);
tabbedPane.setVisible(false);
添加(选项卡窗格);
包装();
设置大小(250、110);
setLocationRelativeTo(空);
}
私有类处理程序实现ActionListener{
已执行的公共无效操作(操作事件evt){
字符串输入=jTextField1.getText();
如果(!input.isEmpty()){
设置大小(250250);
JPanel inst=createPanel(输入);
仪器设置可见(真);
tabbedPane.addTab(Integer.toString(tabIndex),inst);
tabbedPane.setVisible(true);
}
}
}
受保护的JPanel createPanel(字符串输入){
JPanel inst=新的JPanel();
仪器设置可见(真);
JTextField textField=新的JTextField();
textField.setVisible(true);
textField.setBounds(12,12,80,30);
JButton button=新JButton();
按钮。设置可见(真);
按钮.立根(100,12,80,30);
JTextPane textPane=新的JTextPane();
textPane.setBounds(12,54,168,40);
说明添加(文本窗格);
textPane.setVisible(true);
仪器设置布局(空);
安装添加(按钮);
仪器添加(文本字段);
3.增补(inst);
tabIndex=索引;
索引++;
返回仪表;
}
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
TestGUI inst=newtestgui();
仪器设置可见(真);
}
}
对所示的修改显示了重命名选项卡的一种方法。它在每个窗格上侦听JButton
,但JTextField
上的ActionListener
也应该起作用 您可以将ActionListener
添加到createPanel
方法内的按钮中。所以你的方法应该是这样的(假设你想对文本做什么,因为它不清楚):
您可以使用以下几点来增强用户界面的健壮性:
- 这真的不是一个好主意。 请务必阅读链接的第一段以获得更多信息 有关为什么不鼓励过度使用此方法的信息
- 无需显式使用
setVisible(true)代码>关于各种
s正如您在代码中所做的,因为 父组件可见,所有子组件也将设置为可见JComponent
- 像
这样的调用必须在 EDT事件调度线程,而不是从主线程调用它们 方法。欲了解更多信息,请阅读pack()/setVisible(true/false)
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);
}
});
}
}
导入java.awt.BorderLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JTabbedPane;
导入javax.swing.JTextField;
导入javax.swing.JTextPane;
导入javax.swing.SwingUtilities;
公共类TestGUI扩展了JFrame{
私有JPanel内容窗格;
专用JTextField jTextField1;
私有JButton jButton1;
静态ArrayList ary=新ArrayList();
私有int tabIndex=0;
静态int指数=0;
私有JTabbedPane选项卡窗格;
公共TestGUI(){
超级(“测试标签框架”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=newjpanel();
setLayout(新的BorderLayout(5,5));
Handler but1=新的Handler();
JPanel footerPanel=新的JPanel();
jTextField1=新的JTextField(10);
footerPanel.add(jTextField1);
jButton1=新JButton(“创建选项卡”);
footerPanel.add(jButton1);
jButton1.addActionListener(but1);
tabbedPane=新的JTabbedPane();
添加(选项卡窗格,BorderLayout.CENTER);
contentPane.add(页脚面板,边框布局,页尾);
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);
}
});
}
}