Multiple JLabel

This program uses a JFrame container to hold an instance of a multiple JLabel. The labels displays a multiple text message. Program       Program Source import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.FlowLayout; class Frame extends JFrame{ Frame() { setTitle(“Multiple JLabel”); setLayout(new FlowLayout()); setLabel(); setSize(700, 200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void setLabel() { JLabel jl1 … Read more…

JButton

The JButton class provides a rich set of features to of design highly user-friendly buttons.with initial text and an icon. Program       Program Source import javax.swing.JFrame; import javax.swing.JButton; import java.awt.FlowLayout; class Frame extends JFrame{ Frame() { setTitle(“JButton”); setLayout(new FlowLayout()); setButtons(); setSize(700, 200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void setButtons() { JButton jb1 = new … Read more…

JButton with ActionListener and ActionEvent

Event Handling When the user clicks a button or perform any other interface related action in your application, an interface event occurs. Each components has a predefined set of events. When an event occurs in your application, the application¬†notified and takes the appropriate action. Events are passed from source controls to Listener objects. That means … Read more…

Implement the ActionListener Interface As An Inner Class

Program       Program Source import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JOptionPane; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class Frame extends JFrame { private JButton jb; Frame() { setTitle(“JButton with ActionListener”); setLayout(new FlowLayout()); setButton(); setAction(); setSize(700, 200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void setButton() { jb = new JButton(“JButton”); add(jb); } private void setAction() { jb.addActionListener(new … Read more…

setActionCommand() getActionCommand()

SetActionCommand() This method changes the action command string, but does not affect the string used to label the button. getActionCommand() The action command identifies the button. Thus, when using two or more buttons within the same application, the action command gives you an easy way to determine which button was pressed. Program       … Read more…

ActionEvent.getSource()

The EventObject contains the getSource( ) method.Suppose you have many buttons in your application. So, you can find which button is cliked by, using the getSource() method. The getSource( ) method returns the source of the event. Program       Program Source import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JOptionPane; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; … Read more…