Hi everyone,
I started working in a GUI, and I did this. But, I don't know how to add functionality to the "Clear" button, so when it is clicked to clear all the content of the text are. Also the "Reset" button to clear all of the areas.
Thanks in advance
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class FindIt implements ActionListener { public FindIt() { JFrame FindIt = new JFrame("Word Search");//frame is created FindIt.setLayout(new BorderLayout());//layout is set to Border Layout FindIt.setSize(500, 300);//dimensions of frame are set JLabel label = new JLabel("Empty or Copy & Paste Text");//Label for the panel JButton clear = new JButton("Clear");//Clear button clear.setMnemonic('C');//set c as the mnemonic for clear JTextArea area = new JTextArea(30,40);//text area is set area.setLineWrap(true); area.setWrapStyleWord(true); JTextArea search = new JTextArea(2,10);//search area is set search.setVisible(true); search.setEditable(true);//make the text editable JButton search1 = new JButton("Search");//search button search1.setMnemonic('S');//set s as the mnemonic for search JLabel found2 = new JLabel("# Found"); JTextArea found = new JTextArea(2,5);//found search area found.setVisible(true); JButton reset = new JButton("Reset");//reset button reset.setMnemonic('R'); JButton exit = new JButton("Exit");//exit button exit.setMnemonic('x'); JPanel jpNorth = new JPanel();// panel for the top JPanel jpCenter = new JPanel();//center panel for text box JPanel jpSouth = new JPanel();//panel for the bottom jpNorth.add(label);// place label in top of the panel jpNorth.add(clear);// button in top of the panel jpCenter.add(area);// text box in center panel jpSouth.add(search);//place text box in bottom panel jpSouth.add(search1);//place search button jpSouth.add(found2);//label for found jpSouth.add(found);//place found text box in bottom panel jpSouth.add(reset);//place reset button in bottom panel jpSouth.add(exit);//place exit button in bottom panel FindIt.add(jpNorth, BorderLayout.NORTH);//places top panel in north position FindIt.add(jpCenter, BorderLayout.CENTER);//places center panel in center position FindIt.add(jpSouth, BorderLayout.SOUTH);//places bottom panel in bottom postion JMenuBar mbar = new JMenuBar();//create menu bar FindIt.setJMenuBar(mbar); JMenu file = new JMenu("File");//creates a file button on menu bar JMenuItem resetAll = new JMenuItem("ResetAll");//create reset all option under file resetAll.setMnemonic('A'); JMenuItem exit1 = new JMenuItem("Exit");//create exit option under file exit1.setMnemonic('x'); file.add(resetAll);//add reset all to file option on menu bar file.add(exit1);//add exit to file option on menu bar mbar.add(file);//adds file option to menu bar exit.addActionListener(this);//adds action listener to the exit button exit1.addActionListener(this);//adds action listener to the exit menu item clear.addActionListener(this); FindIt.setVisible(true);//allows the frame to be visible Searcher searchIT = new Searcher(search1, area, search, found);//sets listner for search1 JButton FindIt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//so program will close when x is clicked } public void actionPerformed(ActionEvent e)//sets an action to be performed for the exit options { if (e.getActionCommand().equals("Exit")) { System.exit(0); } else if (e.getActionCommand().equals("x")) { System.exit(0); } } public static void main(String[] args) { FindIt word = new FindIt(); } }