班长:
import javax.swing.*;
import java.util.Random;
public class Principal extends Guii {
public int combo;
public static Random bulion = new Random();
public static boolean sansa;
public static boolean input;
public int status;
//STATUS 0 = HEADS;
//STATUS 1 = TAILS;
public static void main(String[] args) {
Guii lee = new Guii();
Principal obiect = new Principal();
}
public int flip(){
boolean sansa2 ;
sansa2 = bulion.nextBoolean();
if(sansa2){
status = 0;
display.setText("Heads");
}
else{
status = 1;
display.setText("Tails");
}
return status;
}
public int returnStatus(){
return status;
}
}
班级Guii:
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import java.awt.Dimension;
public class Guii extends JFrame{
Principal obiect;
public JButton heads = new JButton("Heads");
public JButton tails = new JButton("Tails");
public JTextArea display = new JTextArea();
public JTextArea comboul = new JTextArea();
private JPanel panel;
public int predictie;
public Guii(){
super("Heads or Tails");
setContentPane(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);}
public void dacaHeads(){
if(heads.getModel().isPressed()) predictie = 0;
}
public void dacaTails(){
if(tails.getModel().isPressed()) predictie = 1;
heads.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
dacaHeads();
obiect.flip();
if(predictie == obiect.returnStatus() ){
String s = comboul.getText();
int combo = Integer.valueOf(s);
s = Integer.toString(++combo);
comboul.setText("asdsaad");}
else{
String z = "0";
comboul.setText("asdasda");
}
}
});
tails.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
dacaTails();
obiect.flip();
if(predictie == obiect.returnStatus() ){
String s = comboul.getText();
int combo = Integer.valueOf(s);
s = Integer.toString(++combo);
comboul.setText(s);}
else{
String z = "0";
comboul.setText(z);
}
}
});}
问题是窗口打开,我看到了所有内容,但按下按钮时没有任何反应。 我使用了来自intellij想法的gui设计师。 谢谢。 //对不起第二个问题。删除它。
答案 0 :(得分:1)
我不知道编写代码是否错误,但是函数dacaTails()会将监听器添加到按钮,似乎从未调用过。你应该把addActionListener函数放在构造函数方法中。我想。