标签:actionlistener swing java user-interface
这个问题已经在这里有了答案: > Detect enter press in JTextField 9个
我有一个具有三个功能的程序;读取文件,写入文件并在文件中搜索特定文本.我目前正在创建一个可与其一起使用的GUI,以便不再依赖控制台.我已经针对上述三个功能创建了一个功能齐全的“主窗口”和功能按钮,以及一个退出按钮.现在,我正在使用用于搜索功能的GUI窗口-创建该窗口是对单击“搜索”按钮的响应.我的窗口和组件的布局方式是我想要的,但是当用户在输入要搜索的字符串后按Enter时,无法设置动作侦听器.我看过许多不同的资源,包括SOverflow,javadoc和actionlistener教程.但我走得很快.
这是在主窗口中绘制“搜索”按钮并链接到“搜索GUI”(我通过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();
}
}
}
}
}
}
解决方法:
您应该尝试重新构建代码一些
我不会在actionPerformed中创建GUI,不在外部创建GUI(或为其扩展JFrame做一个类),而在actionPerformed中,只显示setVisible(true).
即使这个建议不是您的问题,也可能会解决.
您当前的情况是:
您正在将actionListener添加到JTextField searchTerm中,您要添加的actionListener实际上是ButtonClickListener,因此您已经有一个actionPerformed方法,因此是相同的方法!!!
结果是:
当您在searchTerm中按Enter时,将调用ButtonClickListener.actionPerformed,如果尝试在文本字段中编写“ Search”,您将看到一些有趣的东西!
添加新的actionListener的简短代码是
searchTerm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//DO YOUR STUFF
}
});
标签:actionlistener,swing,java,user-interface 来源: https://codeday.me/bug/20191027/1946093.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。