[英]How to get value from JTextField in Jpanel and send it to other JPanel?
我正在为简单的信息管理程序编写代码。 我在 Java swing Gui 遇到麻烦。 在这段代码中,我计划将Northpanel_center 中的JTextField 字符串用于Northpanel_east。 但我不能使用它。
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。 因此,当我单击按钮时,调用使用上述字符串作为参数的“搜索”方法。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.