|
||
|
||||
|
This code is an example of how to add ActionListener to a JButton.
When the JButton is clicked, the desired action is performed. In this case it prints a message to the console. Java Code
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonAction {
private static void createAndShowGUI() {
JFrame frame1 = new JFrame("JAVA");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton(" >> JavaProgrammingForums.com <<");
//Add action listener to button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
}
});
frame1.getContentPane().add(button);
frame1.pack();
frame1.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
__________________
Don't forget to add code tags around your code: ![]() Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
|
|||
|
Thank you for helping. I think this book is moving a bit too quick for me. My friend says he learned nothing about Gui until his second year. I have started reading a new book, and I will post another question soon. Thanks again.
IDK12 |
|
|||
|
inside the post method u can call get method. if u r sending post request.
if u r sending get request u can call post method inside the Get method try it once. |
![]() |
| Tags |
| actionlistener, java swing, jbutton |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to Add a JMenu toolbar to a Java Swing application | JavaPF | Java Code Snippets and Tutorials | 2 | 04-05-2009 02:56 PM |
| JAVA ImageIcon and JButton resizing problem -- please go easy on me. :)? | antitru5t | AWT / Java Swing | 1 | 13-03-2009 09:39 AM |