带有面板和按钮的 setText 方法 [英] setText method with panel and button

查看:35
本文介绍了带有面板和按钮的 setText 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多方法来解决这个问题,并尝试到处寻找答案,我的颜色按钮可以工作,但同样的方式内置的数字按钮没有......

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");
    }

}
}

我的按钮 0 不起作用...所以我无法继续编写代码.

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

推荐答案

我的按钮 0 不起作用...所以我无法继续编写代码.

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

您没有向按钮添加 ActionListener.

You didn't add an ActionListener to the button.

但是,在解决该问题之前,您应该简化代码并学习如何使用循环来创建具有相同功能的多个组件.这是一个创建单个 ActionListener 并将其添加到每个按钮的示例:

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();
            }
        });
    }
}

另外,不要在你的组件上使用 setSize().布局管理器负责确定组件的大小.

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

这篇关于带有面板和按钮的 setText 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆