Olá,
Fiz um código com o botão alternaTexto mas, na hora de compilar ele não dá erro mas na interface gráfica ao apertar o botão bom dia ele não alterna para outra mensagem e sim para vazio. O código é este:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Saudacao extends JFrame implements ActionListener {
JButton bt_saud;
public Saudacao() {
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
getContentPane().setLayout(null);
bt_saud = new JButton("Bom Dia!");
bt_saud.setBounds(80,100,100,30);
bt_saud.addActionListener(this);
getContentPane().add(bt_saud);
}
private static String alternaTexto() {
return null;
}
public void stop() {
if (bt_saud != null) {
getContentPane().remove(bt_saud);
bt_saud = null;
}
}
public static void main(String args[]) {
JFrame obj_bomdia = new Saudacao();
obj_bomdia.setVisible(true);
}
public void actionPerformed (ActionEvent e) {
bt_saud.setText(alternaTexto());
}
public String alternTexto() {
String texto = "";
Random num = new Random();
switch (num.nextInt(3)+1) {
case 1: texto = "Bom Dia!!"; break;
case 2: texto = "Boa Tarde!!"; break;
case 3: texto = "Boa Noite!!"; break;
}
return texto;
}
}
Alguém poderia me dizer onde está o erro?