【问题标题】:How to get value from JTextField in Jpanel and send it to other JPanel?如何从 Jpanel 中的 JTextField 获取值并将其发送到其他 JPanel?
【发布时间】:2022-01-10 09:12:26
【问题描述】:

我正在为简单的信息管理程序编写代码。我在 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;
}

【问题讨论】:

    标签: java swing user-interface


    【解决方案1】:

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

    一种方法是使用计算属性,当调用该属性时,计算其结果,而不是预先定义它们。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2012-03-08
      相关资源
      最近更新 更多