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);
Related
these are four buttons, i wanna just select one or two button!
how do i do that?
public class Game {
private JButton button1,button2,button3,button4;
public Game(){
buttonMethod();
private void buttonMethod() {
JButton button1 = new JButton("?");
button1.setFont(new Font("Tahoma",Font.BOLD,20));
button1.setBackground(Color.yellow);
panel2.add(button1);
JButton button2 = new JButton("?");
button2.setFont(new Font("Tahoma",Font.BOLD,20));
button2.setBackground(Color.yellow);
button2.addActionListener(e->{
button2.setText("2");
});
panel2.add(button2);
JButton button3 = new JButton("?");
button3.setFont(new Font("Tahoma",Font.BOLD,20));
button3.setBackground(Color.yellow);
button3.addActionListener(e->{
button3.setText("3");
});
panel2.add(button3);
JButton button4 = new JButton("?");
button4.setFont(new Font("Tahoma",Font.BOLD,20));
button4.setBackground(Color.yellow);
button4.addActionListener(e->{
button4.setText("4");
});
panel2.add(button4);
}
}
}
I have found out about the action event function recently, and I am really confused on how to make it so when I press 2 numbers and an operation it'll add/subtract/multiply/divide them.
Here is the menu that doesn't do anything yet:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
#SuppressWarnings("serial")
public class Window extends JFrame implements ActionListener {
#SuppressWarnings("unused")
public static void main (String[] args) {
Window em = new Window();
}
public Window() {
setTitle("testing");
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,6,3,3));
setResizable(false);
JButton button1= new JButton("7");
JButton button2= new JButton("8");
JButton button3= new JButton("9");
JButton button10= new JButton("+");
JButton button4= new JButton("4");
JButton button5= new JButton("5");
JButton button6= new JButton("6");
JButton button11= new JButton("-");
JButton button7= new JButton("1");
JButton button8= new JButton("2");
JButton button9= new JButton("3");
JButton button12= new JButton("*");
JButton button0= new JButton("0");
JButton buttonR= new JButton("Reset");
JButton buttonE= new JButton("Enter");
JButton button13= new JButton("÷");
JTextField x = new JTextField();
JTextField y = new JTextField();
JTextField CPP_entry = new JTextField();
CPP_entry.setEditable(false);
add(button1);
add(button2);
add(button3);
add(button10);
add(new JLabel(" x:"));
add(x);
add(button4);
add(button5);
add(button6);
add(button11);
add(new JLabel(" y:"));
add(y);
add(button7);
add(button8);
add(button9);
add(button12);
add(new JLabel(" x/y:"));
add(CPP_entry);
add(buttonR);
add(button0);
add(buttonE);
add(button13);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton currentButton = (JButton)e.getSource();
}
}
You may use the following piece of code as example to get the sum of the two numbers:
public double first;
public double sum;
private void method() {
JTextField field = new JTextField();
JButton plus = new JButton("+");
plus.addActionListener(e -> {
first = Double.parseDouble(field.getText());
});
JButton result = new JButton("=");
result.addActionListener(e -> {
sum = first + Double.parseDouble(field.getText());
});
}
For a school assignment I need to have 2 panels.
The right needs to be 3x3 with buttons (which I have made black for easy identification when setting up the GUI) and the left with 1 label and 4 buttons.
Label should display the name of the current picture (placed randomly on a button in the 3x3 grid), 3 buttons to place images randomly, and one button to clear them off. I don't need help with the logic, I can do that part.
I am having trouble setting up the panel so it looks somewhat decent. I was thinking of making it a 1x5 grid but I don't know how to do that. I have spent multiple hours looking up how to do it as well as trying out my own stuff (notice the commented out stuff). Any help would be greatly appreciated.
public class Characters extends JFrame {
private Container pane;
private JButton Button1, Button2, Button3, Button4, Button5, Button6;
private JButton Button7, Button8, Button9;
private JButton BMolly, BOctavious, BJimmy, BClear;
private ImageIcon Molly, Octavious, Jimmy;
private JLabel LName;
public Characters() {
setTitle("Characters");
pane = getContentPane();
pane.setLayout(new GridLayout(3, 3));
Button1 = new JButton((Icon) Button1);
Button1.setBackground(Color.BLACK);
pane.add(Button1);
Button2 = new JButton((Icon) Button2);
Button2.setBackground(Color.BLACK);
pane.add(Button2);
Button3 = new JButton((Icon) Button3);
Button3.setBackground(Color.BLACK);
pane.add(Button3);
Button4 = new JButton((Icon) Button4);
Button4.setBackground(Color.BLACK);
pane.add(Button4);
Button5 = new JButton((Icon) Button5);
Button5.setBackground(Color.BLACK);
pane.add(Button5);
Button6 = new JButton((Icon) Button6);
Button6.setBackground(Color.BLACK);
pane.add(Button6);
Button7 = new JButton((Icon) Button7);
Button7.setBackground(Color.BLACK);
pane.add(Button7);
Button8 = new JButton((Icon) Button8);
Button8.setBackground(Color.BLACK);
pane.add(Button8);
Button9 = new JButton((Icon) Button9);
Button9.setBackground(Color.BLACK);
pane.add(Button9);
LName = new JLabel(" ");
pane.add(LName);
BMolly = new JButton("Molly");
pane.add(BMolly);
BOctavious = new JButton("Octavious");
pane.add(BOctavious);
BJimmy = new JButton("Jimmy");
pane.add(BJimmy);
BClear = new JButton("Clear");
pane.add(BClear);
pack();
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
new Characters();
}
}
What you need are two different panels.
pane = new JPanel(); //instead of pane = getContentPane();
//set your Layout
//add the 9 buttons
//...
add(pane, BorderLayout.CENTER); //add panel to the jframe
pane = new JPanel(); //creat new panel
//set your Layout
//add the other 4 buttons + label
//...
add(pane, BorderLayout.EAST); //add panel to the jframe
If it still dont work i can add the full code.
Very good starting point for dealing with layout managers is Java documentation. For your need looks like BorderLayout manager should be good choice.
Read how to use layout managers with examples, it gives you first look.
There's a few issues with your code, by convention variable names start with a lower letter, and you should only do one thing per line of code (even declaring variables). Also you shouldn't "extend" a class unless you are going to extend the functionality of it, if all you are going to do is use it, then just create your own instance of the JFrame. (Oh, and when you're posting code on fourms looking for help, if you've felt the need to add an annotation to ignore an unused warning, then you probably don't need to post it in your question either :p )
For your problem you need to consider using multiple layouts (they can even be embedded in each other to provide some very complex effects) - Swing layouts
I've used a borderLayout and a BoxLayout to achieve something along the lines of what you require.
public class Characters {
private JFrame frame;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton mollyButton;
private JButton octaviousButton;
private JButton jimmyButton;
private JButton clearButton;
public Characters() {
frame = new JFrame("Characters");
JPanel rightPanel = new JPanel(new GridLayout(3, 3));
button1 = new JButton();
button1.setBackground(Color.BLACK);
rightPanel.add(button1);
button2 = new JButton();
button2.setBackground(Color.BLACK);
rightPanel.add(button2);
button3 = new JButton();
button3.setBackground(Color.BLACK);
rightPanel.add(button3);
button4 = new JButton();
button4.setBackground(Color.BLACK);
rightPanel.add(button4);
button5 = new JButton();
button5.setBackground(Color.BLACK);
rightPanel.add(button5);
button6 = new JButton();
button6.setBackground(Color.BLACK);
rightPanel.add(button6);
button7 = new JButton();
button7.setBackground(Color.BLACK);
rightPanel.add(button7);
button8 = new JButton();
button8.setBackground(Color.BLACK);
rightPanel.add(button8);
button9 = new JButton();
button9.setBackground(Color.BLACK);
rightPanel.add(button9);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
JLabel nameLabel = new JLabel("Name");
leftPanel.add(nameLabel);
mollyButton = new JButton("Molly");
leftPanel.add(mollyButton);
octaviousButton = new JButton("Octavious");
leftPanel.add(octaviousButton);
jimmyButton = new JButton("Jimmy");
leftPanel.add(jimmyButton);
clearButton = new JButton("Clear");
leftPanel.add(clearButton);
JPanel centrePanel = new JPanel();
centrePanel.add(new JLabel("Stuff goes here"));
JPanel content = new JPanel(new BorderLayout());
content.add(leftPanel, BorderLayout.WEST);
content.add(centrePanel, BorderLayout.CENTER);
content.add(rightPanel, BorderLayout.EAST);
frame.setContentPane(content);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
new Characters();
}
}
I am having a hard time figuring out how to make the clear button stretch across the entire panel. I am also having a hard time finding how to create the portion above the panel that shows the keys that the user clicks on. Please help? Here is what I have so far... I tried to change the size of the clear button but that portion of the code does nothing.
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TelephonePanel extends JPanel
{
public TelephonePanel()
{
JPanel panel = new JPanel ();
panel.setLayout(new BorderLayout());
setLayout (new GridLayout (5, 3));
setPreferredSize (new Dimension(300, 400));
JButton b1 = new JButton ("1");
JButton b2 = new JButton ("2");
JButton b3 = new JButton ("3");
JButton b4 = new JButton ("4");
JButton b5 = new JButton ("5");
JButton b6 = new JButton ("6");
JButton b7 = new JButton ("7");
JButton b8 = new JButton ("8");
JButton b9 = new JButton ("9");
JButton ba = new JButton ("*");
JButton b0 = new JButton ("0");
JButton bp = new JButton ("#");
JButton bclear = new JButton ("Clear");
bclear.setSize(new Dimension(300, 100));
add (b1);
add (b2);
add (b3);
add (b4);
add (b5);
add (b6);
add (b7);
add (b8);
add (b9);
add (ba);
add (b0);
add (bp);
add (bclear);
}
}
Try to run this, see if it is something like what you are looking for.
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TelephonePanel extends JPanel {
String numberString = "";
JTextField jtf = new JTextField();
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton ba = new JButton("*");
JButton b0 = new JButton("0");
JButton bp = new JButton("#");
JButton bclear = new JButton("Clear");
public TelephonePanel() {
JPanel panel1 = new JPanel(new GridLayout(4, 3));
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
panel1.add(b4);
panel1.add(b5);
panel1.add(b6);
panel1.add(b7);
panel1.add(b8);
panel1.add(b9);
panel1.add(ba);
panel1.add(b0);
panel1.add(bp);
ButtonListener listener = new ButtonListener();
b1.addActionListener(listener);
// add listener to all buttons
setLayout(new BorderLayout());
add(panel1, BorderLayout.CENTER);
add(bclear, BorderLayout.SOUTH);
add(jtf, BorderLayout.NORTH);
jtf.setHorizontalAlignment(SwingConstants.RIGHT);
jtf.setPreferredSize(new Dimension(300, 30));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new TelephonePanel());
frame.setSize(300, 400);
frame.setVisible(true);
}
});
}
class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
numberString += "1";
jtf.setText(numberString);
} else if (e.getSource() == b2) {
numberString += "2";
jtf.setText(numberString);
}
// finish all the else ifs
}
}
}
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
}