简体   繁体   中英

My JButtons don't work

    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 JTextField display = new JTextField();
    public JTextField comboul = new JTextField();
    private JPanel panel;
    public int predictie;

    public Guii(){
        super("Heads or Tails");
        setContentPane(panel);
        initUi();
        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;*/

    }
    public void initUi(){



        heads.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                predictie = 0;
                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){
                predictie = 1;
                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);
                }
            }





        });}}

Why my buttons don't work?I think i added everything to work.I made the buttons, added actionListener and actionPerfomed.Also added @Override because somebody said to.

I checked that in the other class with a while function.Thank you.

You didn't add the buttons (and textfield) anywhere, as far as I can see. Also, the method in which you add the listeners is never called - except by the listener itself, but as that one is never added...

Try adding the buttons to the panel in the constructor and then also add the listeners to the buttons.

Where you initialize your panel, buttons, textfields? This code could not work. Look at you console output!

The code must contain somethin like:

panel = new JPanel();
panel.add(heads);
panel.add(tails);
panel.add(display);
panel.add(comboul);

setContentPane(panel);

and it looks you forget to call dacaTails(); So add this to the construction too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM