在 JPanel 中使用 ArrayList

发布于 2024-11-07 06:31:48 字数 1612 浏览 0 评论 0原文

SampleFiveA 类扩展了 JPanel。其中包含文本字段,一个在另一个下面,每个文本字段的左侧都有一个标签。所有文本字段的宽度都相同,并且位于面板的右边框上。 SampleFiveA 只有一个构造函数,它接受以下三个参数:

ArrayList 名称、 数组列表值, int cols

到目前为止,我在 GUI 中创建了示例用户名密码屏幕,但现在我在 JPanel 中实现 ArrayList 时遇到问题,一个用于用户名,另一个用于密码。有点卡在那里几个小时了,现在找不到合适的例子来做到这一点。下面是我评论了我需要使用 ArrayList 完成的事情的代码。

public class SampleFiveA extends JPanel {


ArrayList<String> names = new ArrayList<String>(); //the text for the labels
ArrayList<String> values = new ArrayList<String>(); // the initial contents of the text fields
int col ; //the number of columns used to set the width of each textfield


public SampleFiveA()
{
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2,2)); 

        JLabel lab1 = new JLabel("User Name", JLabel.LEFT);         
        p.add(lab1 = new JLabel("User Name"));
        JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
        p.add(txt1= new JTextField());



        JLabel lab2 = new JLabel("Password ", JLabel.LEFT);            
        p.add(lab2 = new JLabel("Password"));
        JPasswordField txt2 = new JPasswordField("*****",JPasswordField.RIGHT );
        p.add(txt2 = new JPasswordField());



      //  names.add(lab1,lab2);// Not right but I want to put the label text to this arrayList
      //  values.add(txt1,txt2);// 


        add(p);
};



public static void main(String[] args) 
{


JFrame frame = new JFrame();
frame.getContentPane().add(new SampleFiveA());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);


};
};

Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which has a label on the left. All textfields will be of the same width and positioned against the right border of the panel. SampleFiveA has only one constructor that accepts the following three parameters:

ArrayList names,
ArrayList values,
int cols

I so far created the sample username password screen in GUI but now I have a problem implementing an ArrayList in JPanel one for User Name and the other for password. Kind of stuck there for hours now cant find a proper example to do it. Below is the code I commented what I need to be done using ArrayList.

public class SampleFiveA extends JPanel {


ArrayList<String> names = new ArrayList<String>(); //the text for the labels
ArrayList<String> values = new ArrayList<String>(); // the initial contents of the text fields
int col ; //the number of columns used to set the width of each textfield


public SampleFiveA()
{
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2,2)); 

        JLabel lab1 = new JLabel("User Name", JLabel.LEFT);         
        p.add(lab1 = new JLabel("User Name"));
        JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
        p.add(txt1= new JTextField());



        JLabel lab2 = new JLabel("Password ", JLabel.LEFT);            
        p.add(lab2 = new JLabel("Password"));
        JPasswordField txt2 = new JPasswordField("*****",JPasswordField.RIGHT );
        p.add(txt2 = new JPasswordField());



      //  names.add(lab1,lab2);// Not right but I want to put the label text to this arrayList
      //  values.add(txt1,txt2);// 


        add(p);
};



public static void main(String[] args) 
{


JFrame frame = new JFrame();
frame.getContentPane().add(new SampleFiveA());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);


};
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆橘 2024-11-14 06:31:48

你可以使用,

names.add(txt1.getText());
values.add(txt2.getText());

但也许你应该考虑一种更好的数据结构,例如 HashMap 和

hashmap.put(txt1.getText(),txt2.getText())

(并且你应该基于某些事件来执行此操作,例如用户按下按钮,而不是在构造函数中,否则该值将是你设置的值前)

you can use

names.add(txt1.getText());
values.add(txt2.getText());

but maybe you should think about a better data structure, e.g. a HashMap and

hashmap.put(txt1.getText(),txt2.getText())

(and you should do this based on some event,e.g. user presses a button, not in the constructor, as otherwise the value will be the one you set before)

暮色兮凉城 2024-11-14 06:31:48

这是您的一个开始。

它将 FocusListener 添加到文本字段,并确保当文本字段失去焦点时将 ArrayList 的内容更新为当前值。

import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

public class Main extends JPanel {

    ArrayList<String> names = new ArrayList<String>(); // the text for the
                                                       // labels
    ArrayList<String> values = new ArrayList<String>(); // the initial contents
                                                        // of the text fields
    int col; // the number of columns used to set the width of each textfield

    public Main() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2, 2));

        names = new ArrayList<String>();
        values = new ArrayList<String>();


        JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
        p.add(lab1);
        JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
        p.add(txt1);

        names.add(lab1.getText());
        values.add(txt1.getText());

        JLabel lab2 = new JLabel("Password ", JLabel.LEFT);
        p.add(lab2);
        JPasswordField txt2 = new JPasswordField("*****", JPasswordField.RIGHT);
        p.add(txt2);
        names.add(lab2.getText());
        values.add(txt2.getText());


        // names.add(lab1,lab2);// Not right but I want to put the label text to
        // this arrayList
        // values.add(txt1,txt2);//
        txt1.addFocusListener(new ArrayListFocusListener(txt1, values, 0));
        txt2.addFocusListener(new ArrayListFocusListener(txt2, values, 1));

        add(p);


        // Start a thread to print the content of the list for 10 seconds
        new Thread() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println(values);
                }
            }
        }.start();


    };



    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.getContentPane().add(new Main());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);

    };

    class ArrayListFocusListener implements FocusListener {
        JTextField textField;
        ArrayList<String> backingList;
        int myIndex;

        public ArrayListFocusListener(JTextField textField,
                ArrayList<String> backingList, int myIndex) {
            this.textField = textField;
            this.backingList = backingList;
            this.myIndex = myIndex;
        }

        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            backingList.set(myIndex, textField.getText());
        }

    }
};

Here's a start for you.

It adds a FocusListener to the text fields and makes sure that the content of the ArrayList is updated with the current value when the text field looses focus.

import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

public class Main extends JPanel {

    ArrayList<String> names = new ArrayList<String>(); // the text for the
                                                       // labels
    ArrayList<String> values = new ArrayList<String>(); // the initial contents
                                                        // of the text fields
    int col; // the number of columns used to set the width of each textfield

    public Main() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2, 2));

        names = new ArrayList<String>();
        values = new ArrayList<String>();


        JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
        p.add(lab1);
        JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
        p.add(txt1);

        names.add(lab1.getText());
        values.add(txt1.getText());

        JLabel lab2 = new JLabel("Password ", JLabel.LEFT);
        p.add(lab2);
        JPasswordField txt2 = new JPasswordField("*****", JPasswordField.RIGHT);
        p.add(txt2);
        names.add(lab2.getText());
        values.add(txt2.getText());


        // names.add(lab1,lab2);// Not right but I want to put the label text to
        // this arrayList
        // values.add(txt1,txt2);//
        txt1.addFocusListener(new ArrayListFocusListener(txt1, values, 0));
        txt2.addFocusListener(new ArrayListFocusListener(txt2, values, 1));

        add(p);


        // Start a thread to print the content of the list for 10 seconds
        new Thread() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println(values);
                }
            }
        }.start();


    };



    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.getContentPane().add(new Main());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);

    };

    class ArrayListFocusListener implements FocusListener {
        JTextField textField;
        ArrayList<String> backingList;
        int myIndex;

        public ArrayListFocusListener(JTextField textField,
                ArrayList<String> backingList, int myIndex) {
            this.textField = textField;
            this.backingList = backingList;
            this.myIndex = myIndex;
        }

        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            backingList.set(myIndex, textField.getText());
        }

    }
};
李不 2024-11-14 06:31:48

我确定你想做什么。您要么想将 JLabel 放入 ArrayList 中,要么将其文本放入该标签中。

如果要将整个 JLabel 放入 ArrayList 中,则应该创建一个 ArrayList。但我认为您希望从 JLabel 获取文本,因此您应该编写 names.add(lab1.getText());

您创建的构造函数不带任何参数。您编写的参数是实例变量,这意味着这些是该类的任何实例都将具有的变量。如果您想在构造函数中传递参数,您应该按照 thasc 告诉您的操作进行操作。

您可以这样写:

JLabel lab1 = new JLabel("User Name", JLabel.LEFT);         
p.add(lab1 = new JLabel("User Name"));

但是由于您已经创建了 lab1 JLabel,因此您可以只编写 p.add(lab1)

最后一点,我认为 SampleFiveA 应该更好地扩展 JFrame ,除非您希望它扩展 JPanel 以在其他地方使用它。如果您需要它是独立的,您应该更改它。

干杯

I'm sure what you are trying to do. You either want to put the JLabel in an ArrayList or the text of that label.

If you want to put the whole JLabel in an ArrayList, you should make a ArrayList<JLabel>. But I take it you want to get the text from the JLabel, so you should write names.add(lab1.getText());.

The constructor you have made doesn't take any parameters. The parameters you have wrote are the instance variable, meaning those are the variables any instance of that class will have. If you want to pass parameters in your constructor you should do what thasc told you.

You write:

JLabel lab1 = new JLabel("User Name", JLabel.LEFT);         
p.add(lab1 = new JLabel("User Name"));

But since you are already creating the lab1 JLabel you could just write p.add(lab1).

And a final note I think SampleFiveA should better extend JFrame unless you want it to extend JPanel to use it somewhere else. If you need it to be standalone you should change that.

cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文