简体   繁体   中英

Gui JList ActionListener

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() {

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


        }
    });

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM