使用单选按钮显示集合中的项目

时间:2015-03-06 05:22:05

标签: java swing collections jtextarea jradiobutton

我一直在学习java一段时间,甚至买了几本书来帮助我。我现在正处于旅程中我想要了解收藏品的那一点。我能够以非常基本的方式使用数组列表,但我想了解它们与J单选按钮和其他摆动组件一起使用时的工作原理。我一直在工作,观看YouTube视频和阅读书籍,如#34; Java编程简介 - 综合,10E" " Java in a Nutshell - (6th E)O' Reilly"但我还没有找到问题的答案。我可以通过单独使用以下代码获取要在J文本区域中显示的预定义信息。 "人"我打电话给第二堂课,我有我的主持人和吸气剂。

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

但是你们可以看到这不是使用列表,而是我在每个用户单独执行各自的动作执行事件。这里是包含集合/列表的代码。

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中。这是我正在研究的另一种方法。

    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中。

1 个答案:

答案 0 :(得分:1)

首先,变量名不应以大写字母开头。我怀疑你的O&#39; Rielly教科书是否使用大写字母。

不是将Person对象存储在ArrayList中,而是可以存储在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,供所有单选按钮共享:

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

获得Person对象后,您可以显示文本区域中Person的数据。

此外,您不应该一次创建一个按钮。而是创建要使用的名称的Array(或ArrayList),然后遍历Array。类似的东西:

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

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