【问题标题】:JTextfield array, retrieve name and textJTextfield 数组,检索名称和文本
【发布时间】:2020-03-28 12:50:49
【问题描述】:

我有一个JTextField 数组,当JTextField 被修改时,我想要JTextField 的名称和文本。

while ((line = bufferedReader.readLine()) != null) { // 1 by 1 line of file
    if (f == 6) {
        g++;
        f = 0;
    }
    tableauDonnee[g][f] = line;
    fields[g][f] = new JTextField(tableauDonnee[g][f]);
    fields[g][f].setName(String.valueOf(g + etf));
    fields[g][f].setBounds(positionY, positionX, 160, 40);
    pan.add(fields[g][f]);
    positionY = positionY + 180;
    f++;

在将JTextField 添加到面板之前我可以这样做

fields[g][f].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        textfield = fields[g][f].getText();
        nameTextfield = fields[g][f].getName();
    }
});

但它不起作用,因为gf 的值不好。

我被卡住了,不知道该怎么办。

【问题讨论】:

    标签: java arrays swing actionlistener jtextfield


    【解决方案1】:

    您可以从ActionEvent 获取事件的来源(即您按下 Enter 键的文本字段):

    public void actionPerformed(ActionEvent event) 
    {
        JTextField textField = (JTextField)event.getSource();
        ...
    }
    

    另外,为什么要使用文本字段的二维数组。

    我建议使用JTable 将是更好的组件。阅读 How to Use Tables 上的 Swing 教程部分了解更多信息。

    【讨论】:

    • 如果我使用它,它会返回 javax.swing.JTextField[5,750,110,160x40,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax .swing.plaf.BorderUIResource$CompoundBorderUIResource@4fa1d375,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing .plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],sel等我在哪里可以检索名称和文本。顺便说一句,textfield 是一个字符串而不是 jtextfield
    • 对不起,你的天才。我将文本字段作为 jtexfield,然后从文本字段中检索名称和文本
    猜你喜欢
    • 1970-01-01
    • 2020-02-26
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2012-12-30
    • 1970-01-01
    相关资源
    最近更新 更多