|
||
|
||||
|
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 syntax highlighted code tags around your code: [highlight=Java] code here [/highlight] 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. |
|
|||
|
Quote:
What code do I use instead of " System.out.println("You clicked the button");" if I want the button to open a new class from my code? In this case I want the button to randomly select five cards from my deck of cards in the code. Java Code
//Necessary imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SlumpaKort extends JPanel {
private static final long serialVersionUID = 1L;
//Knappvariabler
private static String buttonString = "Slumpa Kort";
private JButton btnRefresh;
/*
* SlumpaKort-konstruktor
*/
public SlumpaKort() {
//Skapa en JPanel
super(new BorderLayout());
//Skapa knappen
btnRefresh = new JButton(buttonString);
btnRefresh.setActionCommand(buttonString);
btnRefresh.addActionListener(new Clicked());
//Skapa en panel som knappen kan ligga på
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.add(btnRefresh);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
//Lägg till allt ovan till fönstret
add(buttonPane, BorderLayout.PAGE_END);
}
/*
* Klick-Event för knappen
*/
class Clicked implements ActionListener {
class Kort {
private String[] farg ={"Hjärter", "Spader", "Ruter", "Klöver"};
private String[] valor ={"Ess", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Knäckt", "Dam", "Kung"};
//* Visar vilka färger samt valörer som finns representerade i kortleken *//
public int f, v;
public Kort(int farg, int valor) {
if (farg < 1 || farg > 4 ||
//* Fyra fäger *//
valor <1 || valor > 13) {
//* 13 valörer *//
}
f = farg;
v = valor;
}
public int geFarg() {
return f;
}
public int geValor() {
return v;
}
public String toString() {
return farg[f - 1] + " " + valor[v - 1];
}
}
public class Kortlek {
public Kort[] bunt = new Kort[52];
public int antal = 0;
public int geAntal () {
return antal;
}
public void laggOverst(Kort k) {
bunt[antal] = k;
antal++;
}
public Kort geOversta() {
antal--;
return bunt[antal];
}
public void görKortlek() {
for (int f=1; f<=4; f++)
for (int v=1; v<=13; v++)
laggOverst(new Kort(f, v));
}
public void blanda() {
for (int i=0; i<antal; i++) {
int n = i + (int)(Math.random() * (antal - i));
Kort temp = bunt[n];
bunt[n] = bunt[i];
bunt[i] = temp;
}}
public void main( String args[]){
//* Blanda kortlek *//
Kortlek kortlek = new Kortlek();
kortlek.görKortlek();
kortlek.blanda();
//*Loopar kortlek.geOversta fem gånger för att sedan dela ut fem slumpade kort*//
for (int i=0; i<5; i++){
Kort k = kortlek.geOversta();
System.out.println(k.toString());
}
}
}
public void actionPerformed(ActionEvent e) {
//Vad som ska hända när man klickar på knappen
JOptionPane.showMessageDialog(null, "Detta fungerar!");
}
}
/*
* Startpunkten för programmet.
* Här skapas fönstret.
*/
public static void main(String[] args) {
//Skapa formen
JFrame frame = new JFrame("Cards");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
//Skapa TestButton
JComponent contentPane = new SlumpaKort();
contentPane.setOpaque(true);
frame.setContentPane(contentPane);
//Visa fönstret
frame.pack();
frame.setVisible(true);
}
}
|
![]() |
| 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 | 3 | 30-04-2010 02:00 PM |
| JAVA ImageIcon and JButton resizing problem -- please go easy on me. :)? | antitru5t | AWT / Java Swing | 1 | 13-03-2009 08:39 AM |
|
100 most searched terms
Search Cloud
|
| 2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps |