嘿,大家晚上好,我对名为“提交”的第二个按钮有问题,因为我无法将我输入的所有信息都转移到框架中的空JList中,这是我的代码,到目前为止,我的问题是如果我单击“提交”,我的所有信息都将出现在我的代码中框架中的消息区域需要列出。谢谢
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class vin extends JFrame { JLabel lab = new JLabel("Enter Your Name :"); JLabel lab2 = new JLabel("Enter Your Birthday :"); JLabel lab3 = new JLabel("Enter Your Age:"); JLabel lab4 = new JLabel("Enter Your HomeTown:"); JLabel lab5 = new JLabel("Choose Your Department:"); JButton b1 = new JButton("Exit"); JTextField t1 = new JTextField(15); JTextField t2 = new JTextField(15); JTextField t3 = new JTextField(15); JTextField t4 = new JTextField(15); JButton b2 = new JButton("Submit"); JButton b3 = new JButton("Clear"); JLabel lab6 = new JLabel("Message :"); JList list = new JList(); JPanel panel = new JPanel(); JLabel brief; public vin() { setLocation(500,280); panel.setLayout(null); lab.setBounds(10,10,150,20); t1.setBounds(130,10,150,20); lab5.setBounds(10,40,150,20); lab2.setBounds(10,140,150,20); t2.setBounds(130,140,150,20); lab3.setBounds(10,170,150,20); t3.setBounds(110,170,150,20); lab4.setBounds(10,200,150,20); t4.setBounds(150,200,150,20); lab6.setBounds(10,240,150,20); list.setBounds(50,270,150,20); list.setSize(250,150); b1.setBounds(250,470,150,20); b2.setBounds(60,470,150,20); b3.setBounds(160,470,150,20); b1.setSize(60,30); b2.setSize(75,30); b3.setSize(65,30); panel.add(lab); panel.add(t1); panel.add(lab5); panel.add(lab2); panel.add(t2); panel.add(t3); panel.add(t4); panel.add(lab4); panel.add(lab3); panel.add(lab6); panel.add(b1); panel.add(b2); panel.add(b3); panel.add(list); brief = new JLabel("Goodmorning "+t1+" Today is "+t2+" its your birthday You are now"+t3+" of age You are From"+t4); getContentPane().add(panel); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { Object source = a.getSource(); if(source == b1) { System.exit(0); } } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { Object source = a.getSource(); if(source == b2) { list = new JList(); } } }); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { Object source = a.getSource(); if(source == b3) { t1.setText(""); t2.setText(""); t3.setText(""); t4.setText(""); } } }); } public static void main(String args []) { vin w = new vin(); w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w.setSize(400,600); w.setVisible(true); } }
好吧,据我所知,这将从Fields中获取信息,并将其放入您在那里的列表中
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a) { DefaultListModel listModel = new DefaultListModel(); listModel.addElement(t1.getText()); listModel.addElement(t2.getText()); listModel.addElement(t3.getText()); listModel.addElement(t4.getText()); list.setModel(listModel); } });
这对我来说很简单,所以您可以看到我正在做的事情,我将txtfield放入数组,因此它更容易处理,对您来说很好:)
您可以使用数组和for循环来做到这一点,这样就可以每次摆脱listmodel.add ....等时,都希望酸痛痛楚的信息用户以前使用过的所有信息,例如arraylist。我不确定您的期望如何您的程序在您下达要求后下次运行时,请指出确切的问题以及您希望程序如何详细运行。还请仅发布相关代码,而不是全部。