使用Jradio按钮显示集合中的项目 [英] Displaying an item from a collection using Jradio button

查看:59
本文介绍了使用Jradio按钮显示集合中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习Java一段时间了,甚至还买了几本书来帮助我.我现在正要在旅程中想了解有关收藏的知识.我能够以一种非常基本的方式使用数组列表,但是我想了解它们与J单选按钮和其他swing组件一起使用时的工作方式.我一直在工作,观看YouTube视频并阅读诸如"Java编程简介-全面,10E"和"Java in a Nutshell-(6th E)O'Reilly"之类的书,但是我没有找到解决我问题的答案.通过单独使用以下代码,我可以获得在J文本区域中显示的预定义信息. 人"称第二类,在那里我有我的二传手和二传手.

I been learning about java for some time now and even bought several books to help me along. i am now in at the point in my journey that i want to learn about collections. i am able to use array lists in a very basic ways but i would like to understand how they work when use in conjunction with J radio buttons and other swing components. I been working, watching YouTube videos and reading books such as "Introduction to Java Programming- Comprehensive, 10E" and "Java in a Nutshell - (6th E) O'Reilly " but i have not found the answer to my problem. i can get the predefined information to display in the J text area by using the following code individually. "Person" calls a second class where I have my setters and getters.

private void LuisActionPerformed(java.awt.event.ActionEvent evt) {                                     
    Person Luis = new Person("Luis ", "Peres\n", 1953);
    Result.setText(String.valueOf(Luis));
}   

但是,正如你们看到的那样,这不是使用列表,而是我在每个用户执行的单独事件中分别编写了每个用户.这是包含收集/列表的代码.

But as you guys can see this is not using lists, instead i am writting each user individually in their individual action performed events. here is the code tha includes the collection/ list.

import java.util.*;

public class FinalProject extends javax.swing.JFrame {


    public FinalProject() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        DisplayText = new javax.swing.JTextArea();
        Exit = new javax.swing.JButton();
        ClearText = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        Maria = new javax.swing.JRadioButton();
        Jose = new javax.swing.JRadioButton();
        Josh = new javax.swing.JRadioButton();
        Dahn = new javax.swing.JRadioButton();
        Terry = new javax.swing.JRadioButton();
        Clark = new javax.swing.JRadioButton();
        Sasha = new javax.swing.JRadioButton();
        Paul = new javax.swing.JRadioButton();
        Irlen = new javax.swing.JRadioButton();
        Mary = new javax.swing.JRadioButton();
        Puttin = new javax.swing.JRadioButton();
        James = new javax.swing.JRadioButton();
        Jack = new javax.swing.JRadioButton();
        Mark = new javax.swing.JRadioButton();
        Jane = new javax.swing.JRadioButton();
        Sal = new javax.swing.JRadioButton();
        Perry = new javax.swing.JRadioButton();
        Charley = new javax.swing.JRadioButton();
        Anna = new javax.swing.JRadioButton();
        Bruce = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        DisplayText.setEditable(false);
        DisplayText.setColumns(20);
        DisplayText.setLineWrap(true);
        DisplayText.setRows(5);
        DisplayText.setWrapStyleWord(true);
        jScrollPane1.setViewportView(DisplayText);

        Exit.setText("Exit");
        Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitActionPerformed(evt);
            }
        });

        ClearText.setText("Clear Text");
        ClearText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearTextActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(195, 205, 226));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(153, 153, 255), new java.awt.Color(51, 51, 51)));

        buttonGroup1.add(Maria);
        Maria.setText("Maria");
        Maria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MariaActionPerformed(evt);
            }
        });

        buttonGroup1.add(Jose);
        Jose.setText("Jose");
        Jose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JoseActionPerformed(evt);
            }
        });

        buttonGroup1.add(Josh);
        Josh.setText("Josh");
        Josh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JoshActionPerformed(evt);
            }
        });

        buttonGroup1.add(Dahn);
        Dahn.setText("Dahn");
        Dahn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DahnActionPerformed(evt);
            }
        });

        buttonGroup1.add(Terry);
        Terry.setText("Terry");

        buttonGroup1.add(Clark);
        Clark.setText("Clark");

        buttonGroup1.add(Sasha);
        Sasha.setText("Sasha");

        buttonGroup1.add(Paul);
        Paul.setText("Paul");

        buttonGroup1.add(Irlen);
        Irlen.setText("Irlen");

        buttonGroup1.add(Mary);
        Mary.setText("Mary");

        buttonGroup1.add(Puttin);
        Puttin.setText("Puttin");

        buttonGroup1.add(James);
        James.setText("James");

        buttonGroup1.add(Jack);
        Jack.setText("Jack");

        buttonGroup1.add(Mark);
        Mark.setText("Mark");

        buttonGroup1.add(Jane);
        Jane.setText("Jane");

        buttonGroup1.add(Sal);
        Sal.setText("Sal");

        buttonGroup1.add(Perry);
        Perry.setText("Perry");

        buttonGroup1.add(Charley);
        Charley.setText("Charley");

        buttonGroup1.add(Anna);
        Anna.setText("Anna");

        buttonGroup1.add(Bruce);
        Bruce.setText("Bruce");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Perry)
                    .addComponent(Jack)
                    .addComponent(Irlen)
                    .addComponent(Terry)
                    .addComponent(Maria))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Charley)
                    .addComponent(Mark)
                    .addComponent(Jose)
                    .addComponent(Clark)
                    .addComponent(Mary))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Anna)
                    .addComponent(Jane)
                    .addComponent(Puttin)
                    .addComponent(Sasha)
                    .addComponent(Josh))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Sal)
                    .addComponent(Paul)
                    .addComponent(James)
                    .addComponent(Dahn)
                    .addComponent(Bruce))
                .addGap(18, 18, 18))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Josh)
                    .addComponent(Dahn)
                    .addComponent(Jose)
                    .addComponent(Maria))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Paul)
                    .addComponent(Sasha)
                    .addComponent(Clark)
                    .addComponent(Terry))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(James)
                    .addComponent(Puttin)
                    .addComponent(Mary)
                    .addComponent(Irlen))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Sal)
                    .addComponent(Jane)
                    .addComponent(Jack)
                    .addComponent(Mark))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Bruce)
                    .addComponent(Anna)
                    .addComponent(Charley)
                    .addComponent(Perry))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ClearText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Exit))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Exit)
                    .addComponent(ClearText))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void ExitActionPerformed(java.awt.event.ActionEvent evt) {                                     
        System.exit(0);
    }                                    

    private void ClearTextActionPerformed(java.awt.event.ActionEvent evt) {                                          
        DisplayText.setText("");
    }                                         

    private void MariaActionPerformed(java.awt.event.ActionEvent evt) {                                      


    }                                     

    private void JoseActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    private void JoshActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    private void DahnActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FinalProject().setVisible(true);
            }
        });
        // list where all user reside 
        List<Person> ml = new ArrayList<>();
        ml.add(new Person("Maria ", "Short\n", 2001));
        ml.add(new Person("Jose ", "White\n", 1980));
        ml.add(new Person("Josh ", "Brown\n", 1970));
        ml.add(new Person("Dahn ", "Lee\n", 1977));
        ml.add(new Person("Terry ", "Black\n", 1983));
        ml.add(new Person("Clark ", "Pale\n", 1999));
        ml.add(new Person("Sasha ", "Cole\n", 1989));
        ml.add(new Person("Paul ", "Chan\n", 1966));
        ml.add(new Person("Irlen ", "Bry\n", 1981));
        ml.add(new Person("Mary ", "Williams\n", 1975));
        ml.add(new Person("Puttin ", "Moreno\n", 1955));
        ml.add(new Person("James ", "Blanco\n", 1960));
        ml.add(new Person("Jack ", "Negro\n", 1963));
        ml.add(new Person("Mark ", "Torres\n", 1957));
        ml.add(new Person("Jane ", "Perez\n", 1995));
        ml.add(new Person("Sal ", "Salas\n", 1992));
        ml.add(new Person("Perry ", "Blah\n", 1973));
        ml.add(new Person("Charley ", "Nunca\n", 1996));
        ml.add(new Person("Anna ", "Cali\n", 2012));
        ml.add(new Person("Bruce ", "Cola\n", 2009));


    }


    // Variables declaration - do not modify                     
    private javax.swing.JRadioButton Anna;
    private javax.swing.JRadioButton Bruce;
    private javax.swing.JRadioButton Charley;
    private javax.swing.JRadioButton Clark;
    private javax.swing.JButton ClearText;
    private javax.swing.JRadioButton Dahn;
    private javax.swing.JTextArea DisplayText;
    private javax.swing.JButton Exit;
    private javax.swing.JRadioButton Irlen;
    private javax.swing.JRadioButton Jack;
    private javax.swing.JRadioButton James;
    private javax.swing.JRadioButton Jane;
    private javax.swing.JRadioButton Jose;
    private javax.swing.JRadioButton Josh;
    private javax.swing.JRadioButton Maria;
    private javax.swing.JRadioButton Mark;
    private javax.swing.JRadioButton Mary;
    private javax.swing.JRadioButton Paul;
    private javax.swing.JRadioButton Perry;
    private javax.swing.JRadioButton Puttin;
    private javax.swing.JRadioButton Sal;
    private javax.swing.JRadioButton Sasha;
    private javax.swing.JRadioButton Terry;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}

基本上,我想要的是用户能够单击Jradio按钮,并且预定义的数据显示在jtextarea中.这是我正在研究的另一种方法.

Essentially what i want is for the user to be able to click on the Jradio button and the predefined data to appear in the jtextarea. Here is an alternative method that i am working on.

    class Storage{
    Person Juan = new Person("Juan ", "Short\n", 2001);
    Person Jose = new Person("Jose ", "White\n", 1980);
    Person Josh = new Person("Josh ", "Brown\n", 1970);
    Person Dahn = new Person("Dahn ", "Lee\n", 1977);
    Person Terry = new Person("Terry ", "Black\n", 1983);
    Person Clark = new Person("Clark ", "Pale\n", 1999);
    Person Sasha = new Person("Sasha ", "Cole\n", 1989);        
    Person Paul = new Person("Paul ", "Chan\n", 1966);
    Person Irlen = new Person("Irlen ", "Bry\n", 1981);
    Person Mary = new Person("Mary ", "Williams\n", 1975);
    Person Puttin = new Person("Puttin ", "Moreno\n", 1955);
    Person James = new Person("James ", "Blanco\n", 1960);
    Person Jack = new Person("Jack ", "Negro\n", 1963);
    Person Mark = new Person("Mark ", "Torres\n", 1957);
    Person Jane = new Person("Jane ", "Perez\n", 1995);
    Person Sal = new Person("Sal ", "Salas\n", 1992);
    Person Perry = new Person("Perry ", "Blah\n", 1973);
    Person Charley = new Person("Charley ", "Nunca\n", 1996);
    Person Anna = new Person("Anna ", "Cali\n", 2012);
    Person Bruce = new Person("Bruce ", "Cola\n", 2009);

     List<Person> ml = new ArrayList<>();

     protected Storage (){
         ml.add(Juan);

         ml.add(Jose); // and so on..

但是我仍然无法弄清楚如何将我的Jradio按钮连接到预定义的信息,因此,当用户单击单选按钮时,该信息将显示在JtextArea中.

But again I can't figure out how to connect my Jradio button the predefined information, so that when the user click on the radio button the information displays in the JtextArea.

推荐答案

首先,变量名称不应以大写字母开头.我怀疑您的O'Rielly教科书使用大写字母.

First of all variable names should NOT start with an upper case character. I doubt your O'Rielly text book uses upper case characters.

您可以将Person对象存储在ArrayList中,而不是存储在ArrayList中:

Instead of storing your Person object in an ArrayList you can store then in a HashMap:

HashMap<String, Person> people = new HashMap<String, Person>();
people.put("Juan", new Person("Juan ", "Short\n", 2001));
people.put("Jose", new Person("Jose ", "White\n", 1980));

现在,您可以编写一个公共ActionListener,以供所有单选按钮共享:

Now you can write a common ActionListener to be shared by all radio buttons:

ActionListener al = new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        Person person = people.get(command);
    }
};

一旦有了Person对象,就可以在您的文本区域中显示Person中的数据.

Once you have the Person object you can display the data from the Person in you text area.

此外,您不应一次创建一个按钮.而是创建一个要使用的名称的Array(或ArrayList),然后遍历该Array.像这样:

Also, you shouldn't create the buttons one at a time. Instead create a Array (or ArrayList) of the names you want to use and then iterate through the Array. Something like:

String[] names = 
{
    "Juan",
    "Jose"
};

for (String name: names)
{
    JRadioButton button = new JRadioButton(name);
    button.addActionListener( al );
    panel.add( button );
}

这篇关于使用Jradio按钮显示集合中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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