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

时间:2016-04-24 15:26:54

标签: java arrays combobox

我的代码用于预订带有jpanel的总线,我需要从第二个类添加数组到我的第一个。数组需要预先填充,但我可以做。我不知道从二等课程中访问它。我的代码是使用数组并将它们变成一个组合框。我和其他人一起完成了这个,但是我需要在另一个数组中创建一个数组并将数组传回我的第一个类并将其转换为组合框。我只需要帮助就可以访问它。

我刚刚添加了额外的代码,但不确定。如果我弄错了,你能说明它应该去哪里吗?否则它会需要一段时间。头等舱还可以。第二课已经失去了错误,不知道这次造成了什么。

code 2 red lines

头等舱:

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

        }

    }

代码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; }
    }

2 个答案:

答案 0 :(得分:1)

似乎只是创建第二个类的对象并使用提供的方法请求列表:

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

答案 1 :(得分:0)

JComboBox c = new JComboBox(items); //这是错的 你需要设置一个新的组合框模型,你的阵列就是这样做的 相应地更改其他组合框

c.setModel(new DefaultComboBoxModel(item));

//如果你想添加你从第二堂课得到的东西    getlist返回一个arraylist如果你想将它们添加到你的组合框中,你需要通过toArray();将其转换为数组

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