0% found this document useful (0 votes)
32 views4 pages

Java Quick Tutorial

This document provides code examples for common tasks when creating graphical user interfaces (GUIs) in Java using Swing: 1) It shows how to create a basic JFrame window, set its size and title, and make it visible. 2) It demonstrates how to add a JButton to a JPanel and then add that panel to the JFrame. 3) It includes an example of adding an action listener to a JButton to respond to button clicks. 4) Additional code snippets illustrate how to set an icon image for the frame, restrict input to integers in a JTextField, and set a background image on a JLabel.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Java Quick Tutorial

This document provides code examples for common tasks when creating graphical user interfaces (GUIs) in Java using Swing: 1) It shows how to create a basic JFrame window, set its size and title, and make it visible. 2) It demonstrates how to add a JButton to a JPanel and then add that panel to the JFrame. 3) It includes an example of adding an action listener to a JButton to respond to button clicks. 4) Additional code snippets illustrate how to set an icon image for the frame, restrict input to integers in a JTextField, and set a background image on a JLabel.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Opening a new form from jbutton --------------------- new

formname().setVisible(true);
-------------------------------------------------------------------------------------------------------------------------

Create window
import javax.swing.*;
public class Create_window()
{
public static void main(String args[])
{
JFrame frame = new JFrame(The title );
frame.setSize(640,480);
frame.setVisible(true);
frame.setDefaultcloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Adding Jbuttons on the form


import javax.swing.*;
public class classname{
public static void main(String[] args){
JFrame frame = new JFrame("title");
frame.setVisible(true);
JPanel panel = new JPanel();
//creates a new panel
panel.setLayout(null);
JButton button1 = new JButton("OK");
button1.setBounds(40, 40, 75, 25);

panel.add(button1);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
}
}
--------------------------------------------------------------------------------------------------------------

Set the frame icon to an image loaded from a


file
frame.setIconImage(new ImageIcon(imgURL).getImage());

Action Listner
JButton button1 = new JButton(submit);
button1.addActionListener(new ActionListener())
{
public void actionPerformed(ActionEvent e)
{
//do action
}

JTextfield (Only accepts Integer values)


public void jTextfield1 KeyTyped(KeyEvent ke)
{
char c = ke.getKeyChar();
if(!Character.isDigit(c))
ke.consume();

// prevent event propogation

SetBackground image for JLabel


public class Yourframe extends JFrame {
private ImageIcon icon;
private JLabel label;

//Global declaration of ImageIcon

public Yourframe()
{
JLabel label2 = new JLabel("Name:");
getContentPane().add(label2);
label2.setBounds(20,50,75,25);
icon = new ImageIcon("e:/ashick1.jpg");

//creates label
//adds to the frame
//sets position of label
//store the image in icon

label= new JLabel()


//function for label
{
public void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, 640, 480, null);
super.paintComponent(g);
}
};
label.setOpaque(false);
getContentPane().add( label );
label.setText("");

//label function ends

} //closing parenthesis of function Yourframe


public static void main(String[] args) {
Yourfframe frame = new Yourfframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(100, 150);
frame.setSize(640,480);
frame.setVisible(true);

// closing parenthesis of main class

// closing Paranthesis of class Yourframe

You might also like