【发布时间】:2014-05-05 11:39:54
【问题描述】:
我正在尝试制作一个程序,我输入名字和姓氏等,然后将再次查看该信息。
我正在使用 Java 和 Jframe
我已经使用 JTextfileds 和 Jlabels 以及一个按钮设置了我的界面
我正在尝试创建一个对象数组,因此每个数组编号都将包含(名字、姓氏等),然后我将把它打印到 Jlist 中。我想这样做,所以当我按下按钮时,它将在第一个数组中输入我的 Jtextinputs 中的所有内容
但我不知道如何用我已经开始的对象做数组,但我需要帮助谢谢
我知道我需要使用
ArrayList<> mylist = new ArrayList<Data>();
Data myTask = new Data("imput", "input");
myList.add(myTask);
但我不明白我怎么用它
谢谢
那是我的 data.java 文件
package components;
Import Java.Io.Serializable;
public class Data implements Serializable {
static final long serialVersionUID = 1;
String name;
String description;
public Task(String Fname, String Lname) {
this.name = Fname;
this.description = Lname;
}
public String getName() {
return this.name;
}
public String getDescription() {
Return this.description;
}
接口文件
public class DataDemo extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
//Name Label and TextField
protected static JLabel LbName;
protected static JTextField txtInputName;
//Description Label and TextField
protected static JLabel LbDescription;
protected static JTextField txtInputDescription;
//Submit Button
static JButton btnSubmit;
@SuppressWarnings("unchecked")
public DataDemo() {
//Name Label and TextField
LbName = new JLabel("Name:");
txtInputName = new JTextField(200);
//Description Label and TextField
LbDescription = new JLabel("Description:");
txtInputDescription = new JTextField(20);
//Submit Button
btnSubmit = new JButton("Done");
btnSubmit.addActionListener(this);
//Add Components to this container, using the default FlowLayout.
setLayout(null);
//Name
add(LbName);
add(txtInputName);
//Description
add(LbDescription);
add(txtInputDescription);
//button Submit
add(btnSubmit);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == btnSubmit)
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("DataDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
DataDemo newContentPane = new DataDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
//(x,y,with,height)
//Name interface position
LbName.setBounds(50, 70, 200, 25);
txtInputName.setBounds(200, 70, 200, 25);
//Description interface position
LbDescription.setBounds(50, 100, 200, 25);
txtInputDescription.setBounds(200, 100, 200, 25);
//button Submit
btnSubmit.setBounds(200, 150, 200, 25);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
【问题讨论】:
-
txtInputName.setBounds(200,70,200,25);Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。 -
我的问题是我需要从 JTextField 中获取数据,然后将其放入数组中
标签: java arrays swing arraylist