我们如何从swingField中获取价值 [英] how we can get value from textField in swing

查看:66
本文介绍了我们如何从swingField中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究摇摆相关项目,因为我是动态增加文本字段,但我无法为每个文本字段赋予唯一名称。请给我建议如何使用每个文本字段.getText() ;文本方法字段或我们如何为每个文本字段赋予唯一名称

我的代码如下:



  public   class  tt  implements  ActionListener {

public static < span class =code-keyword> void main( String [] args){
SwingUtilities.invokeLater( new Runnable(){
public void run() {
new tt()。createAndShowGUI();
}
});
}

私人 JFrame框架;
public JPanel panel = new JPanel( new GridBagLayout());
public GridBagConstraints constraints = new GridBagConstraints();
public GridBagConstraints constraints1 = new GridBagConstraints();

public 列表字段= new ArrayList();
public 列表fieldButton = new ArrayList();
public 列表fieldFile = new ArrayList();
public JTextField字段;
public JButton button1 = new JButton( 添加部门);
public JButton button2 = new JButton( 添加数据库);
public static int countReport = 1 ;
String files = null;
int y = 2 ;

protected void createAndShowGUI(){
字符串 [] labels = { };
for String label:labels)
addColumn(label);
addRowBelow();
constraints.gridx = 20 ;
constraints.gridy = 20 ;
constraints1.gridx = 30 ;
constraints1.gridy = 20 ;
panel.add(button1,constraints);
panel.add(button2,constraints1);

frame = new JFrame( 动态添加按钮);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(panel));
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
button1.addActionListener( this );
JRootPane root = frame.getRootPane();
root.setDefaultButton(button1);
frame.addWindowListener( new WindowAdapter(){
public void windowClosing(我们是WindowEvent){
System.exit( 0 );
}
});
}

public void addColumn( String labelText){
constraints.gridx = fields.size();
constraints.gridy = 1 ;
panel.add( new JLabel(labelText),constraints);
constraints.gridy = 2 ;
field = new JTextField( 40 );
String str = field.getText();
System.out.println( 在列块 + str);
// field.setEditable(false); s
panel.add(场,约束);
fields.add(field);
}

public void addRowBelow(){
y ++;
constraints.gridy = y;
// System.out.println(fields.size());
for int x = 0 ; x< fields.size(); x ++){
constraints.gridx = x;
JTextField field = new JTextField( 40 );
// field.setEditable(false);
panel.add(field ,约束);
constraints.gridx = x + 1 ;


}
}

public void actionPerformed(ActionEvent ae){
if Add Dept .equals(ae.getActionCommand())){


addRowBelow();
field.getText();
System.out.println(field.getText());
frame.pack();
frame.setLocationRelativeTo(null);

}
}
}







我的代码工作正常,但我无法得到动态文本的值字段

解决方案

这里有两个TextField,名称字段一个是Local,另一个是Global和In Action Listener你正在访问的是`field`是Global(类的Datamember),如果你想获得`addrowbelow`方法中的文本字段的值,那么你必须在全局范围(我的意思是在类内)中使用不同的JTextField对象名称

i am working on swing related project in that i am increase text Field dynamically but i am not able to give unique name to each text field .please give me suggestion how can i use each text Field .getText(); method for text Field or how we can give unique name to the each text field
my code bellow


public class tt implements ActionListener {

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new tt().createAndShowGUI();
			}
		});
	}

	private JFrame frame;
	public JPanel panel = new JPanel(new GridBagLayout());
	public GridBagConstraints constraints = new GridBagConstraints();
	public GridBagConstraints constraints1 = new GridBagConstraints();

	public List fields = new ArrayList();
	public List fieldButton = new ArrayList();
	public List fieldFile = new ArrayList();
public JTextField  field;
	public JButton button1 = new JButton("Add Dept");
	public JButton button2 = new JButton("Add DataBase");
	public static int countReport = 1;
	String files = null;
	int y = 2;

	protected void createAndShowGUI() {
		String[] labels = { "" };
		for (String label : labels)
			addColumn(label);
		addRowBelow();
		constraints.gridx = 20;
		constraints.gridy = 20;
		constraints1.gridx = 30;
		constraints1.gridy = 20;
		panel.add(button1, constraints);
		panel.add(button2, constraints1);

		frame = new JFrame("Add Button Dynamically");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new JScrollPane(panel));
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		button1.addActionListener(this);
JRootPane root = frame.getRootPane();
		root.setDefaultButton(button1);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	}

	public void addColumn(String labelText) {
		constraints.gridx = fields.size();
		constraints.gridy = 1;
		panel.add(new JLabel(labelText), constraints);
		constraints.gridy = 2;
	 field = new JTextField(40);
		String str=field.getText();
		System.out.println("in column block"+str);
		// field.setEditable(false);s
		panel.add(field, constraints);
		fields.add(field);
		}

	public void addRowBelow() {
		y++;
		constraints.gridy = y;
		// System.out.println(fields.size());
		for (int x = 0; x < fields.size(); x++) {
			constraints.gridx = x;
		JTextField field = new JTextField(40);
			// field.setEditable(false);
			panel.add(field, constraints);
			constraints.gridx = x + 1;


		}
	}

	public void actionPerformed(ActionEvent ae) {
		if ("Add Dept".equals(ae.getActionCommand())) {
			
			
			addRowBelow();
			field.getText();
			System.out.println(field.getText());
			frame.pack();
			frame.setLocationRelativeTo(null);
			
		}
	}
}




my code working fine but i cant able getvalue of dynamic text Field

解决方案

Here You Have Two TextField with name field one is Local And Another is Global And In Action Listener Which One You Are Accessing is that `field` is Global(Datamember of class) if you want to get value of text field that is in `addrowbelow` method then you must take that JTextField Object In Global Scope(i means inside class) with different name


这篇关于我们如何从swingField中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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