A simple component that can be added to a container is a JButton object, and the JButton object is a button that can be clicked
Create JButton Component : JButton button = new JButton ("button");
Call the Add () method to add it to the container : Add (Button);
When adding components to a container, you do not need to indicate where the component is located in the container, the layout of the component is determined by the layout manager , and the simplest layout manager is the FlowLayout class
Create FlowLayout Object : FlowLayout flo = new FlowLayout ();
Call the SetLayout () method to associate the container with the manager : setlayout (FLO);
Demo:
1 PackageCom.swingdemo.demo;2 3 Importjava.awt.FlowLayout;4 5 ImportJavax.swing.JButton;6 ImportJavax.swing.JFrame;7 ImportJavax.swing.UIManager;8 Importjavax.swing.UnsupportedLookAndFeelException;9 Ten Public classPlaybackextendsJFrame { One A Private Static Final LongSerialversionuid = 1L; - - PublicPlayback () { the - Super("Playback"); - Setlookandfeel (); -SetSize (225, 80); + setdefaultcloseoperation (jframe.exit_on_close); -FlowLayout Flo =NewFlowLayout (); + setlayout (flo); AJButton play =NewJButton ("Play"); atJButton stop =NewJButton ("Stop"); -JButton pause =NewJButton ("Pause"); - Add (play); - Add (stop); - Add (pause); -SetVisible (true); in - } to + Private voidSetlookandfeel () { - the Try { *Uimanager.setlookandfeel ("Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); $}Catch(Exception e) {Panax Notoginseng e.printstacktrace (); - } the } + A Public Static voidMain (string[] args) { the +Playback PB =NewPlayback (); - $ } $ -}View Code
Buttons for the Java user interface