我正在尝试访问JTextField文本,即' searchBox'来自外部类中创建的外部动作侦听器。
GUI:
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类获取searchBox文本值(上面的箭头)到url变量。有人可以帮我这个吗?我一直在寻找解决方案3小时......
答案 0 :(得分:5)
使用getSource
获取对JTextField
JTextField searchBox = (JTextField) e.getSource();
答案 1 :(得分:2)
将您的JTextField
变量声明为实例var。 (在initUI
方法之外)并创建getter方法。然后在listeners
课程中调用它:
myframe.url = myframe.getSearchBox().getText();;
其他旁注:
正确命名您的课程。类的名称以大写字母开头。
如果您不打算覆盖某些方法或定义新方法,请不要使用JFrame
扩展您的课程。
DON&#T; T使用摆动部件的绝对定位!使用适当的布局管理器。