简体   繁体   English

如何在此代码中为每个文本字段和标签命名? JAVA

[英]How to give each textfield and label a name in this code? JAVA

below I have pasted my java code which I need a bit help with. 下面我粘贴了我需要帮助的java代码。 How would I refer to each individual textbox and label? 我如何引用每个单独的文本框和标签? eg I have created a code that loops through the array list and create appropriate amount of labels and textfields in my case below it creates 3 of each. 例如,我创建了一个循环遍历数组列表的代码,并在我的情况下创建适当数量的标签和文本字段,在它下面创建了3个。 The problem I am having is, how would I refer to each of these components? 我遇到的问题是,我如何参考这些组件? for example, if i want to get the value of my second textbox how would I do that? 例如,如果我想获得第二个文本框的值,我该怎么做? is there any way to give each textbox and textfield a name. 有没有办法给每个文本框和文本字段一个名称。 eg say first label name is label1, second is label2 and so on and first textbox name is field1, second box name is field2 and so on. 例如,假设第一个标签名称是label1,第二个是label2,依此类推,第一个文本框名称是field1,第二个框名称是field2,依此类推。

Retrieve them from the Map with their associated key. 使用关联的密钥从Map中检索它们。

String textOfFirst = fieldMap.get("Text1").getText();
fieldMap.get("Text2").setEnabled(false);

But as my opinion, it seems like with this kind of group creation you should have it set up so you don't need to refer to them individually. 但正如我的观点,似乎有这种组创建你应该设置它,所以你不需要单独引用它们。

Here's an example you can run. 这是一个你可以运行的例子。 I used a Map as you were trying to do. 我正在尝试使用Map There may be a lot of code, but what your interested in is this 可能有很多代码,但您感兴趣的是这个

Map<String, JTextField> fields;
....
JTextField field = new JTextField(15);
fields.put(fieldName, field);
....
public class ButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource();
        String fieldName = button.getText();
        JTextField field = fields.get(fieldName);
        System.out.println(field.getText());
    }
}

I just put the JTextField in a Map . 我只是将JTextField放在Map When I want to access it, I just use JTextField field = fields.get("textfield name"); 当我想访问它时,我只使用JTextField field = fields.get("textfield name");

In the example below, just type in something to the field, then click the button next to it. 在下面的示例中,只需在字段中键入内容,然后单击旁边的按钮。 You will see that the button has the same name as the text field key in the map, so I just use that to print the value in the text field 您将看到该按钮与地图中的文本字段键具有相同的名称,因此我只是使用它来打印文本字段中的值

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test2 {

    Map<String, JTextField> fields;
    Map<String, JLabel> labels;

    public Test2() {
        fields = new HashMap<>();
        labels = new HashMap<>();

        JPanel mainPanel = new JPanel(new GridLayout(10, 1));
        for (int i = 1; i <= 10; i++) {
            JPanel panel = createPanel("Text Field " + i);
            mainPanel.add(panel);
        }

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setVisible(true);

    }

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

    private JPanel createPanel(String fieldName) {
        JPanel panel = new JPanel();
        JTextField field = new JTextField(15);
        field.addActionListener(new FieldListener());
        fields.put(fieldName, field);

        JLabel label = new JLabel(fieldName);
        label.addMouseListener(new MouseHandler());
        labels.put(fieldName, label);

        JButton button = new JButton(fieldName);
        button.addActionListener(new ButtonListener());

        panel.add(label);
        panel.add(field);
        panel.add(button);

        return panel;
    }

    public class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton)e.getSource();
            String fieldName = button.getText();
            JTextField field = fields.get(fieldName);
            System.out.println(field.getText());
        }
    }

    public class FieldListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField field = (JTextField)e.getSource();
            System.out.println(field.getText());
        }
    }

    public class MouseHandler extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            JLabel label = (JLabel)e.getSource();
            System.out.println(label.getText());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM