【问题标题】:Accessing Jtextfield from outer action listener从外部动作监听器访问 Jtextfield
【发布时间】:2014-03-23 00:10:34
【问题描述】:

我正在尝试从外部类中创建的外部操作侦听器访问 JTextField 文本,即“searchBox”。

界面:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 

public class MyFrame extends JFrame
{
   public int stat1_var;
   public int stat2_var;
   public int stat3_var;
   public String stat4_var;
   private int statX_bound = 50;
   private int statY_bound = 280;
   public String url;

/*
* @param width  The width of the frame.
* @param height The height of the frame.
* @param title  The title of the frame.
*/


public MyFrame(int width, int height, String title, int stat1_var, int stat2_var, int stat3_var, String stat4_var)
{
  this.stat1_var = stat1_var;   //threading var
  this.stat2_var = stat2_var;   //spiders var
  this.stat3_var = stat3_var;   //results var
  this.stat4_var = stat4_var;   //other var

  initUI(width, height, stat1_var, stat2_var, stat3_var, stat4_var);

  this.setSize(width,height);   //set frame size
  this.setTitle(title);     //set frame title
  this.setVisible(true);    //set visible
  this.setResizable(false); //disable resizable frame

}


private void initUI(int width, int height, int stat1_var, int stat2_var, int stat3_var, String stat4_var) 
{
      ImageIcon ic = new ImageIcon("background.jpg");//background image source
      JDesktopPane dp = new JDesktopPane(); //create desktop pane
      JLabel lbl = new JLabel(ic);  //create label
      JPanel transparentPanel = new JPanel(); //create a JPanel

      lbl.setBounds(0, 0, width, height);  //bounds for the background

      transparentPanel.setOpaque(false);  
      transparentPanel.setBounds(0, 0, width, height);//bounds for the panel
      transparentPanel.setLayout(null);//default layout
      setLayeredPane(dp);

      JTextField searchBox = new JTextField("http://", 40); //keyword search box
      searchBox.setEditable(true);
      searchBox.setBounds(250, 130, 300, 25);
      searchBox.setToolTipText("Enter domain to be crawled");
      transparentPanel.add(searchBox);
      searchBox.setActionCommand("url");
      searchBox.addActionListener(new listeners(this));


       JButton crawlBtn = new JButton("Crawl"); // search button
       crawlBtn.addActionListener(new listeners(this));
       crawlBtn.setBounds(555, 130, 80, 25);
       crawlBtn.setActionCommand("crawl");
       crawlBtn.setToolTipText("crawl domain");
       transparentPanel.add(crawlBtn);//end

      JTextField searchBox2 = new JTextField("", 40); //crawl url search box
      searchBox2.setEditable(true);
      searchBox2.setBounds(250, 160, 300, 25);
      searchBox2.setToolTipText("enter your keywords");
      transparentPanel.add(searchBox2);     //end

      JButton jumpBtn = new JButton("Jump!"); // search button
      jumpBtn.addActionListener(new listeners(this));
      jumpBtn.setBounds(555, 160, 80, 25);
      jumpBtn.setActionCommand("crawl");
      jumpBtn.setToolTipText("crawl domain");
      transparentPanel.add(jumpBtn);//end

      JLabel stat1 = new JLabel("Threads: " + stat1_var);  //stat labels
      stat1.setToolTipText("Threads");
      stat1.setBounds(statX_bound, statY_bound, 300, 25);
      statY_bound += 25; //place the label one place below 
      transparentPanel.add(stat1);

      JLabel stat2 = new JLabel("Spiders: " + stat2_var);  //stat labels
      stat2.setToolTipText("Spiders");
      stat2.setBounds(statX_bound, statY_bound, 300, 25);
      statY_bound += 25; //place the label one place below 
      transparentPanel.add(stat2);

      JLabel stat3 = new JLabel("Results found: " + stat3_var);
      stat3.setToolTipText("Results found");
      stat3.setBounds(statX_bound, statY_bound, 300, 25);
      statY_bound += 25; //place the label one place below 
      transparentPanel.add(stat3);

      JLabel stat4 = new JLabel("Status: " + stat4_var);
      stat4.setToolTipText("Status");
      stat4.setBounds(statX_bound, statY_bound, 300, 25);
      statY_bound += 25; //place the label one place below 
      transparentPanel.add(stat4);          

      dp.add(lbl,new Integer(50));  
      dp.add(transparentPanel,new Integer(350));

      setDefaultCloseOperation(EXIT_ON_CLOSE); //close the app if close button clicked

} //end constructor

public void paint(Graphics g) 
{ 
  super.paint(g); //call superclass' paint method 
  g.setColor(Color.BLACK); 

} //end method paint 

} //end class MyFrame

这是外部类监听器:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.File;

public class listeners implements ActionListener
{
    private MyFrame myframe;
    public listeners(MyFrame myframe){
        this.myframe = myframe;
        //String source = e.getSource();
    }

    public void actionPerformed(ActionEvent event) {
        String action = event.getActionCommand();

        if (action == "url")
        {
         myframe.url = searchBox.getText(); //<----- I am trying to obtain this from the gui
        }

    }
}

我正在尝试从 GUI 类获取 url 变量的 searchBox 文本值(上面的箭头)。谁能帮我解决这个问题?我一直在寻找解决方案 3 小时了...

【问题讨论】:

    标签: java swing class actionlistener jtextfield


    【解决方案1】:

    使用getSource 获取对JTextField 的引用

    JTextField searchBox = (JTextField) e.getSource();
    

    【讨论】:

    • 哈哈!我完全错过了他实际上将 ActionListener 添加到 JTextField。 +1 非常优雅的解决方案。
    • 像魅力一样工作。谢谢你的朋友,我真的很感激。
    【解决方案2】:

    将您的JTextField 变量声明为实例变量。 (在initUI 方法之外)并创建getter 方法。然后在你的 listeners 类中调用它:

    myframe.url = myframe.getSearchBox().getText();;

    其他注释:

    1. 正确命名你的类。类名以大写开头。

    2. 如果您不打算重写某些方法或定义新方法,请不要使用 JFrame 扩展您的类。

    3. 不要对摆动组件使用绝对定位!使用适当的布局管理器。

    【讨论】:

    • 感谢朋友的提示。
    猜你喜欢
    • 2015-05-12
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多