One of my friend asked me, how to create a basic java GUI application which can run on desktop?
A small java gui desktop application, which is having a button and when user click on it, it will show the status of the application.
Source code:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class PSRSampleGUI
public static void main(String[] args) {
// Creating a frame
JFrame frame_name = new JFrame("PSR");
//Setting the layout to view the content
frame_name.setLayout(new FlowLayout());
// Sample lable option
JLabel label_name = new JLabel("Click Start button");
// Creating a button
JButton button_name = new JButton("Start");
// Adding button to frame
frame_name.add(label_name);
frame_name.add(button_name);
// Adding button functionality
button_name.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null, "Started", "App Status", JOptionPane.INFORMATION_MESSAGE);
// Call the method here ( the implemented method)
}
});
// Displaying the frame in the center of the screen window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = frame_name.getSize();
int windowX = Math.max(0, (screenSize.width - windowSize.width ) / 2);
int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);
frame_name.setLocation(windowX,windowY);
// Displaying the Frame with packing all the contents properly
frame_name.setVisible(true);
frame_name.pack();
} // main method ends here
} // class ends here
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class PSRSampleGUI
public static void main(String[] args) {
// Creating a frame
JFrame frame_name = new JFrame("PSR");
//Setting the layout to view the content
frame_name.setLayout(new FlowLayout());
// Sample lable option
JLabel label_name = new JLabel("Click Start button");
// Creating a button
JButton button_name = new JButton("Start");
// Adding button to frame
frame_name.add(label_name);
frame_name.add(button_name);
// Adding button functionality
button_name.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null, "Started", "App Status", JOptionPane.INFORMATION_MESSAGE);
// Call the method here ( the implemented method)
}
});
// Displaying the frame in the center of the screen window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = frame_name.getSize();
int windowX = Math.max(0, (screenSize.width - windowSize.width ) / 2);
int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);
frame_name.setLocation(windowX,windowY);
// Displaying the Frame with packing all the contents properly
frame_name.setVisible(true);
frame_name.pack();
} // main method ends here
} // class ends here
Screenshots:
- By Running the application, you will get the following window in the center of your screen.
- After clicking on "Start" button, you will get the following window
If you have any queries, please mail to psrdotcom@gmail.com
No comments:
Post a Comment