gpt4 book ai didi

java - Gui JList Action 监听器

转载 作者:行者123 更新时间:2023-12-02 06:12:17 26 4
gpt4 key购买 nike

嘿大家晚上好,我对名为“提交”的第二个按钮有一个问题,因为我无法将我输入的所有信息传输到框架中的空 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);
}
}

最佳答案

好吧,据我了解,这将从字段中获取信息并将它们放入您在那里的列表中

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)时。我不确定当您发布问题时,您希望您的程序下次如何运行,请指出确切的问题以及您希望程序如何详细工作。也只发布相关代码而不是全部。

关于java - Gui JList Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21780427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com