Display Message : Button « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » ButtonScreenshots 
Display Message
  

/*
 * This example is from the book "Java Foundation Classes in a Nutshell".
 * Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import java.awt.*;             // AWT classes
import javax.swing.*;          // Swing components and classes
import javax.swing.border.*;   // Borders for Swing components
import java.awt.event.*;       // Basic event handling 

public class DisplayMessage {
  public static void main(String[] args) {
    /*
     * Step 1: Create the components
     */
    JLabel msgLabel = new JLabel();      // Component to display the question
    JButton yesButton = new JButton();   // Button for an affirmative response
    JButton noButton = new JButton();    // Button for a negative response

    /*
     * Step 2: Set properties of the components
     */
    msgLabel.setText(args[0]);                           // The msg to display
    msgLabel.setBorder(new EmptyBorder(10,10,10,10));    // A 10-pixel margin 
    yesButton.setText((args.length >= 2)?args[1]:"Yes")// Text for Yes button
    noButton.setText((args.length >= 3)?args[2]:"No");   // Text for no button

    /*
     * Step 3: Create containers to hold the components
     */
    JFrame win = new JFrame("Message");  // The main application window
    JPanel buttonbox = new JPanel();     // A container for the two buttons
    
    /*
     * Step 4: Specify LayoutManagers to arrange components in the containers
     */
    win.getContentPane().setLayout(new BorderLayout())// layout on borders
    buttonbox.setLayout(new FlowLayout());              // layout left-to-right

    /*
     * Step 5: Add components to containers, with optional layout constraints
     */
    buttonbox.add(yesButton);            // add yes button to the panel
    buttonbox.add(noButton);             // add no button to the panel
    
    // add JLabel to window, telling the BorderLayout to put it in the middle
    win.getContentPane().add(msgLabel, "Center");        

    // add panel to window, telling the BorderLayout to put it at the bottom
    win.getContentPane().add(buttonbox, "South");  
    
    /*
     * Step 6: Arrange to handle events in the user interface.
     */
    yesButton.addActionListener(new ActionListener() {  // Note: inner class
      // This method is called when the Yes button is clicked.
      public void actionPerformed(ActionEvent e) { System.exit(0)}
    });

    noButton.addActionListener(new ActionListener() {   // Note: inner class
      // This method is called when the No button is clicked.
      public void actionPerformed(ActionEvent e) { System.exit(1)}
    });

    /*
     * Step 7: Display the GUI to the user
     */
    win.pack();   // Set the size of the window based its children's sizes.
    win.show();   // Make the window visible.
  }
}


           
         
    
  
Related examples in the same category
1. Creating a JButton Component from Action
2. Various Swing buttonsVarious Swing buttons
3. Responding to button pressesResponding to button presses
4. Putting buttons on an appletPutting buttons on an applet
5. Uses reflection to create groups of different types of AbstractButtonUses reflection to create groups of different types of AbstractButton
6. The Swing-ified button exampleThe Swing-ified button example
7. Create a JButton that does not show focusCreate a JButton that does not show focus
8. Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
9. The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
10. An example of radio button menu items in actionAn example of radio button menu items in action
11. JToggleButton DemoJToggleButton Demo
12. A Selected ButtonA Selected Button
13. Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
14. Button action to change the panel backgroundButton action to change the panel background
15. HTML buttonHTML button
16. Creating a Multiline Label for a JButton Component
17. Lines are left justified. This label text will center the lines
18. Label text italicizes the second line
19. Changing the Label of a JButton Component
20. Ploygon ButtonPloygon Button
21. Simple Button UISimple Button UI
22. Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
23. Displaying a Button with a BorderDisplaying a Button with a Border
24. Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
25. Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
26. Working with Tooltip TextWorking with Tooltip Text
27. Sharing Models between Buttons
28. Sharing an Action between JButton ComponentsSharing an Action between JButton Components
29. Button Html DemoButton Html Demo
30. Swing Button DemoSwing Button Demo
31. LoadSave: Button actionLoadSave: Button action
32. Button with ICON and Multiline Button with ICON and Multiline
33. Action Button SampleAction Button Sample
34. Mnemonic SampleMnemonic Sample
35. Swing Default ButtonSwing Default Button
36. Button Action SampleButton Action Sample
37. Button icons, a default button, HTML in a button,and button mnemonics.
38. Adding a Rollover and Pressed Icon to a JButton Component
39. Adding a Disabled Icon to a JButton Component
40. Setting the Gap Size Between the Label and Icon in a JButton Component
41. Moving the Icon in a JButton Component
42. Adding an Icon to a JButton Component
43. Moving the Label/Icon Pair in a JButton Component
44. If the action does not have an icon or a different icon must be used, add or change the icon using setIcon():
45. Dynamically update the appearance of a component
46. Simple ToggleButton SampleSimple ToggleButton Sample
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.