Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-08-2009
Member
 
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,
Reply With Quote
  #2  
Old 10-08-2009
Member
 
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);
      }
    });
  }
Reply With Quote
  #3  
Old 10-08-2009
Member
 
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
Reply With Quote
  #4  
Old 10-08-2009
Member
 
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);
   }
}
Reply With Quote
  #5  
Old 10-08-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create button on frame in java?"
Thread Thread Starter Forum Replies Last Post
What is the use of Frame class in java? Truster Software Development 5 25-02-2010 11:16 AM
Creating an Internal Frame in java samualres Software Development 4 24-02-2010 10:46 PM
Java program code for frame Sarfaraj Khan Software Development 5 08-01-2010 11:02 AM
Frame constructor in java HardWeaR Software Development 3 08-12-2009 02:46 PM
Remove Title Bar of a Frame in Java rashmi_ay Software Development 4 30-11-2009 12:57 PM


All times are GMT +5.5. The time now is 08:52 AM.