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,
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,
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); } }); }
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
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:
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.Code:public void init () { clickOnMe = new Button("Here's a button!"); add(clickOnMe); }
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); } }
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.
I hope this helps those looking for some fancy buttons.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() ); } } }
Bookmarks