当前位置: 首页 > 知识库问答 >
问题:

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

充培
2023-03-14

我在写一个简单的信息管理程序的代码。我在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();
    
}
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;
}

共有1个答案

柯立果
2023-03-14

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

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

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;
    }
}
 类似资料:
  • 我有一个名为Trade的实体,它可以映射到包含属性的表。这个实体也有一个字段,存储来自另一个表的值。我的贸易表包含第二个表的主键。我知道如何获得整个第二个表作为实体在我的贸易实体,但我只想要1列。 如你所见,我尝试了,但是连接是在贸易实体的主密钥上执行的。我如何使用注释从贸易表中选择外键,将其连接到第二/第三表,并直接只从col2/col3中获取值?任何建议都很感激 餐桌贸易: 表2: 表3: 现

  • 我正在构建一个简单的PHP代理,缓存响应头和对象。 我的问题是,如果我登录到youtube.com,我就看不到自己的签名,youtube一直说登录(未签名),但是如果我停止我的脚本,打开youtube.com网站,我就会看到我自己签名了。我认为这是饼干的问题。是吗? 我的脚本只是抓取响应头并将其发送回浏览器。当我使用fopen()下载对象时,一些网站,如Google“Play STore” 我在响

  • 问题内容: 这是我的配置: 回波用于测试。我隐藏了IP。 我正在尝试通过NGiNX从Redis获取会话数据。这就是为什么我安装HttpRedis的原因。 我不了解的是如何从Redis获取数据,然后将其放入变量中。所以我可以随便使用它。 问题答案: 使用NGiNX-Eval-Module:https : //github.com/vkholodkov/nginx-eval- module 这是您的操

  • 问题内容: 是否也有类似event.getSource的东西用于DocumentListener?我试图更改仅一个JTextField的颜色,在其中更改文本。这是我的DocumentListener: 如果没有与DocumentListener 类似的东西。怎么做? 问题答案: 你是正确的,没有像其他一些听众,但你可以使用文档类的,并达到你在找什么。 你可以做 和 稍后在DocumentListe

  • 我如何通过我自己的API从另一个API获取PDF,然后到前面供用户下载。 我现在得到的只是一张空白页。 后面是Scala的,当我打印文件时,我得到一个字符串。