You are on page 1of 8

Perform on Button :

1. Transfer text one frame to another frame text area or text box or
lable

TextReceive receive = new TextReceive();

TextReceive.jTextArea1.setText(jTextField1.getText()+ ""+
jTextField2.getText());
receive.setVisible(true);

2. Close and Minimise option code

Event ->> Mouse->> Mouseclick


For Minimise
this.setState(JFrame.ICONIFIED);

For closure
System.exit(0)

3. How to show password


if (jCheckboxShowPass.isSelected()){
jPasswordField1.setEcoChar((char)0);
}else {
jPasswordField1.setEcoChar("*");
}

4. Create JFrame and close it


import javax.swing.*; // to import package

JFrame frame = new JFrame();


frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

5. Set Size, location and bounds in JFrame

frame.setSize(700,500);
frame.setLocation(100,50);

//both properties in one commamd (First two location,


next two size)
frame.setBounds(100,100,1000,500);

6. Set title of JFrame

frame.setTitle("My JFrame");

7. Set title image of JFrame

ImageIcon icon = new ImageIcon("image" or Path);


frame.setIconImage(icon.setImage());

7. Set Background color of JFrame

Container c = frame.getContentPane();
c.setBackground(Color.RED);

8. How to restrict the user to resize the JFrame

frame.setResizable(false);

9. How to create a JLabel

c.setLayout(null) // first set the container layout as


null

JLabel label = new JLabel("User Name");


label.setBounds(100,50,100,30);

c.add(label); // add the label in container

10. How to set Text of a JLabel

JLabel label = new JLabel("User Name");


label.setText("Password";

11. How to set Font style and Font size of a JLabel Text

JLabel label = new JLabel("User Name");

Font f = new Font("Ariel",Font.PLAIN, 30); // font can be


set bold by replacing Font.BOLD

label.setFont(f) // set font to


label

c.add(label); // add the label


in container

12. How to set Image in JLabel

ImageIcon icon = new ImageIcon("LOGO.jpg"); //"LOGO.jpg


" can be replaced by image path

JLabel label = new JLabel(icon);


label.setBounds(100,50,100,30);

If the size of image is not known, we can set the image size by
following program

label.setBounds(100,50,icon.setIconWidth(),icon.setIconHeight());
c.add(label); // add the
label in container

13.How to set both Image and Text in JLabel

ImageIcon icon = new ImageIcon("LOGO.jpg");


//"LOGO.jpg" can be replaced by image path

JLabel label = new JLabel("Text", icon, JLabel.LEFT); // it can


be set right, centre by writing JLabel.RIGHT or JLabel.CENTRE

label.setBounds(100,50,100,30);

c.add(label); // add the


label in container

14. How to create JTextField

JTextField mytext = new JTextField();


mytext.setBounds(100,50,100,30);

c.add(mytext); // add the label in container

15. How to change Font style and Font size of a JTextField

JTextField mytext = new JTextField();


mytext.setBounds(100,50,100,30);

Font f = new Font("Arial", Font.BOLD, 30);


mytext.setFont(f);

c.add(mytext); // add the label in container

16. How to set Font color and background color of a JTextField

JTextField mytext = new JTextField();


mytext.setBounds(100,50,100,30);

Font f = new Font("Arial", Font.BOLD, 30);


mytext.setFont(f);

mytext.setBackground(color.YELLOW);
mytext.setForeground(color.RED)

c.add(mytext); // add the label in container

16. How to set RGB Font color and background color of a JTextField

JTextField mytext = new JTextField();


mytext.setBounds(100,50,100,30);
Font f = new Font("Arial", Font.BOLD, 30);
mytext.setFont(f);
Color clr = new Color(120, 100,50);

mytext.setBackground(clr);
mytext.setForeground(color.RED)

c.add(mytext); // add the label in container

17. How to create JPasswordField

JPasswordField pass = new JPasswordField();


pass.setBounds(100,50,100,30);

c.add(pass); // add the label in container

18. How to set Font size and color, background, foreground of a


JPasswordField

JPasswordField pass = new JPasswordField();


pass.setBounds(100,50,100,30);

Font f = new Font("Arial", Font.BOLD, 30);


pass.setFont(f);

Color clr = new Color(120, 100,50);


// if you want to get RGB color on background
mytext.setBackground(clr);
mytext.setForeground(color.RED)

c.add(pass); // add the label in container

19. How to create JButton

JButton btn = new JButton("Click Me");


btn.setBounds(100,50,100,30);

c.add(btn); // add the label in container

20. How to set Text in JButton

JButton btn = new JButton("Click Me");

btn.setText("Get");

c.add(btn); // add the label in container

21. How to set Image in JButton

ImageIcon icon = new ImageIcon("LOGO.jpg")


//"LOGO.jpg" can be replaced by image path
JButton btn = new JButton(icon);
btn.setBounds(100,50,icon.getIconWidth,icon.getIconHeight);

c.add(btn); // add the label in container

22. How to change Font style and Font size of JButton

JButton btn = new JButton("Click Me");


btn.setBounds(100,50,100,30);
Font f = new Font("Arial",Font.BOLD, 30);
btn.setFont(f)

c.add(btn); // add the label in container

23 How to set background color and font color of JButton

JButton btn = new JButton("Click Me");


btn.setBounds(100,50,100,30);
Font f = new Font("Arial",Font.BOLD, 30);
btn.setFont(f)

Color clr= new Color(100, 50,50);


// if you want to get RGB color on background

btn.setBackgroung(clr);
btn.setForegroung(Color.YELLOW);

c.add(btn); // add the label in container

24. How to set different type of cursor for a JButton

Curser cur = new Curser(Curser.HAND_CURSER);


btn.setCurser(cur);

Another curser(crosshair)

Curser cur = new Curser(Curser.CROSSHAIR_CURSER);


btn.setCurser(cur);

Another curser(WAIT)

Curser cur = new Curser(Curser.WAIT_CURSER);


btn.setCurser(cur);

c.add(btn); // add the label in container

25. How to write Action Listener for a JButton


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyFrame extends JFrame implement ActionListner


{
JButton btn = new JButton("Click");
Container c;

MyFrame()
{
c=this.getContentPane();
c.setLayout(null)
btn.setBounds(100,50,100,50);
btn.addActionLIstner(this);

c.add(btn);
}
public void actionPerformed(ActionEvent e)
{ c.setBackground(color.YELLOW);

}
}
class ActionDemo
{
public static void main(String[] args)
{
MyFrame f= new MyFrame();
f.setTitle("Action Demo");
f.setVisible(true)
f.defaultCloseOperatin(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,50,700, 500);

26. How to write Action Listener for a JButton in multiple button


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyFrame extends JFrame implement ActionListner


{
JButton btn1 = new JButton("RED");
JButton btn2 = new JButton("YELLOW");
JButton btn3 = new JButton("GREEN");

Container c;

MyFrame()
{
c=this.getContentPane();
c.setLayout(null)
btn1.setBounds(100,50,100,50);
btn2.setBounds(250,50,100,50);
btn3.setBounds(400,50,100,50);

btn1.addActionLIstner(this);
btn2.addActionLIstner(this);
btn3.addActionLIstner(this);

c.add(btn1);
c.add(btn2);
c.add(btn3);

}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1){
c.background(color.RED);
}
if(e.getSource()==btn2){
c.background(color.YELLOW);
}
if(e.getSource()==btn3){
c.background(color.GREEN);
}

}
}
class ActionDemo
{
public static void main(String[] args)
{
MyFrame f= new MyFrame();
f.setTitle("Action Demo");
f.setVisible(true)
f.defaultCloseOperatin(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,50,700, 500);

28. How to write Action Listener using anonymous inner class


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ActionDemo2
{
Static Container c;

public static void main(String[] args)


{
JFram frame=new JFame();
f.setTitle("Action Demo Using Anonymous Inner Class");
f.setVisible(true)
f.defaultCloseOperatin(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,50,700, 500);

c=frame.getContentPane();
c.setBackground(color.YELLOW);

JButton red=new JButton(RED);


red.setBounds(100,50,570,30);
c.add(red);

red.addActionListner(new ActionListner(){
public void actionPerformed(ActionEvent event){
redButtoneAction(event);
}

})

JButton green=new JButton(GREEN);


green.setBounds(250,50,570,30);
c.add(green);
JButton yellow=new JButton(YELLOW);
yellow.setBounds(400,50,570,30);
c.add(yellow);

You might also like