gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:00:50 26 4
gpt4 key购买 nike

我的代码用于使用 jpanel 预订巴士,我需要将第二个类(class)的数组添加到第一个类(class)。该数组需要预先填充,但我可以这样做。我不知道如何从二等舱访问它。我的代码是使用数组并将它们变成一个组合框。我已经与其他人一起完成了此操作,但我需要在另一个中创建一个数组并将该数组传递回我的第一个类并将其转换为组合框。我只需要访问它的帮助。

我刚刚添加了额外的代码,但不确定。如果我弄错了,你能告诉我它应该去哪里吗,否则需要一段时间。第一类还好,第二类丢失了错误,不知道这次是什么原因造成的。

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

最佳答案

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

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

关于Java 数组从另一个类访问它并用作组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36825129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com