Gui JList ActionListener [英] Gui JList ActionListener

查看:64
本文介绍了Gui JList ActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,晚安,我遇到第二个名为提交"的按钮的问题,因为我无法将我输入的所有信息传送到框架中的空JList上,这是我的代码,到目前为止,我的问题是如果我单击提交所有信息出现在我的消息"区域的框架中,需要将其列出.谢谢

hey all goodevening i have a problem about my second button named "Submit" because i cant transfer all information i entered to my null JList in the frame here is my code so far my problem is if i clicked submit my all information will appear in my Message area in frame it needs to be list. thanks

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);
    }
}

推荐答案

好吧,据我了解,我将从字段中获取信息,并将其放入您有的列表中

Ok so from what i understand this gonna take informations from Fields and put them into LIST that you have there

b2.addActionListener(new ActionListener() {

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放入数组,这样它更容易的处理对您就可以了:)

this i made simple for you so you can see what im doing i woud put txtfield to array so its easyer bud this will work for you just fine :)

您可以使用数组和for循环来做到这一点,这样就可以每次摆脱listmodel.add ....等时,都将使用户以前使用过的所有信息痛苦,例如arraylist.我不确定请问您希望程序在下一次发布时下次运行的方式,请指出确切的问题以及您希望程序如何详细运行.也请仅发布相关的代码,而不是全部.

You can do it with arrays and for loops so you can get rid off the listmodel.add .... etc each time and when you will want to sore all that info user put previously use for example arraylist.Im not sure how you expect your program to run next time when you post quesiton please point out exact problem and how do you expect program to work in detail.also post just relevant code not all of it.

这篇关于Gui JList ActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆