How do I compare java swing button icons to check equality? - java

I am making a Tic-Tac-Toe game in Java to teach myself the Swing class. I am having to problems, though.
First, how do you compare buttons using icons?
symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
symO = new ImageIcon(this.getClass().getResource("symbolO.png"));
I use those 2 variables to set the button images.
if (e.getSource() instanceof JButton) {
JButton source = (JButton) e.getSource();
if (isX) {
source.setIcon(symX);
source.setEnabled(false);
source.setDisabledIcon(symX);
} else {
source.setIcon(symO);
source.setEnabled(false);
source.setDisabledIcon(symO);
}
}
The second question is where do you compare objects are an Action Event? I tried to compare inside of the if statements in the above code, but Eclipse always gives me compile time errors doing that.
If I place the code in the method with the buttons, it seems like java never gets to them.
As requested, here is the entirety of my Java file.
package ticTacToeGUI;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class tttGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
private ImageIcon symX, symO;
private ImageIcon symIco = new ImageIcon(this.getClass().getResource("symbolX.png"));
private boolean isX = true;
public static void main(String[] args) {
new tttGUI();
}
public tttGUI() {
//Setup the window.
super("Tic-Tac-Toe GUI 1.0");
setSize(425,425);
setIconImage(symIco.getImage());
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Create the content.
symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
symO = new ImageIcon(this.getClass().getResource("symbolO.png"));
panel.setLayout(new GridLayout(3,3));
setVisible(true);
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
JButton btn4 = new JButton();
JButton btn5 = new JButton();
JButton btn6 = new JButton();
JButton btn7 = new JButton();
JButton btn8 = new JButton();
JButton btn9 = new JButton();
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
add(panel);
revalidate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
JButton source = (JButton) e.getSource();
if (isX) {
source.setIcon(symX);
source.setEnabled(false);
source.setDisabledIcon(symX);
} else {
source.setIcon(symO);
source.setEnabled(false);
source.setDisabledIcon(symO);
}
}
isX ^= true;
}
}

I know this is two years too late, but other people in the future might find this useful...
Anyway, coincidentally I'm programming up 3D Tic Tac Toe.
To compare ImageIcons, I initialized the Icons with a String description, and then used:
((ImageIcon) JLabel().getIcon()).getDescription().compareTo("someOtherDesc")
Hope that helps you or someone else in future...

I have some ideas on how to do that, for instance:
symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
symO = new ImageIcon(this.getClass().getResource("symbolO.png"));
if (btn1.getIcon().equals(symX) && btn2.getIcon().equals(symX) {
// Your logic here...
}
This can be done inside you actionPerformed() method if you promote your local variables btn1...btn9 to instance variables. It seems you mixed up concepts since you already have btn1...btn9 declared as instance variables, but also create new ones as local variables.
public class TttGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel = new JPanel();
private JButton btn1 = new JButton();
private JButton btn2 = new JButton();
private JButton btn3 = new JButton();
private JButton btn4 = new JButton();
private JButton btn5 = new JButton();
private JButton btn6 = new JButton();
private JButton btn7 = new JButton();
private JButton btn8 = new JButton();
private JButton btn9 = new JButton();
private ImageIcon symX, symO;
private ImageIcon symIco = new ImageIcon(this.getClass().getResource("symbolX.png"));
private boolean isX = true;
public static void main(String[] args) {
new TttGUI();
}
public TttGUI() {
//Setup the window.
super("Tic-Tac-Toe GUI 1.0");
setSize(425,425);
setIconImage(symIco.getImage());
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Create the content.
symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
symO = new ImageIcon(this.getClass().getResource("symbolO.png"));
panel.setLayout(new GridLayout(3,3));
setVisible(true);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
add(panel);
revalidate();
}
}
I have changed your class name so that it is compliant to Java conventions (class names must always start with capital case).

Though it is too late but anyone who search may get help in comparing imageicons.I think it is very easy to campare
ImageIcon icon = new ImageIcon("images/myimage.jpg");
JLabel a = new JLabel(icon);
JLabel b = new JLabel(icon);
if(a.getIcon().toString().equals(b.getIcon().toString()))
{
//do whatever
}

Related

Use of JButton array Java

I am not sure what I am doing wrong. I am trying to create 4 buttons using the arrays and passing the method to the Set Panel class. When I run it I only get one button. Any help would be great. Essentially what I want is a Panel with four buttons, through the use of Methods.
public class SetButtons extends JButton implements ActionListener {
JButton [] buttonArray = new JButton[4];
JButton exitBtn, newGameBtn, checkBtn, clearBtn;
Font myFont = new Font("ink free", Font.BOLD,22);
public SetButtons(){
this.exitBtn = new JButton("Exit");
this.newGameBtn = new JButton("New Game");
this.checkBtn = new JButton("Check Answer");
this.clearBtn = new JButton("Clear");
this.buttonArray[0] = exitBtn;
this.buttonArray[1] = newGameBtn;
this.buttonArray[2] = checkBtn;
this.buttonArray[3] = clearBtn;
for (int i = 0; i < 4; i++){
this.buttonArray[i].addActionListener(this);
this.buttonArray[i].setFont(myFont);
this.buttonArray[i].setFocusable(false);
this.buttonArray[i].setLayout(new FlowLayout());
}
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this){
System.exit(0);
}
}
}
import javax.swing.*;
import java.awt.*;
public class SetPanel extends JPanel {
Font myFont = new Font("ink free", Font.BOLD,22);
SetLabels labels;
SetButtons exitButton;
SetPanel(){
SetButtons btn = new SetButtons();
this.add(btn);
this.setBackground(Color.darkGray);
this.setLayout(new FlowLayout());
}
}

Java Text Field Not Updating

I am trying to make a calculator program where click a button and then it would add to the Java Text Field(Not the Java Text Area), and I use the ActionEvent e to figure out the action. Then using the actionCommand where I get the actionListener I try to add the text to the java text field. And somehow it is not updating into the java text field. Is this a problem with repaint or something.
Here's my code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.Writer;
import java.util.ArrayList;
public class Calculator extends JFrame implements ActionListener {
String actionCommand = "";
ArrayList<Integer> numberSet1 = new ArrayList<Integer>();
ArrayList<Integer> numberSet2 = new ArrayList<Integer>();
JLabel jl = new JLabel();
JPanel jp = new JPanel();
int number1 = 0;
int number2 = 0;
JTextField jtf = new JTextField();
JButton btn1;
JTextArea jta = new JTextArea();
public Calculator() {
super("Calculator");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600,600);
jp.setLayout(new BorderLayout());
JTextField jtf = new JTextField("This is the text field");
jtf.setSize(getWidth(),50);
add(jtf, BorderLayout.NORTH);
jtf.setBackground(Color.YELLOW);
jtf.setEditable(false);
JPanel jp = new JPanel();
Font myFont = new Font("Serif", Font.BOLD, 32);
jp.setLayout(new GridLayout(4,4));
JButton butt0 = new JButton("0");
butt0.setFont(myFont);
JButton butt1 = new JButton("1");
butt1.setFont(myFont);
JButton butt2 = new JButton("2");
butt2.setFont(myFont);
JButton butt3 = new JButton("3");
butt3.setFont(myFont);
JButton butt4 = new JButton("4");
butt4.setFont(myFont);
JButton butt5 = new JButton("5");
butt5.setFont(myFont);
JButton butt6 = new JButton("6");
butt6.setFont(myFont);
JButton butt7 = new JButton("7");
butt7.setFont(myFont);
JButton butt8 = new JButton("8");
butt8.setFont(myFont);
JButton butt9 = new JButton("9");
butt9.setFont(myFont);
JButton butt10 = new JButton("+");
butt10.setFont(myFont);
JButton butt11 = new JButton("-");
butt11.setFont(myFont);
JButton butt12 = new JButton("*");
butt12.setFont(myFont);
JButton butt13 = new JButton("/");
butt13.setFont(myFont);
JButton butt14 = new JButton("=");
butt14.setFont(myFont);
JButton butt15 = new JButton("clear");
butt15.setFont(myFont);
butt1.setBackground(Color.cyan);
butt2.setBackground(Color.cyan);
butt3.setBackground(Color.cyan);
butt4.setBackground(Color.cyan);
butt5.setBackground(Color.cyan);
butt6.setBackground(Color.cyan);
butt7.setBackground(Color.cyan);
butt8.setBackground(Color.cyan);
butt9.setBackground(Color.cyan);
butt10.setBackground(Color.red);
butt11.setBackground(Color.red);
butt12.setBackground(Color.red);
butt13.setBackground(Color.red);
butt14.setBackground(Color.red);
butt15.setBackground(Color.red);
butt0.setBackground(Color.cyan);
butt10.setForeground(Color.lightGray);
butt11.setForeground(Color.lightGray);
butt12.setForeground(Color.lightGray);
butt13.setForeground(Color.lightGray);
butt14.setForeground(Color.lightGray);
butt15.setForeground(Color.lightGray);
jp.add(butt0);
jp.add(butt1);
jp.add(butt2);
jp.add(butt3);
jp.add(butt4);
jp.add(butt5);
jp.add(butt6);
jp.add(butt7);
jp.add(butt8);
jp.add(butt9);
jp.add(butt10);
jp.add(butt11);
jp.add(butt12);
jp.add(butt13);
jp.add(butt14);
jp.add(butt15);
butt0.addActionListener(this);
butt0.setActionCommand("0");
butt1.addActionListener(this);
butt1.setActionCommand("1");
butt2.addActionListener(this);
butt2.setActionCommand("2");
butt3.addActionListener(this);
butt3.setActionCommand("3");
butt4.addActionListener(this);
butt4.setActionCommand("4");
butt5.addActionListener(this);
butt5.setActionCommand("5");
butt6.addActionListener(this);
butt6.setActionCommand("6");
butt7.addActionListener(this);
butt7.setActionCommand("7");
butt8.addActionListener(this);
butt8.setActionCommand("8");
butt9.addActionListener(this);
butt9.setActionCommand("9");
butt10.addActionListener(this);
butt10.setActionCommand("+");
butt11.addActionListener(this);
butt11.setActionCommand("-");
butt12.addActionListener(this);
butt12.setActionCommand("*");
butt13.addActionListener(this);
butt13.setActionCommand("/");
butt14.addActionListener(this);
butt14.setActionCommand("=");
butt15.addActionListener(this);
butt15.setActionCommand("clear");
add(jp, BorderLayout.CENTER);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
actionCommand = e.getActionCommand();
System.out.println("Clicked" + actionCommand);
jtf.setText(actionCommand);
}
public void phase1() {
while (!(actionCommand.equals("+")) || !(actionCommand.equals("-")) || !(actionCommand.equals("*")) || !(actionCommand.equals("/")) || !(actionCommand.equals("clear")) || !(actionCommand.equals("="))) {
numberSet1.add(Integer.parseInt(actionCommand));
}
for(int i = 0; i < numberSet1.size(); i++) {
}
}
public void phase2() {
}
public int calculations() {
return 0;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
}
}
You have two different JTextField variables that are both named jtf.
The first is an instance variable, which you have declared with JTextField jtf = new JTextField();, and is accessible from anywhere in the class.
The second is a local variable, which you have declared with JTextField jtf = new JTextField("This is the text field");, and is only accessible in the constructor of Calculator.
The problem is that the second jtf (the local variable) is being added to the UI, while the first jtf (the instance variable) is what's being updated by the action event.
To fix this, change this (near the top of the class): JTextField jtf = new JTextField();
to this: JTextField jtf;
And then change this (in the constructor):
JTextField jtf = new JTextField("This is the text field");
to this: jtf = new JTextField("This is the text field");
Then, you'll only have one jtf variable (which will be an instance variable), and your action event should work.
Just remove this line:
JTextField jtf = new JTextField("This is the text field");
from constructor.
The first is an instance variable, JTextField e.g. jtf is accessible from anywhere in the class.

How come my lists doesn't print when I run the program?

It runs okay, but my lists doesn't print at all. Also, where should I add the convert to lowercase method for my lists? And I also need to trim the white spaces before or after the lists, which method is best for that?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class ShoppingList extends JFrame {
private JPanel groceryPanel;
private JPanel selectedGroceryPanel;
private JPanel buttonPanel;
private JList groceryList;
private JList selectedGroceryList;
private JLabel label;
private JTextField selectedGroceryItem;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private String[] lists = { "Burger Patty", "Honey Ham", "Milk",
"Egg", "Orange Juice", "Ketchup", "Lettuce",
"Hamburger Buns", "Tomatoes", "Cheese"
};
//ctor
public ShoppingList() {
setTitle("Shopping List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildGroceryPanel();
buildSelectedGroceryPanel();
buildButtonPanel();
add(groceryPanel, BorderLayout.NORTH);
add(selectedGroceryPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}//end ctor
private void buildGroceryPanel() {
groceryPanel = new JPanel();
groceryList = new JList(lists);
groceryList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}//end buildGroceryPanel
private void buildSelectedGroceryPanel() {
selectedGroceryPanel = new JPanel();
label = new JLabel("You selected: ");
selectedGroceryList = new JList();
selectedGroceryItem = new JTextField(10);
selectedGroceryItem.setEditable(false);
selectedGroceryPanel.add(label);
selectedGroceryPanel.add(selectedGroceryItem);
}//end buildSelectedGroceryPanel
private void buildButtonPanel() {
buttonPanel = new JPanel();
button1 = new JButton("ADD");
button2 = new JButton("REMOVE");
button3 = new JButton("SAVE");
button4 = new JButton("LOAD");
button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
button3.addActionListener(new ButtonListener());
button4.addActionListener(new ButtonListener());
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(button4);
}//end buildButtonPanel
private class ButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object[] selections =
groceryList.getSelectedValues();
selectedGroceryList.setListData(selections);
}
}
public static void main(String[] args) {
new ShoppingList();
}//end main
}
You need to add your groceryList to your groceryPanel. Adding groceryPanel.add(groceryList); in buildGroceryPanel() will help.
Not sure if this is what you're asking for, but there already exists two methods for what you're speaking of in the String class. Example:
String str = " SamPle TeXt ";
str = str.toLowerCase().trim();
System.out.println(str);
Will print out "sample text"

setText issue using getActionCommand

I am a beginner programmer trying to make a simple calculator. Towards the end of my code I try and have the text field show whatever button the user presses. For some reason the text.setText("7") does not show up in the text field. Could you please help me with this?
import javax.swing.*; //imports all that is needed for the code
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
public JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16;
public JTextArea text;
public Calculator()
{
setSize(350,300); //sets size to 300 by 300 //LOOK UP HOW TO LOCK SIZE
setResizable(false); //does not let user change the size of the window
setDefaultCloseOperation(EXIT_ON_CLOSE);//makes app close when I press the x on the top left
Container contentPane = getContentPane();//gets the contentPane
contentPane.setBackground(Color.CYAN);//sets background color to white
contentPane.setLayout(new FlowLayout());//makes the contentPane read from left to right
JTextArea text = new JTextArea(1, 25);
contentPane.add(text);
text.setEditable(false);
button1 = new JButton ("7");
contentPane.add(button1);
button1.addActionListener(this);
button2 = new JButton ("8");
contentPane.add(button2);
button2.addActionListener(this);
button3 = new JButton ("9");
contentPane.add(button3);
button3.addActionListener(this);
button4 = new JButton ("รท");
contentPane.add(button4);
button4.addActionListener(this);
button5 = new JButton ("4");
contentPane.add(button5);
button5.addActionListener(this);
button6 = new JButton ("5");
contentPane.add(button6);
button6.addActionListener(this);
button7 = new JButton ("6");
contentPane.add(button7);
button7.addActionListener(this);
button8 = new JButton ("x");
contentPane.add(button8);
button8.addActionListener(this);
button9 = new JButton ("1");
contentPane.add(button9);
button9.addActionListener(this);
button10 = new JButton ("2");
contentPane.add(button10);
button10.addActionListener(this);
button11 = new JButton ("3");
contentPane.add(button11);
button11.addActionListener(this);
button12 = new JButton ("-");
contentPane.add(button12);
button12.addActionListener(this);
button13 = new JButton ("0");
contentPane.add(button13);
button13.addActionListener(this);
button14 = new JButton (".");
contentPane.add(button14);
button14.addActionListener(this);
button15 = new JButton ("=");
contentPane.add(button15);
button15.addActionListener(this);
button16 = new JButton ("+");
contentPane.add(button16);
button16.addActionListener(this);
}
public static void main(String[] args)
{
Calculator guiWindow = new Calculator(); //uses GUI
guiWindow.setVisible(true); //makes it visible
}
public void actionPerformed(ActionEvent e)
{
Container contentPane = getContentPane();
if (e.getActionCommand().equals("7"));
{
text.setText("7");
}
}
}
It looks like you shadowed your member field JTextArea text inside the constructor. So that means you didn't assign an instance of JTextArea to the member field but to the local variable in the constructor.
Use instead of this
JTextArea text = new JTextArea(1, 25);
this
text = new JTextArea(1, 25);

setText method with panel and button

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.
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.
You have never added ActionListener to that button, just to three color buttons and a JTextField polje
I think you didn't added this as the actionListener of dugmeBroja0:
you need this:
dugmeBroja0.addActionListener(this);

Categories

Resources