从JTextField获取数据,该数据是几个局部变量之一 [英] Getting data from JTextField that is one of several local variables

查看:176
本文介绍了从JTextField获取数据,该数据是几个局部变量之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在读取一个文件,然后得到该文件中的行数.基于此,我生成了我的界面.现在,我需要具有编辑值对话框UI的功能.行是具有输入文档行的方式的变量.当然,下面的代码不起作用.我想向读取的数组中写入新值.

So I am reading a file and I get the amount of lines in that file. Based on that I generate my interface. Now I need to have the ability to edit valus trougth UI . Rows is the variable that has how lines the input document has . Of course the code below doesnt work . I want to write new values to the array from which i read .

for(int i=0;i<Rows;i++)
{
    //System.out.println("!"+Symbol[1]+"!");
    //if(Symbol[i]!=""&&Symbol[i]!=null)
    // {
    JTextField symbol = new JTextField(6);
    symbol.setText(Symbol[i]);
    symbol.setBounds(10,25*i+10 , 75, 20);
    symbol.setEditable(false);
    frame.add(symbol);
    JTextField buyf = new JTextField(4);
    buyf.setText(String.valueOf(buy[i]));
    buyf.setBounds(95, 25*i+10, 50, 20);
    buyf.setEditable(true);
    buyf.addActionListener(new java.awt.event.ActionListener() { 
         public void actionPerformed(ActionEvent ae) {  
              buy[i]=Integer.parseInt(buyf.getText());
         }
    });                      
    frame.add(buyf);
} 

推荐答案

  1. 请勿使用 AbsoluteLayout ,例如symbol.setBounds(10,25*i+10 , 75, 20);使用正确的LayoutManager,也许 GridLayout 最好为了你的...

  1. don't use AbsoluteLayout e.g. symbol.setBounds(10,25*i+10 , 75, 20); use proper LayoutManager, maybe GridLayout is best for your ...

使用 DocumentListener 来监听 JTextComponents

使用带有数字格式设置符的JFormattedTextField ,而是比普通的 JTextField ,那么您可以删除有关parseWhatever的所有内容

use JFormattedTextField with Number formatter, rather than plain JTextField, then you can remove everything about parseWhatever

您可以使用普通的 JTextField ,但是与DocumentFilter(删除非数字字符)

you can to use plain JTextField but with DocumentFilter (remove non numberic chars)

ActionListener 可能是正确的Listener另一种选择是在第二点DocumentListener

ActionListener could be correct Listener an alternative is DocumentListener in point second

为了获得更好的帮助,请尽早发布 SSCCE ,但我认为DocumentListener可以解决此问题

for better help sooner post an SSCCE, but I think that DocumentListener can solve that

这篇关于从JTextField获取数据,该数据是几个局部变量之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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