我有一个程序,它有三个功能:读取一个文件,写入一个文件,并在一个文件中搜索特定的文本。我目前正在创建一个GUI,以便不再依赖控制台。我已经为上述三个函数创建了一个功能齐全的“主窗口”和功能按钮,以及一个退出按钮。现在,我正在处理我的搜索功能的GUI窗口-该窗口是创建为响应被点击的搜索按钮。,我有窗口和组件按我想要的方式布局,但是当用户输入他们想要搜索的字符串后,当用户按Enter时,设置动作监听器会有困难。我看过许多不同的源,包括SOverflow、javadoc和actionlistener教程;但是我没有进展很快。
下面是在主窗口中绘制搜索按钮的基本代码,并链接到Search (我通过main链接到它):
public class SimpleDBGUI{
static File targetFile; //Declare File var to be used in methods below for holding user's desired file
static JTextField sdbTarget;
static JTextField searchTerm;
public void mainWindow(){
//Create main window for Program
JFrame mainWindow = new JFrame("Simple Data Base"); //Init frame
mainWindow.setSize(500, 180); //Set frame size
mainWindow.setVisible(true); //Make frame visible
//Create panel for the main window of the GUI
JPanel simpleGUI = new JPanel( new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
mainWindow.getContentPane().add(simpleGUI); //Adds JPanel container to the ContentPane of the JFrame
//Create button linking to the search function
JButton searchButton = new JButton("Search"); //Init button with text
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 2;
gbCons.weightx = .1;
searchButton.setActionCommand("Search");
searchButton.addActionListener( new ButtonClickListener());
simpleGUI.add(searchButton, gbCons); //Adds the "Search" button to the JPanel
//Create TextField for user to input a desired file
sdbTarget = new JTextField();
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
gbCons.gridwidth = 3;
simpleGUI.add(sdbTarget, gbCons); //Adds TextField to GUI
}
public class ButtonClickListener implements ActionListener{ //Sets the EventListener for every function
public void actionPerformed(ActionEvent event){
targetFile = new File(sdbTarget.getText());
String function = event.getActionCommand(); //Reads the ActionCommand into a string for use in performing desired function
if( function.equals("Search")){ //Search Function, draws search window and components
JFrame searchWindow = new JFrame("SimpleDB Search"); //Draw window
searchWindow.setSize(500, 200);
searchWindow.setVisible(true);
JPanel searchGUI = new JPanel( new GridBagLayout()); //Create container and add to window
GridBagConstraints gb1Cons = new GridBagConstraints();
searchWindow.getContentPane().add(searchGUI);
JLabel searchPrompt = new JLabel("Please input the word/phrase you wish to find:"); //Prompt user to specify string to search for
gb1Cons.fill = GridBagConstraints.BOTH;
gb1Cons.gridy = 0;
gb1Cons.gridx = 0;
//gb1Cons.weighty = .1;
searchGUI.add(searchPrompt, gb1Cons); //Add prompt to container
JTextField searchTerm = new JTextField(); //Create JTextField for user input and add to container
gb1Cons.fill = GridBagConstraints.BOTH;
gb1Cons.gridy = 1;
gb1Cons.gridx = 0;
//gb1Cons.weighty = .1;
searchGUI.add(searchTerm, gb1Cons);
searchTerm.addActionListener(this); //Assign ActionListener to JTextField
JTextArea searchResult = new JTextArea(); //Create search output box and add to container
gb1Cons.fill = GridBagConstraints.BOTH;
gb1Cons.gridy = 2;
gb1Cons.gridx = 0;
//gb1Cons.weighty = .1;
searchGUI.add(searchResult, gb1Cons);
public void actionPerformed( ActionEvent event){ //Tried this as one event handler, supposed to execute the following upon the user pressing Enter, failed of course
boolean stringFound = false; //Set flag false
try{
Scanner searchFile = new Scanner(targetFile); //Read file to be searched into a scanner
String searchInput = searchTerm.getText(); //Read term to search for into a string
while( searchFile.hasNextLine()){ //Check that specified file has a next line and:
String searchLine = searchFile.nextLine(); //Read line into string
if( searchLine.contains(searchInput)){ //Check that Line contains searched term and:
stringFound = true; //If line contains term, set flag to true
searchResult.append("**" + searchLine + "**"); //Append line with term to output box
}
}searchFile.close(); //Close scanner
if(!stringFound){
searchResult.append("The term(s) you searched for does not exist in this file"); //Output if line does not contain term
}
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}发布于 2015-11-24 15:56:52
您应该尝试重新构造代码
我不会在actionPerformed中创建GUI,也不会在外部创建GUI(或者为其扩展JFrame创建类),而在actionPerformed中,只需显示setVisible(true)即可。
即使这个建议不是你的问题,它也可能解决它。
当前的情况是:
您要将actionListener添加到JTextField searchTerm中,而您要添加的actionListener实际上是ButtonClickListener,因此您已经有了一个actionPerformed方法,因此使用相同的方法!
所以结果是::
当您在searchTerm中按enter键时,ButtonClickListener.actionPerformed将被调用,如果您试图在textfield中编写"Search“,您将看到--一些有趣的!!
添加新actionListener的简短代码将是
searchTerm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//DO YOUR STUFF
}
});发布于 2015-11-24 15:59:58
只需将此代码粘贴到构造函数中即可。
希望能帮上忙。请记住,您的searchTerm应该是jtextfield,因为jtextxarea的代码有点不同。
searchTerm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformed(e);
}
});https://stackoverflow.com/questions/33898003
复制相似问题