[JAVA100 Example]020, toolbar

Source: Internet
Author: User
Tags final save file

Import Javax.swing.JToolBar;


import Javax.swing.JButton;


import Javax.swing.ImageIcon;





import Javax.swing.JFrame;


import Javax.swing.JTextArea;


import Javax.swing.JScrollPane;


import Javax.swing.JPanel;





import Java.net.URL;





import java.awt.*;


import java.awt.event.*;


/**


* <p>title: Tool bar Demo </p>


* <p>description: Provides a toolbar, including "Open", "save", "Search" tool buttons </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:ToolBarDemo.java</p>


* @version 1.0


*/


public class Toolbardemo extends JPanel


implements ActionListener {


protected JTextArea TextArea;


protected String newline = "\ n";


Static final private String open = "open";


static final private String save = "Save";


static final private String search = "search";


/**


*<br> Method Description: Constructor


*<br> input Parameters:


*<br> return type:


*/


public Toolbardemo () {


Super (New BorderLayout ());





//Create toolbar


JToolBar ToolBar = new JToolBar ();


addbuttons (ToolBar);





//Create a text field to output some information


TextArea = new JTextArea (15, 30);


textarea.seteditable (FALSE);


JScrollPane ScrollPane = new JScrollPane (TextArea);





//Placement member


setpreferredsize (New Dimension (450, 110));


Add (ToolBar, Borderlayout.page_start);


Add (ScrollPane, borderlayout.center);


  }


/**


*<br> Method Description: Building the toolbar


*<br> Input Parameters: JToolBar ToolBar tool bar


*<br> return type:


*/


protected void AddButtons (JToolBar toolBar) {


JButton button = null;





//First button, "open"


button = Makenavigationbutton ("Open16", OPEN,


"Open a file!" ",


"Open");


toolbar.add (button);





//second button, "Save"


button = Makenavigationbutton ("Save16", SAVE,


"Save the current file!" ",


"Save");


toolbar.add (button);





//Third button, "Search"


button = Makenavigationbutton ("Search16", SEARCH,


"Search the characters in the file!" ",


"Search");


toolbar.add (button);


  }


/**


*<br> Method Description: Constructs a button on a toolbar


*<br> input Parameters:


*<br> return type:


*/


protected JButton Makenavigationbutton (String imagename,


String Actioncommand,


String ToolTipText,


String alttext) {


//Search Pictures


String imglocation = "images/"


+ imagename


+ ". gif";


URL ImageURL = ToolBarDemo.class.getResource (imglocation);





//Initialization Tool button


JButton button = new JButton ();


//Set button command


Button.setactioncommand (Actioncommand);


//Set hint information


Button.settooltiptext (ToolTipText);


Button.addactionlistener (this);


   


if (ImageURL!= null) {//Find image


Button.seticon (New ImageIcon (ImageURL));


} else {//no image


Button.settext (AltText);


System.err.println ("Resource not Found:"


+ imglocation);


    }





return button;


  }


/**


*<br> Method Description: Event monitoring


*<br> input Parameters:


*<br> return type:


*/


public void actionperformed (ActionEvent e) {


String cmd = E.getactioncommand ();


String description = null;





if (open.equals (cmd)) {//Click on the first button


Description = "Open a file operation!" ";


} else if (save.equals (cmd)) {//Click on the second button


Description = "Save file operation";


} else if (search.equals (cmd)) {//Click on the third button


Description = "search character operation";


    }





Displayresult ("If here is the real program, you will enter:"


+ description);


  }





protected void Displayresult (String actiondescription) {


textarea.append (actiondescription + newline);


  }





public static void Main (string[] args) {


jframe.setdefaultlookandfeeldecorated (TRUE);





//define form


JFrame frame = new JFrame ("Toolbardemo");


frame.setdefaultcloseoperation (jframe.exit_on_close);





//Definition panel


Toolbardemo Newcontentpane = new Toolbardemo ();


Newcontentpane.setopaque (TRUE);


Frame.setcontentpane (Newcontentpane);





//Display form


Frame.pack ();


frame.setvisible (TRUE);


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.