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

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

问题描述

你好每一个我的名字是亚伯拉罕我需要一些帮助我的程序。我一直在学习Java一段时间了,甚至买了几本书帮助我一起。我现在在我的旅程中,我想要了解收集的点。我能够以非常基本的方式使用数组列表,但我想了解它们如何工作,当使用与J单选按钮和其他摇摆组件。我一直在工作,看YouTube视频和阅读书籍,如Java编程 - 综合,10E和Java在一个简单的 - (第6 E)O'Reilly,但我还没有找到我的问题的答案。我可以通过单独使用以下代码获得预定义的信息显示在J文本区域。 Person调用第二个类,其中我有我的setters和getters。

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

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

  import java.util。*; 

public class FinalProject extends javax.swing.JFrame {


public FinalProject(){
initComponents();
}

@SuppressWarnings(unchecked)
//< editor-fold defaultstate =collapseddesc =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 javaxueswing.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.setHorizo​​ntalGroup(
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 )
.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 )
.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.setHorizo​​ntalGroup(
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在此处添加您的处理代码:
}

private void JoshActionPerformed(java.awt.event.ActionEvent evt ){
// TODO在此处添加您的处理代码:
}

private void DahnActionPerformed(java.awt.event.ActionEvent evt){
// TODO add你的处理代码在这里:
}

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>

/ *创建并显示表单* /
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
new FinalProject ().setVisible(true);
}
});
//所有用户所在的列表
List< Person> ml = new ArrayList<>();
ml.add(new Person(Maria,Short\\\
,2001));
ml.add(new Person(Jose,White\\\
,1980));
ml.add(new Person(Josh,Brown\\\
,1970));
ml.add(new Person(Dahn,Lee \\\
,1977));
ml.add(new Person(Terry,Black\\\
,1983));
ml.add(new Person(Clark,Pale \\\
,1999));
ml.add(new Person(Sasha,Cole \\\
,1989));
ml.add(new Person(Paul,Chan\\\
,1966));
ml.add(new Person(Irlen,Bry \\\
,1981));
ml.add(new Person(Mary,Williams\\\
,1975));
ml.add(new Person(Puttin,Moreno\\\
,1955));
ml.add(new Person(James,Blanco \\\
,1960));
ml.add(new Person(Jack,Negro \\\
,1963));
ml.add(new Person(Mark,Torres \\\
,1957));
ml.add(new Person(Jane,Perez\\\
,1995));
ml.add(new Person(Sal,Salas \\\
,1992));
ml.add(new Person(Perry,Blah \\\
,1973));
ml.add(new Person(Charley,Nunca \\\
,1996));
ml.add(new Person(Anna,Cali \\\
,2012));
ml.add(new Person(Bruce,Cola \\\
,2009));


}


//变量声明 - 不要修改
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退出;
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;
//变量声明结束
}

基本上我想要的是用户能够点击Jradio按钮和预定义的数据出现在jtextarea中。这里是一个替代方法,我正在努力。

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

列表< Person> ml = new ArrayList<>();

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

ml.add(Jose); //等等..

但是我再也不知道如何连接我的Jradio按钮预定义的信息,这样当用户点击单选按钮时,信息显示在JtextArea中。



非常感谢您的时间。

解决方案

首先,变量名不能以大写字符开头。



而不是在ArrayList中存储Person对象,你可以存储在一个HashMap中:

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

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

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

一旦拥有Person对象,就可以显示Person中的数据。 p>

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

  String [] names = 
{
Juan,
Jose
};

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


Hello every one my name is Abraham I need some assistance with my program. 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                   
}

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..

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.

Thank you so much for your time.

解决方案

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

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

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

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

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 buton显示集合中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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