Hello @joaoazevedo, I’ll explain in my brief how it works and give an example of Actionlistener.
So, Actionlistener is a code that allows you to give an action to an object...
For example here I defined that the variable 'exit' will have an action to give an 'Exit' in the programming.
- Actionlistener = creates a list of events for the object.
Actionevent = creates the variable list event and inside the event
is placed its respective action...
sair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Here I’ll complete my programming if you want to copy and run your tests in your IDE.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class telaPrincipal extends JFrame{
private Container c1;
private JButton sair, inicio;
private ImageIcon img1;
private JLabel imgfundo, logo;
Quiz frame = new Quiz();
public telaPrincipal() {
inicializarComponentes();
definirEventos();
}
private void inicializarComponentes() {
setLayout(null);
c1 = getContentPane();
getContentPane().setBackground(Color.black);
img1 = new ImageIcon(getClass().getResource("telaprincipal2.gif"));
imgfundo = new JLabel(img1);
inicio = new JButton("Iniciar");
sair = new JButton("Sair");
sair.setMnemonic('S');
logo = new JLabel("QUIZ PROGRAMER FOR PROGRAMERS");
logo.setForeground(Color.white);
logo.setFont(new Font ("SansSerif", Font.ITALIC, 50));
logo.setBounds(5,20,1500,60);
inicio.setFont(new Font("Arial", Font.BOLD, 60));
inicio.setForeground(Color.white);
inicio.setOpaque (false);
inicio.setContentAreaFilled(false);
inicio.setBorderPainted (false);
sair.setFont(new Font("Arial", Font.BOLD, 60));
sair.setForeground(Color.white);
sair.setOpaque (false);
sair.setContentAreaFilled(false);
sair.setBorderPainted (false);
imgfundo.setBounds(0,20,960,740);
inicio.setBounds(360,260,250,80);
sair.setBounds(360,460,250,80);
add(logo);
add(inicio);
add(sair);
add(imgfundo);
}
public void definirEventos() {
sair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
inicio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Quiz p1 = new Quiz();
//p1.validate();
//c1.add(p1);
//c1.removeAll();
//c1.validate();
}
});
}
public static void main(String args[]) {
telaPrincipal frame = new telaPrincipal();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setBounds(200,0,960,740);
frame.setVisible(true);
}
}
I hope very much to have helped, if you are in doubt do not hesitate to ask.
alias, here in this code it calls another Jframe. start.addActionListener(new Actionlistener() {...});
– Matheus Vidigal
ah vlw...but while I was thinking I created another question, like I create a program or the login screen for example, and put if click the button open another program . java?
– joaoazevedo
yes yes@ joaoazevedo, for example my 'start' button you would change it to 'login', and inside it you call the screen normally as I did and on the screen to be called you put your "application".
– Matheus Vidigal