简体   繁体   English

Java数组从另一个类访问它并用作组合框

[英]Java array accessing it from another class and using as a combobox

My code is used to book a bus with jpanel, I need to add array from a second class to my first.The array needs to be pre-filled which I can do however. 我的代码用于预订使用jpanel的巴士,我需要将第二个类的数组添加到我的第一个类中。但是需要预先填充数组,但是我可以这样做。 I don't know to access it from the second class. 我不知道从第二堂课来访问它。 My code is to use arrays and turn them into a combo-box. 我的代码是使用数组并将它们变成组合框。 I've done this with the others but I need to make an array in another and pass the array back to my first class and turn it into the combobox. 我已经与其他人一起完成了此操作,但我需要在另一个数组中创建一个数组,并将该数组传递回我的第一堂课,然后将其转换为组合框。 I just need help with access it. 我只需要访问它的帮助。

I just added the extra code but unsure. 我只是添加了额外的代码,但不确定。 Can you show where it should go if I got it wrong otherwise it's going to take awhile. 如果我弄错了,可以告诉我它应该去哪里,否则需要一段时间。 First class is OK 2nd class has lost of errors, not sure what's causing it this time. 头等舱是可以的。二等舱已经丢失了错误,不确定这次是什么原因造成的。

代码2红线

first class: 头等舱:

package learning;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;


    @SuppressWarnings("unused")
    public class test {

        String[] items = {"Tipperary","Cork","Limerick","Dublin"};
        JComboBox c = new JComboBox(items);
        JButton b = new JButton("From");
        JLabel l = new JLabel("Choose orgin");

        JComboBox h = new JComboBox(items); // To  destination
        JButton f = new JButton("To");
        JLabel g = new JLabel("Choose destination");

        String[] items2 = {"window","aisle"};
        JComboBox m = new JComboBox(items2); 
        JButton  n = new JButton("Seat");
        JLabel  o = new JLabel("choose seat");

        String[] items3 = {"Single","return"};
        JComboBox x = new JComboBox(items3); 
        JButton  y= new JButton("Ticket");
        JLabel  z = new JLabel("choose Ticket");


        JComboBox xx = new JComboBox(); 
        JButton  yy = new JButton("Ticket");
        JLabel  zz = new JLabel("choose Ticket");


        public test(){

        frame();

        }
         public void frame(){

        JFrame wolf = new JFrame();//frame
        wolf.setVisible(true);
        wolf.setSize(350,350);
        wolf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

        JPanel p = new JPanel();

        p.add(c);//
        p.add(b);//
        p.add(l);//lable1
        p.add(h);//
        p.add(f);//
        p.add(g);//lable 2
        p.add(m);//
        p.add(n);//
        p.add(o);//lable 2
        p.add(x);//
        p.add(y);//
        p.add(z);//lable 2
        p.add(xx);//
        p.add(yy);//
        p.add(zz);//lable 2


        wolf.add(p);

        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
        String s = c.getSelectedItem().toString();
            l.setText(s);
            }
        });


         f.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
            String s = h.getSelectedItem().toString();
                g.setText(s);
                }
            });

         n.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
            String s = m.getSelectedItem().toString();
                o.setText(s);
                }
            });

         y.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
            String s = x.getSelectedItem().toString();
                z.setText(s);
                }
            });
             }
         {
         }
    public static void main(String[]args){

        nextstep myNextstep = new nextstep();
        List List = (myNextstep).getList();


        new test();

        }

    }

code 2: 代码2:

     package learning;
    import java.util.List;
    import java.util.ArrayList;

    public class nextstep {
    private int number1 = 1;
    private int number2 = 2;
    private int number3 = 3;
    private int number4 = 4;
    private int number5 = 5;
    private int number6 = 6;
    private int number7 = 7;
    private int number8 = 8;
    private int number9 = 9;
    private int number10 = 10;
    private int number11 = 11;
    private int number12 = 12;
    private int number13 = 13;
    private int number14 = 14;
    private int number15 = 15;
    private int number16 = 16;

    public List <Integer> getList() { return list; }

    public nextstep() {
       list = new ArrayList<Integer>();
       list.add(number1);
       list.add(number2);
       list.add(number3);
       list.add(number4);
       list.add(number5);
       list.add(number6);
       list.add(number7);
       list.add(number8);
       list.add(number9);
       list.add(number10);
       list.add(number11);
       list.add(number12);
       list.add(number13);
       list.add(number14);
       list.add(number15);
       list.add(number16);
    }
    public List <Integer> getList() 
    { 
        return list; }
    }

It seems to be just a matter of creating an object of the second class and request the list with the provided method: 似乎只需要创建第二个类的对象并使用提供的方法来请求列表即可:

nextstep myNextstep = new nextstep();
List<Integer> myList = myNextstep.getList();

JComboBox c = new JComboBox(items); //this is wrong you need to set a new combobox model with your arrays do it like this change the other comboboxes accordingly //这是错误的,您需要使用数组设置新的组合框模型,就像这样做,相应地更改其他组合框

c.setModel(new DefaultComboBoxModel(item));

// if you want to add what you got from the second class getlist is returning an arraylist if you want to add them to your combobox you need to convert that to a array by toArray(); //如果要添加从第二个类中获得的内容,则getlist返回一个数组列表,如果要将其添加到组合框,则需要通过toArray();将其转换为数组toArray();

 c.setModel(new nextstep().getList().toArray());

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

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