I am working on guessing game.
In the ActionPerformed
function, if the button is clicked, then add JLabel
, but if I use JLabel
, it is not working separately but if use setText
than both working (JLabel+setText
). I want to use Jlabel
separately.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class guess_game1 extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JButton[] jbut = new JButton [5];
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JLabel jl = new JLabel("Cots!");
JButton s;
Random rand = new Random();
public guess_game1(){
for(int i=0; i<jbut.length ;i++){
jbut[i] = new JButton("");
jbut[i].setPreferredSize(new Dimension(100,70));
jp1.add(jbut[i]);
jbut[i].addActionListener(this);
}
s = new JButton("Shuffle");
s.addActionListener(this);
jp2.add(s);
add(jp1);
add(jp2,BorderLayout.SOUTH);
setSize(700,200);
setLocationRelativeTo(null);
setTitle("Awais's Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new guess_game1();
}
@Override
public void actionPerformed(ActionEvent a) {
JButton j = (JButton) a.getSource();
if(a.getSource() == s){
int ran = rand.nextInt(5);
jbut[ran].add(jl);
// jbut[ran].setText("Awais");
}
}
Aucun commentaire:
Enregistrer un commentaire