Você está na página 1de 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

Você também pode gostar