类SampleFiveA扩展JPanel。这包含一个在另一个之下的文本字段,每个文本字段在左侧都有一个标签。所有文本框的宽度都是相同的,并且位于面板的右侧边框上。 SampleFiveA具有接受以下三个参数只有一个构造函数:在JPanel中使用ArrayList
ArrayList的名称, ArrayList的价值观, INT COLS
我创建至今在GUI样品的用户名密码,屏幕上,但现在我已经实现一个ArrayList问题在JPanel中为用户名而另一个为密码。在那里呆了几个小时,现在找不到一个适当的例子来做到这一点。下面是我使用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);
};
};
需要了解的重要事项:您无法创建ArrayLists'name','values'或int'cols'。它们被传递给你的构造函数,然后将它们分配给你的对象自己的变量。然后,您的构造函数的签名是'public SampleFiveA(ArrayList names,ArrayList values,int cols)'。我不知道你在这里需要达到什么目的,但我怀疑它会涉及到从这些提供的列表中读/写。 – Doug 2011-05-13 16:54:17
你知道如何迭代你的列表吗?如果没有,请阅读[for loops](http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html)。 – Jeremy 2011-05-13 16:54:25