我不知道如何从这个例子中的文本字段中获取值请帮帮我 [英] i don't know how to get the values from text field in this example please help me

查看:48
本文介绍了我不知道如何从这个例子中的文本字段中获取值请帮帮我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java swings program



java swings program

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JFrameExample
{   
    private JFrame frame;
    private JButton button;
    private JTextField tfield;
    private String nameTField;
    private int count;

    public JFrameExample()
    {
        nameTField = "tField";
        count = 0;
    }

    private void displayGUI()
    {
        frame = new JFrame("JFrame Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0, 1, 2, 2));
        button = new JButton("Add JTextField");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                tfield = new JTextField();
                tfield.setName(nameTField + count);
                count++;
                frame.add(tfield);
                frame.revalidate();  // For JDK 1.7 or above.
                //frame.getContentPane().revalidate(); // For JDK 1.6 or below.
                frame.repaint();
            }
        });
        frame.add(button);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new JFrameExample().displayGUI();
            }
        });
    }
}





这里我动态生成文本字段我有问题,我没有得到因文本字段名称而在文本字段中输入的值是动态生成的,请建议我.................,





[edit]已添加代码块 - OriginalGriff [/ edit]



here i generate text fields dynamically i have problem here, i don''t get the values entered in text fields because of text fields names are dynamically generated please suggest me .................,


[edit]Code block added - OriginalGriff[/edit]

推荐答案

您正在设置JTextField的名称 - 很好。您应该可以参考:



版本1:

当您离开文本字段时,FocusListener会做出反应:



You are setting the name of the JTextField - fine. You should be able to refer to that:

Version 1:
A FocusListener reacts when you leave the textfield:

public class JFrameExample {
	private JFrame frame;
	private JButton button;
	private final String nameTField = "tField"; // fix that sh*t
	private int count;

	public JFrameExample() {
		count = 0;
	}

	private void displayGUI() {
		frame = new JFrame("JFrame Example");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(0, 1, 2, 2));
		button = new JButton("Add JTextField");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				// only to be created at runtime, does not need to be a variable
				// much saver
				JTextField tfield = new JTextField(); 
				tfield.setName(nameTField + count);
				tfield.addFocusListener(new MyFocusListener());
				count++;
				frame.add(tfield);
				// frame.revalidate(); // For JDK 1.7 or above.
				frame.getContentPane().validate(); // For JDK 1.6 or below.
				frame.repaint();
			}
		});
		frame.add(button);
		frame.pack();
		frame.setLocationByPlatform(true);
		frame.setVisible(true);
	}

	public static void main(String... args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new JFrameExample().displayGUI();
				
			}
		});
	}
	
	private class MyFocusListener implements FocusListener{

		@Override
		public void focusLost(FocusEvent event) {
			if(event.getSource() instanceof JTextField){ // component is JTextfield
				JTextField txtField = (JTextField)event.getSource();
				System.out.println("Hello, I'm TextField " + txtField.getName());
				System.out.println("My Value is:" + txtField.getText());
				// ACCESS txtField.getText() HERE!!!

			}
		}

		@Override
		public void focusGained(FocusEvent event) { }
	}
}





版本2:

键入时KeyListener会做出反应。好处:您可以随时参考该值。只有当你需要随时引用它时才使用这个版本(例如即时搜索),因为它会在每次按键击中时触发一个事件:





Version 2:
A KeyListener reacts when you type. Benefit: you can refer to the value at any time. Only use this version when you need to refer to it any time (e.g. instant search), cause it fires an event on every button stroke:

public class JFrameExample {
	private JFrame frame;
	private JButton button;
	private final String nameTField = "tField"; // fix that sh*t
	private int count;

	public JFrameExample() {
		count = 0;
	}

	private void displayGUI() {
		frame = new JFrame("JFrame Example");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(0, 1, 2, 2));
		button = new JButton("Add JTextField");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				// only to be created at runtime, does not need to be a variable
				// much saver
				JTextField tfield = new JTextField(); 
				tfield.setName(nameTField + count);
				tfield.addKeyListener(new MyKeyListener());
				count++;
				frame.add(tfield);
				// frame.revalidate(); // For JDK 1.7 or above.
				frame.getContentPane().validate(); // For JDK 1.6 or below.
				frame.repaint();
			}
		});
		frame.add(button);
		frame.pack();
		frame.setLocationByPlatform(true);
		frame.setVisible(true);
	}

	public static void main(String... args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new JFrameExample().displayGUI();
				
			}
		});
	}
	
	private class MyKeyListener implements KeyListener{

		@Override
		public void keyReleased(KeyEvent event) {
			if(event.getSource() instanceof JTextField){ // component is JTextfield
				JTextField txtField = (JTextField)event.getSource();
				System.out.println("Hello, I'm TextField " + txtField.getName());
				System.out.println("My Value is:" + txtField.getText());
				// ACCESS txtField.getText() HERE!!!
			}
		}

		@Override
		public void keyTyped(KeyEvent e) { }

		@Override
		public void keyPressed(KeyEvent e) { }
	}
}





监听器中的代码是相同的。

我希望你看到我改变了TextField只是为了创建Actionlistener中的本地 - 不需要成为成员。字符串 nameTField - 根本不允许更改,所以它应该是最终的,以防止这种情况发生。



最后一般Lik使用Listeners - http://docs.oracle .com / javase / tutorial / uiswing / events / index.html [ ^ ]

我建议给它添加书签,教程有很多信息并写得很好。



The code in the Listener is the same.
I hope you see that I''ve changed the TextField only to be created local in the Actionlistener - that one does not need to be a member. Also the String nameTField - that one is not allowed to change at all, so it''s supposed to be final to prevent that.

Finally the general Lik to using Listeners - http://docs.oracle.com/javase/tutorial/uiswing/events/index.html[^]
I recommend to bookmark that, the Tutorials have pretty much info and are written great.


这篇关于我不知道如何从这个例子中的文本字段中获取值请帮帮我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆