在 JPanel 中使用 ArrayList
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以使用,
但也许你应该考虑一种更好的数据结构,例如 HashMap 和
(并且你应该基于某些事件来执行此操作,例如用户按下按钮,而不是在构造函数中,否则该值将是你设置的值前)
you can use
but maybe you should think about a better data structure, e.g. a HashMap and
(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)
这是您的一个开始。
它将
FocusListener
添加到文本字段,并确保当文本字段失去焦点时将ArrayList
的内容更新为当前值。Here's a start for you.
It adds a
FocusListener
to the text fields and makes sure that the content of theArrayList
is updated with the current value when the text field looses focus.我确定你想做什么。您要么想将 JLabel 放入 ArrayList 中,要么将其文本放入该标签中。
如果要将整个 JLabel 放入 ArrayList 中,则应该创建一个 ArrayList。但我认为您希望从 JLabel 获取文本,因此您应该编写
names.add(lab1.getText());
。您创建的构造函数不带任何参数。您编写的参数是实例变量,这意味着这些是该类的任何实例都将具有的变量。如果您想在构造函数中传递参数,您应该按照 thasc 告诉您的操作进行操作。
您可以这样写:
但是由于您已经创建了 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 writenames.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:
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