简体   繁体   English

Gui JList ActionListener

[英]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. 嘿所有goodevening我有一个关于我的第二个按钮名为“提交”的问题,因为我无法将我输入的所有信息传输到框架中的我的空JList这里是我的代码到目前为止我的问题是如果我点击提交我的所有信息将出现在我的帧中的消息区域需要列出。 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 好吧,据我所知,这将从Fields获取信息,并将它们放入LIST,你在那里

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


        }
    });

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 :) 这对我来说很简单,所以你可以看到我在做什么我把txtfield放到数组所以它的easyer芽这对你很有用:)

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. 你可以用数组和for循环这样做,这样你每次都可以摆脱listmodel.add ....等,当你想要把以前用过的所有信息用作例如arraylist。我不确定你的期望您下次发布问题时运行的程序请指出确切的问题以及您希望程序如何详细工作。还要发布相关代码而不是全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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