Swing tutorial et multi button listener (facile)


Contenu du snippet

Swing tutorial et multi button listener (facile)
tres bon pour les débutants, facile a comprendre mais jlai expliquer en anglais car jai poster chez PSC aussi

Source / Exemple :


// Copyright J-f Mitchell, this litle peace of code is a great
// tutorial for swing begigner.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JavaApp {
	JFrame frame1;
	JButton command1;
	JTextField text1;  //Declaring variables in class to able to use it in the whole class.
	JPanel panel1;
	JButton command2;
	
	
	public JavaApp() {
		
		frame1 = new JFrame("Put your title here"); //Main frame
	frame1.setSize(50, 50);
		command1 = new JButton("Button 1"); // creating control from declared variable in the class
		text1 = new JTextField(20);
		panel1 = new JPanel();
		command2 = new JButton("Button 2");
		
		
			command1.setMnemonic(KeyEvent.VK_I); //This is an event listener without the implements in the class, I like this method!
        command1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        text1.setText("Button 1!");
            }
        });
			
			
				command2.setMnemonic(KeyEvent.VK_I); // listener for the second button
        command2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        text1.setText("Button 2!");
            }
        });
			
		
		panel1.setLayout(new FlowLayout());
		panel1.add(text1);
		panel1.add(command1); //adding components to pane, you need this to be able to see ure component,
		panel1.add(command2);
		
		frame1.getContentPane().add(panel1, BorderLayout.CENTER); //add my pane to my frame

        // Exit when the window is closed.
       frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

	// show the frame!
	frame1.pack();
	frame1.setVisible(true);
}

 public static void main(String[] args) {
	// set the look and feel
	try {
	    UIManager.setLookAndFeel(
		UIManager.getCrossPlatformLookAndFeelClassName()); //getting the java swing look
	} catch(Exception e) {}

	JavaApp javaapp = new JavaApp(); // tell the program to show up the frame by calling my GUI function
	
    }
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.