setText method with panel and button

2020-04-30 16:26发布

I tried fixing this in so many ways, and tried searching for answers everywhere, my color buttons are working, but same way built number buttons do not....

   package tralala;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import java.awt.*;

public class DugmiciKojiMenjajuBoju extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 4317497849622426733L;
private JButton dugme1;
private JButton dugme2;
private JButton dugme3;
private JPanel panel;
private JOptionPane a;
private JPanel panel1;
private JButton dugmeBroja0;
private JButton dugmeBroja1;
private JButton dugmeBroja2;
private JButton dugmeBroja3;
private JButton dugmeBroja4;
private JButton dugmeBroja5;
private JButton dugmeBroja6;
private JButton dugmeBroja7;
private JButton dugmeBroja8;
private JButton dugmeBroja9;
private JButton dugmeZaPlus;
private JButton dugmeZaMinus;
private JButton dugmeZaPuta;
private JButton dugmeZaPodeljeno;
private JButton dugmeZaJednako;
private JTextField polje;

DugmiciKojiMenjajuBoju(){
    getContentPane().setBackground(Color.white);
    this.setLayout(new FlowLayout());
    a = new JOptionPane("");
    dugme1 = new JButton("zuta");
    dugme2 = new JButton("plava");
    dugme3 = new JButton("crvena");

    dugmeBroja0 = new JButton("0");
    dugmeBroja1 = new JButton("1");
    dugmeBroja2 = new JButton("2");
    dugmeBroja3 = new JButton("3");
    dugmeBroja4 = new JButton("4");
    dugmeBroja5 = new JButton("5");
    dugmeBroja6 = new JButton("6");
    dugmeBroja7 = new JButton("7");
    dugmeBroja8 = new JButton("8");
    dugmeBroja9 = new JButton("9");
    dugmeZaPlus = new JButton("+");
    dugmeZaMinus = new JButton("-");
    dugmeZaPuta = new JButton("*");
    dugmeZaPodeljeno = new JButton("/");
    dugmeZaJednako = new JButton("=");
    polje = new JTextField("", 20);

    panel = new JPanel();
    panel1 = new JPanel();
    dugmeBroja0.setSize(50,50);
    dugmeBroja1.setSize(50,50);
    dugmeBroja2.setSize(50,50);
    dugmeBroja3.setSize(50,50);
    dugmeBroja4.setSize(50,50);
    dugmeBroja5.setSize(50,50);
    dugmeBroja6.setSize(50,50);
    dugmeBroja7.setSize(50,50);
    dugmeBroja8.setSize(50,50);
    dugmeBroja9.setSize(50,50);


    panel1.add(dugmeBroja0);
    panel1.add(dugmeBroja1);
    panel1.add(dugmeBroja2);
    panel1.add(dugmeBroja3);
    panel1.add(dugmeBroja4);
    panel1.add(dugmeBroja5);
    panel1.add(dugmeBroja6);
    panel1.add(dugmeBroja7);
    panel1.add(dugmeBroja8);
    panel1.add(dugmeBroja9);
    panel1.add(dugmeZaPlus);
    panel1.add(dugmeZaMinus);
    panel1.add(dugmeZaPuta);
    panel1.add(dugmeZaPodeljeno);
    panel1.add(dugmeZaJednako);
    panel1.add(polje);


    panel1.setLayout(new FlowLayout(50, 0 ,0));
    FlowLayout mojLayout = new FlowLayout(FlowLayout.CENTER , 20 , 20);
    panel.setLayout(mojLayout);
    panel.add(dugme1);
    panel.add(dugme2);
    panel.add(dugme3);

    this.setTitle("Kalkulator Koji Menja Boju Pozadine");
    this.setSize(500,500);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.add(panel1);
    this.add(panel);
    dugme1.addActionListener(this);
    dugme2.addActionListener(this);
    dugme3.addActionListener(this);
    polje.addActionListener(this);
    panel.setBackground(Color.white);
    panel1.setBackground(Color.white);
}
public static void main(String[] args) {
     DugmiciKojiMenjajuBoju  a = new  DugmiciKojiMenjajuBoju();
     a.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent arg0) {
    if( arg0.getSource() == dugme1){
        panel.setBackground(Color.yellow);
        panel1.setBackground(Color.yellow);
        getContentPane().setBackground(Color.yellow);
        a.setSize(200,200);
        a.showMessageDialog(this, "Klikcite dalje :D" ,"Postavili ste zutu",JOptionPane.INFORMATION_MESSAGE);
    }
    else if(arg0.getSource() == dugme2){
        panel.setBackground(Color.blue);
        panel1.setBackground(Color.blue);
        getContentPane().setBackground(Color.blue);
        a.setSize(200,200);
        a.showMessageDialog(this, "Klikcite dalje :D","Postavili ste plavu", JOptionPane.INFORMATION_MESSAGE);
    }
    else if(arg0.getSource() == dugme3){
        panel.setBackground(Color.red);
        panel1.setBackground(Color.red);
        getContentPane().setBackground(Color.red);
        a.setSize(200,200);
        a.showMessageDialog(this,  "Klikcite dalje :D","Postavili ste crvenu",JOptionPane.INFORMATION_MESSAGE);
    }
    else if(arg0.getSource() == dugmeBroja0){
        polje.setText("0");
        System.out.println("blabla");
    }

}
}

My button 0 is not working... so i cannot continue writing the code.

标签: java swing
3条回答
时光不老,我们不散
2楼-- · 2020-04-30 16:45

I think you didn't added this as the actionListener of dugmeBroja0:

you need this:

dugmeBroja0.addActionListener(this);
查看更多
够拽才男人
3楼-- · 2020-04-30 16:47

My button 0 is not working... so i cannot continue writing the code.

You didn't add an ActionListener to the button.

However, before just fixing that problem you should simplify your code an learn how to use loops to create multiple components with the same functionality. Here is an example that creates a single ActionListener and add it to each button:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
//              display.setCaretPosition( display.getDocument().getLength() );
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(50, 50) );
            buttonPanel.add( button );
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Also, don't use setSize() on your components. The layout manager is responsible for determining the size of a component.

查看更多
ら.Afraid
4楼-- · 2020-04-30 16:55

You have never added ActionListener to that button, just to three color buttons and a JTextField polje

查看更多
登录 后发表回答