Results 1 to 5 of 5

Thread: How to create button on frame in java?

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    How to create button on frame in java?

    Hi,

    I want to know how to create a custom button in java? On a Frame?
    How to create button on frame in java?
    Can Someone help me?

    Thanks,

  2. #2
    Join Date
    May 2008
    Posts
    33

    Re: How to create button on frame in java?

    How to create Button on frame

    Here, you will learn simply the procedure of creation a command button on the Java Awt Frame.

    The following program has used simply the Button class for creating a command button on the frame object.

    FlowLayout(): This is the constructor of FlowLayout class. This class is used for arranging the component from left to right. Here this is used for creating the object of the class to set the layout by passing the created object from the setLayout() method of the Frame class.

    Code:
    import java.awt.*; 
    import java.awt.event.*;
     
    public class ButtonText { 
      public static void main(String[] args) {
        Frame frame=new Frame("Button Frame");
        Button button = new Button("Submit"); 
        frame.add(button); 
        frame.setLayout(new FlowLayout());
        frame.setSize(200,100);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent e){
            System.exit(0);
          }
        });
      }

  3. #3
    Join Date
    Aug 2009
    Posts
    10

    Re: How to create button on frame in java?

    Hey can you tell me how can I have costume buttons on Java applet?

    May be an example would help me to get an idea.

    Thanks in advance.
    LetsC

  4. #4
    Join Date
    May 2008
    Posts
    30

    Re: How to create button on frame in java?

    Buttons in Java Applets

    Creating a button object

    1. Firstly, declare the object exactly as you would declare a normal number, except this time, the variable type is Button:

    Button clickOnMe;

    This variable declaration should go with all the other declarations just inside the curly brackets used to set up the main program. The order in which you put your variable declarations isn't important - the button declaration can go at the beginning of the declarations, at the end or somewhere in the middle.

    2. Create a special method called init() as follows, and put in it the following instruction:

    Code:
    public void init ()
    { clickOnMe = new Button("Here's a button!");
      add(clickOnMe);
    }
    init() is a special method that is called automatically by Java when you first run the applet. It is used to set up variables (to initialise them - hence the name) and to call up any methods that you want called immediately.

    There are two commands inside init()
    The first one reserves space in the memory for the button object. So far we have declared it (i.e. told Java that we intend to use a variable called clickOnMe, but we haven't actually reserved the memory for it. That is what this new Button instruction does. It is always followed by a pair of brackets and inside those is the caption that you want to appear on the button. In this case, the caption is "Here's a button!"

    The add(clickOnMe) instruction actually makes the button appear on the screen. If you didn't include it, then the button would be created but would remain invisible. Here is the complete program:

    Code:
    import java.applet.*;
    import java.awt.*;
    
    public class buttonDemo1 extends Applet
    {  Button clickOnMe;
    
       public void init ()
       { clickOnMe = new Button("Here's a button!");
         add(clickOnMe);
       }
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    53

    Re: How to create button on frame in java?

    How to create fancy buttons in your application

    This Java Swing tip illustrates a method of creating fancy buttons in your applications. The code uses JButton as the base and then add a roll over image on the button. This code may be generally used by game developers for creating animation in their widgets.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ButtonTest extends JFrame {
       private JButton plainButton, fancyButton;
    
       public ButtonTest()
       {
          super( "Testing Buttons" );
    
          Container c = getContentPane();
          c.setLayout( new FlowLayout() );
    
          // create buttons
          plainButton = new JButton( "Plain Button" );
          c.add( plainButton );
    
          Icon bug1 = new ImageIcon( "bug1.gif" );
          Icon bug2 = new ImageIcon( "bug2.gif" );
          fancyButton = new JButton( "Fancy Button", bug1 );
          fancyButton.setRolloverIcon( bug2 );
          c.add( fancyButton );
    
          // create an instance of inner class ButtonHandler
          // to use for button event handling 
          ButtonHandler handler = new ButtonHandler();
          fancyButton.addActionListener( handler );
          plainButton.addActionListener( handler );
    
          setSize( 275, 100 );
          show();
       }
    
       public static void main( String args[] )
       { 
          ButtonTest app = new ButtonTest();
    
          app.addWindowListener(
             new WindowAdapter() {
                public void windowClosing( WindowEvent e )
                {
                   System.exit( 0 );
                }
             }
          );
       }
    
       // inner class for button event handling
       private class ButtonHandler implements ActionListener {
          public void actionPerformed( ActionEvent e )
          {
             JOptionPane.showMessageDialog( null,
                "You pressed: " + e.getActionCommand() );
          }
       }
    }
    I hope this helps those looking for some fancy buttons.

Similar Threads

  1. What is the use of Frame class in java?
    By Truster in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 11:16 AM
  2. Creating an Internal Frame in java
    By samualres in forum Software Development
    Replies: 4
    Last Post: 24-02-2010, 10:46 PM
  3. Java program code for frame
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 08-01-2010, 11:02 AM
  4. Frame constructor in java
    By HardWeaR in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 02:46 PM
  5. Remove Title Bar of a Frame in Java
    By rashmi_ay in forum Software Development
    Replies: 4
    Last Post: 30-11-2009, 12:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,517,096.46360 seconds with 16 queries