Showing posts with label button. Show all posts
Showing posts with label button. Show all posts
Create button using JButton
Source code below will show you, how to create button in Java using JButton class.
**************************************************************************
COMPLETE SOURCE CODE FOR : CreateButtonUsingJButton.java
**************************************************************************
**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************
**************************************************************************
COMPLETE SOURCE CODE FOR : CreateButtonUsingJButton.java
**************************************************************************
import javax.swing.JButton;
import javax.swing.JFrame;
public class CreateButtonUsingJButton
{
public static void main(String[]args)
{
//Create button using JButton
//Button that we create contain text My first button
JButton myButton=new JButton("My first button");
JFrame frame=new JFrame("Create button using JButton");
//add button that we create in JFrame
frame.add(myButton);
//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set JFrame size
//WIDTH = 300 pixels
//HEIGHT = 100 pixels
frame.setSize(400,100);
//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}
**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************
Close JWindow using button
Source code below will show you how to exit a JWindow using a button.
************************************************************************
COMPLETE SOURCE CODE FOR : ClosingAJWindowUsingButton.java
************************************************************************
************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************
************************************************************************
COMPLETE SOURCE CODE FOR : ClosingAJWindowUsingButton.java
************************************************************************
import javax.swing.JWindow;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
public class ClosingAJWindowUsingButton extends JWindow implements ActionListener
{
JButton button=new JButton("Exit");
public ClosingAJWindowUsingButton()
{
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==button)
{
System.exit(0);
}
}
public static void main(String[]args)
{
ClosingAJWindowUsingButton caj=new ClosingAJWindowUsingButton();
}
}
************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************
Create window without title bar and window management button
In java, we can use JWindow to create window without title bar and window management button.
Close application when click on JFrame close button
JFrame hide when click close button
Set jframe size and it's location when click on maximize button
Subscribe to:
Posts (Atom)