This tutorial explains how you can interact with JFrame by using buttons, listeners and text fields.
Background
Interactivity is what user wants in every application. To add interactivity in a program, Java provides us a very easy way. Javax.swing.JButton calss provides us a way to add buttons and events happens after button click. Similarly with the help of javax.swing.JTextfield allow us to add text fields to JFrame.
Adding text fields
You can create a textfield using JTextfield() method. This class has many constructors like, JTextField(): It constructs a new text field
JTextField(string text): Constructs a text field with the specified text.
JTextField(string text, int column): It creats a new text field with the specified text and number of columns.
Following program shows example of adding text fields to JFrame.
package javatutorial.net; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JTextField; public class Example { static JTextField textfield1, textfield2, textfield3; public static void main(String[] args) { JFrame f = new JFrame("Text Field Examples"); f.getContentPane().setLayout(new FlowLayout()); textfield1 = new JTextField("Text field 1",10); textfield2 = new JTextField("Text field 2",10); textfield3 = new JTextField("Text field 3",10); f.getContentPane().add(textfield1); f.getContentPane().add(textfield2); f.getContentPane().add(textfield3); f.pack(); f.setVisible(true); } }
Here is the output of this code
You can customize by using different mehods provided by the JTextfield.
JTextField.setfont(Font f); It sets the font of text
JTextField.setHorizontalAlignment(int alignment); It sets the horizontal alignment of the text.
JTextFieldsetScrollOffset(int scrolloffset); It sets the scroll offset in pixels.
Adding buttons and applying action listener
Similarly you can add buttons to your JFrame. JButton provides us an easy way to add buttons and action listeners. It has many constructors like
JButton(); creates a button with no text and no icon.
JButton(String text): creates a button with specified text.
JButton(Icon icon); creates a button with specified icon.
JButton(String text, Icon icon); creates a button with specified text and icon.
Here is a simple example of JButton with image icon.
package javatutorial.net; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; public class SimpleJButton { SimpleJButton(){ JFrame f=new JFrame("Button Example"); JButton b=new JButton("Play", new ImageIcon("play.png")); b.setBounds(100,100,140, 40); f.add(b); f.setSize(300,400); f.setLayout(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new SimpleJButton(); } }
Here is the output of this code
Adding action listeners
Adding action listeners on a JButton is very easy a simple. JButton class provides a method JButton.addActionListener() which implements an override method actionPerformed(). Here in the following example I wrote a simple program which says user to enter name, when user clicks on submit button, a message “Name has been submitted.” Shows. Here is the code.
package javatutorial.net; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class SimpleJButton { SimpleJButton(){ JFrame f=new JFrame("Button Example"); //submit button JButton b=new JButton("Submit"); b.setBounds(100,100,140, 40); //enter name label JLabel label = new JLabel(); label.setText("Enter Name :"); label.setBounds(10, 10, 100, 100); //empty label which will show event after button clicked JLabel label1 = new JLabel(); label1.setBounds(10, 110, 200, 100); //textfield to enter name JTextField textfield= new JTextField(); textfield.setBounds(110, 50, 130, 30); //add to frame f.add(label1); f.add(textfield); f.add(label); f.add(b); f.setSize(300,300); f.setLayout(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //action listener b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { label1.setText("Name has been submitted."); } }); } public static void main(String[] args) { new SimpleJButton(); } }
Here is the output of this code
Here is the link of complete code, you can download.
very good help.thanx.
thank you so much for providing suh valuable info
Thank you