如何从Jpanel中的JTextField中获取值并将其发送到其他Jpanel



我正在为简单的信息管理程序编写一段代码。而我在Java swing Gui方面遇到了麻烦。在这段代码中,我计划将Northpanel_center中的JTextField字符串用于Northpanel_ast。但我不能用它。

class Northpanel_Center extends JPanel {
public Northpanel_Center() {
String[] updown = {"이상", "이하"};
setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));
add(new JLabel("   이름   "));
JTextField name = new JTextField(15);
add(name);
add(new JLabel("           "));
add(new JLabel("팔로워수"));
JTextField followers = new JTextField(15);
add(followers);
JComboBox<String> ud1 = new JComboBox<String>(updown);
add(ud1);
add(new JLabel("광고비용"));
JTextField paid = new JTextField(15);
add(paid);
JComboBox<String> ud2 = new JComboBox<String>(updown);
add(ud2);
add(new JLabel("광고횟수"));
JTextField times = new JTextField(15);
add(times);
JComboBox<String> ud3 = new JComboBox<String>(updown);
add(ud3);
//make a new String with JTextField
String[] information = new String[4];
String[] bound = new String[3];
information[0] = name.getText();
information[1] = followers.getText();
information[2] = paid.getText();
information[3] = times.getText();
bound[0] = ud1.getSelectedItem().toString();
bound[1] = ud2.getSelectedItem().toString();
bound[2] = ud3.getSelectedItem().toString();

}

我想在下面的Jpanel中使用上面的String[],有Actionlistener。所以当我点击按钮时;搜索";方法,该方法使用上面的String作为参数。

class Northpanel_East extends JPanel {
public Northpanel_East() {
setLayout(new GridLayout(2, 1, 2, 2));
JButton searchBtn = new JButton("조회");
searchBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Search.searching() // cannot use above String[]

}
});

要添加,搜索方法如下。

public ArrayList<Influencer> searching(String[] information, String[] bound) {
searchedList = new ArrayList<>();
if (!(information[0].equals(""))) {
searchByName(information[0]);
}
else if (!information[1].equals("")) {
searchByFollower(Integer.parseInt(information[1]), bound[0]);
}
else if (!information[2].equals("")) {
searchByCost(Integer.parseInt(information[2]), bound[1]);
}
else if (!information[3].equals("")) {
searchByChanceOfAdvertise(Integer.parseInt(information[3]), bound[2]);
}
else {
searchedList = listOfInfluencer;
}
return searchedList;
}

您正在处理一个事件驱动的环境,发生了一些事情,然后您对其做出响应。这意味着在未来的某个时候,您需要访问这些信息。

实现这一点的一种方法是使用计算的属性,当调用这些属性时,计算它们的结果,而不是预先定义它们。

class Northpanel_Center extends JPanel {
private JTextField name;
private JTextField followers;
private JComboBox<String> ud1;
private JTextField paid;
private JComboBox<String> ud2;
private JTextField times;
private JComboBox<String> ud3;
public Northpanel_Center() {
String[] updown = {"이상", "이하"};
setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));
add(new JLabel("   이름   "));
name = new JTextField(15);
add(name);
add(new JLabel("           "));
add(new JLabel("팔로워수"));
followers = new JTextField(15);
add(followers);
ud1 = new JComboBox<String>(updown);
add(ud1);
add(new JLabel("광고비용"));
paid = new JTextField(15);
add(paid);
ud2 = new JComboBox<String>(updown);
add(ud2);
add(new JLabel("광고횟수"));
times = new JTextField(15);
add(times);
JComboBox<String> ud3 = new JComboBox<String>(updown);
add(ud3);
}
public String[] getInformation() {
String[] information = new String[4];
information[0] = name.getText();
information[1] = followers.getText();
information[2] = paid.getText();
information[3] = times.getText();
return information;
}
public String[] getBound() {
String[] bound = new String[3];
bound[0] = ud1.getSelectedItem().toString();
bound[1] = ud2.getSelectedItem().toString();
bound[2] = ud3.getSelectedItem().toString();
return bound;
}
}

最新更新