Swing,JTextAreas are blank - java

Class principal :
import javax.swing.*;
import java.util.Random;
public class Principal extends Guii {
public int combo;
public static Random bulion = new Random();
public static boolean sansa;
public static boolean input;
public int status;
//STATUS 0 = HEADS;
//STATUS 1 = TAILS;
public static void main(String[] args) {
Guii lee = new Guii();
Principal obiect = new Principal();
}
public int flip(){
boolean sansa2 ;
sansa2 = bulion.nextBoolean();
if(sansa2){
status = 0;
display.setText("Heads");
}
else{
status = 1;
display.setText("Tails");
}
return status;
}
public int returnStatus(){
return status;
}
}
Class Guii :
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 JTextArea display = new JTextArea();
public JTextArea comboul = new JTextArea();
private JPanel panel;
public int predictie;
public Guii(){
super("Heads or Tails");
setContentPane(panel);
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;
heads.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e){
dacaHeads();
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){
dacaTails();
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);
}
}
});}
The problem is that the window opens, i see everything but nothing happens when i press the buttons.
I used gui designer from intellij idea.
Thank you.
//Sorry for the second question.Deleted it.

I don't know if it is a mistake of writing your code but your function dacaTails(), which add the listener to the button, seems to be never called. You should put the addActionListener's functions in the constructor method, I think.

Related

JAVA - class and it's object, super

This is the class Gui.
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;
public class Gui extends JFrame{
Principal obiect;
public JButton heads = new JButton("Heads");
public JButton tails = new JButton("Tails");
public static JTextField display;
public JTextField comboul;
public JPanel panel = new JPanel();
public int predictie;
public Gui(){
super("HeadsOrTails");
panel.setLayout(new BorderLayout(100, 100));
panel.add(heads,BorderLayout.LINE_START);
panel.add(tails,BorderLayout.LINE_END);
panel.add(display,BorderLayout.CENTER); //HERE IS ERROR
panel.add(comboul,BorderLayout.PAGE_START);
}
public void dacaHeads(){
if(heads.getModel().isPressed()) predictie = 0;
}
public void dacaTails(){
if(tails.getModel().isPressed()) predictie = 1;
heads.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
dacaHeads();
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);
}
}
});
tails.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
dacaTails();
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);
}
}
});
}}
This is class Principal.
import javax.swing.*;
import java.util.Random;
public class Principal extends Gui {
public int combo;
public static Random bulion = new Random();
public static boolean sansa;
public static boolean input;
public int status;
//STATUS 0 = HEADS;
//STATUS 1 = TAILS;
public static void main(String[] args) {
Gui lee = new Gui(); //HERE IS ERROR
Principal obiect = new Principal();
lee.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lee.setSize(500,500);
lee.setVisible(true);
}
public int flip(){
boolean sansa2 ;
sansa2 = bulion.nextBoolean();
if(sansa2){
status = 0;
display.setText("Heads");
}
else{
status = 1;
display.setText("Tails");
}
return status;
}
public int returnStatus(){
return status;
}
}
These are the errors :
FIXED:
//Exception in thread "main" java.lang.StackOverflowError
//at Gui.(Gui.java:19) // Super
//at Principal.(Principal.java:3) //public class Principal extends Gui
//at Gui.(Gui.java:9) //Principal obiect = new Principal()
NOW:
Exception in thread "main" java.lang.NullPointerException
at Gui.<init>(Gui.java:23)
at Principal.main(Principal.java:14)
It's my first post so i am sorry if you don't understand.I'll post more information if you need.
When you create a Gui instance, it creates a Principal instance (since the Gui class has a Prinicipal instance variable - Principal obiect = new Principal();) which is also a Gui instance, so you get an infinite chain of constructor calls, leading to StackOverflowError.
To avoid it change
Principal obiect = new Principal();
to
Principal obiect;
and initialize this instance after the Gui instance is created (i.e. after the call to Gui lee = new Gui(); in your main method).
For example, add to Gui class :
public void setPrincipal (Principal object)
{
this.object = object;
}
And in your main :
Gui lee = new Gui();
lee.setPrincipal (new Principal());

How can I solve a string version of a calculation for a simple calculator? [duplicate]

This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
This is a simple calculator where the user can type out the calculation and hit enter, and the calculator would determine if it is a valid calculation or not. If it is a valid calculation, the calculation is carried out. If not, an error message is written to the screen.
The calculation is carried out part isn't finished.
Can someone suggest a solutions to the getAnswer() method.
It would be very much appreciated.
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
#SuppressWarnings("serial")
public class Calculator extends JFrame{
private interface CalculatorInterface {
public void writeToScreen(String text);
public void clearScreen();
public String getScreenText();
}
private class CalculatorPanel extends JPanel implements CalculatorInterface {
private class NumberPanel extends JPanel implements CalculatorInterface {
private static final int NUMTOTAL = 10;
private CalculatorPanel calcPanel;
private JButton[] numButtons;
public NumberPanel(CalculatorPanel calcPanel) {
this.calcPanel = calcPanel;
buildLayout();
addButtons();
}
private void buildLayout() {
this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
GridLayout layout = new GridLayout(4,3);
layout.setHgap(1);
layout.setVgap(1);
this.setLayout(new GridLayout(4,3));
}
private void addButtons() {
numButtons = new JButton[NUMTOTAL];
for(int i = numButtons.length -1; i >= 0 ; i--) {
numButtons[i] = new JButton("" + i);
numButtons[i].setPreferredSize(new Dimension(60,40));
numButtons[i].setFont(new Font("Sans serif", Font.PLAIN, 18));
numButtons[i].addActionListener(
new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String text = ((JButton)e.getSource()).getText().trim();
if(getScreenText().equals("Invalid Calc")) {
clearScreen();
writeToScreen(text);
}else {
writeToScreen(text);
}
}
});
this.add(numButtons[i]);
}
}
#Override
public void writeToScreen(String text) {
calcPanel.writeToScreen(text);
}
#Override
public void clearScreen() {
calcPanel.clearScreen();
}
#Override
public String getScreenText() {
return calcPanel.getScreenText();
}
}
private class OperPanel extends JPanel implements CalculatorInterface {
private static final int ADD = 0;
private static final int SUB = 1;
private static final int MULT = 2;
private static final int DIV = 3;
private static final int OPENB = 4;
private static final int CLOSEB = 5;
private static final int CLEAR = 6;
private static final int EQL = 7;
private static final int OPERTOTAL = 8;
private CalculatorPanel calcPanel;
private JButton[] operButtons;
public OperPanel(CalculatorPanel calcPanel) {
this.calcPanel = calcPanel;
buildLayout();
addButtons();
}
private void buildLayout() {
GridLayout layout = new GridLayout(4,1);
layout.setHgap(1);
layout.setVgap(1);
this.setLayout(new GridLayout(4,1));
}
private void addButtons() {
operButtons = new JButton[OPERTOTAL];
operButtons[ADD] = makeButton(ADD, "+");
operButtons[SUB] = makeButton(SUB, "-");
operButtons[MULT] = makeButton(MULT, "*");
operButtons[DIV] = makeButton(DIV, "/");
operButtons[CLEAR] = makeButton(CLEAR, "CL");
operButtons[EQL] = makeButton(EQL, "=");
operButtons[OPENB] = makeButton(OPENB, "(");
operButtons[CLOSEB] = makeButton(CLOSEB, ")");
for(JButton button: operButtons) {
this.add(button);
}
}
private JButton makeButton(int index, String label) {
operButtons[index] = new JButton(label);
operButtons[index].addActionListener(
new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String text = ((JButton)e.getSource()).getText();
if(text.equals("=")) {
String screenText = getScreenText();
clearScreen();
try {
writeToScreen(getAnswer(screenText));
}catch(Exception excep) {
writeToScreen("Invalid Calc");
}
}else if(text.equals("CL")) {
clearScreen();
}else {
writeToScreen(text);
}
}
});
return operButtons[index];
}
private String getAnswer(String text) throws Exception {
/*I'm trying to solve for any input by the user e.g
*(the stuff in square brackets represents what is displayed
* on the screen:.
*[1+1] (hits equals) [2]
*[1+2-3] (hits equals) [0]
*[1+2*3] (hits equals) [7]
*[10*(14+1/2)] (hits equals) [145]
*/
throw new Exception();
}
#Override
public String getScreenText() {
return calcPanel.getScreenText();
}
#Override
public void clearScreen() {
calcPanel.clearScreen();
}
#Override
public void writeToScreen(String text) {
calcPanel.writeToScreen(text);
}
}
private NumberPanel numPanel;
private OperPanel operPanel;
private JTextField calcScreen;
public CalculatorPanel(JTextField calcScreen) {
this.calcScreen = calcScreen;
buildNumPanel();
buildOperPanel();
buildCalcPanel();
}
private void buildNumPanel() {
this.numPanel = new NumberPanel(this);
}
private void buildOperPanel() {
this.operPanel = new OperPanel(this);
}
private void buildCalcPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.add(numPanel);
this.add(operPanel);
}
#Override
public void writeToScreen(String text) {
calcScreen.setText(getScreenText() + text);
}
#Override
public String getScreenText() {
return calcScreen.getText();
}
#Override
public void clearScreen() {
calcScreen.setText("");
}
}
private JPanel mainPanel;
private JTextField calcScreen;
private CalculatorPanel calcPanel;
public Calculator() {
buildScreen();
buildCalcPanel();
buildMainPanel();
buildCalculator();
}
private void buildScreen() {
this.calcScreen = new JTextField();
this.calcScreen.setPreferredSize(new Dimension(150,50));
this.calcScreen.setHorizontalAlignment(JTextField.CENTER);
this.calcScreen.setFont(new Font("Sans serif", Font.PLAIN, 30));
}
private void buildCalcPanel() {
this.calcPanel = new CalculatorPanel(this.calcScreen);
}
private void buildMainPanel() {
this.mainPanel = new JPanel();
this.mainPanel.setBorder(new EmptyBorder(10,10,10,10));
this.mainPanel.setLayout(new BoxLayout(this.mainPanel, BoxLayout.Y_AXIS));
this.mainPanel.add(calcScreen);
this.mainPanel.add(calcPanel);
}
private void buildCalculator() {
this.add(mainPanel);
this.setTitle("Calculator");
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
#SuppressWarnings("unused")
Calculator calc = new Calculator();
}
}
How can I check to see if a string is a valid calculation for a simple calculator?
Edit 1: Fixed a silly bug in the makeButton() method were I passed in the text of the button to be verified instead of the text on screen. (I'm an idiot.)
Edit 2: Removed the isValid(String text) from the code and make it so the getAnswer() method just threw an exception if input is not a valid calculation.
As well mentioned in a previous StackOverflow answer (Evaluating a math expression given in string form), you could use Javascript's ScriptEngine to calculate expressions based on strings that you would retrieve from the Text Field. Place it in a try-catch block first to see if there's an error in the expression. In the catch block, set the variable storing whether its a valid expression or not to false.
boolean validExpression = true;
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String input = textField.getText() // Modify this to whatever variable you have assigned to your text field
try {
System.out.println(engine.eval(foo));
}
catch (ScriptException e) {
validExpression = false;
System.out.println("Invalid Expression");
}
Make sure you include the following imports:
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
Although you could try to implement Shunting-Yard Algorithm or another arithmetic parser, this is simply a way more pragmatic solution.

How to reset Minesweeper game board in Java.Swing

I have been trying to get my code to reset after hitting the okay button with slider, however, I have had no such luck. Does anyone have an idea on how I can incorporate this into my code?
P.S. The reset must be in Class Field.
package GUI;
import java.awt.GridLayout;
import javax.swing.JPanel;
public class GameDisplay extends JPanel {
private static final long serialVersionUID = 1L;
private boolean activated;
public GameDisplay(Model.Field field) {
setLayout(new GridLayout(GUI.SizeDialog.currentRows, GUI.SizeDialog.currentColumns));
for (int i = 0; i < GUI.SizeDialog.currentRows; ++i) {
for (int j = 0; j < GUI.SizeDialog.currentColumns; ++j) {
add(field.Cells[i][j]);
}
}
}
public boolean isActive() {
return activated;
}
public void setActive(boolean activated) {
this.activated = activated;
}
}
Field
package Model;
import java.awt.Dimension;
import java.util.ArrayList;
import GUI.SizeDialog;
public class Field {
private int difficultyFactor;
public static final int EASY = 1;
public static final int NORMAL = 2;
public static final int HARD = 3;
public Cell[][] Cells;
public Field() {
Cells = new Cell[GUI.SizeDialog.currentRows][GUI.SizeDialog.currentColumns];
for (int i = 0; i < GUI.SizeDialog.currentRows; ++i) {
for (int j = 0; j < GUI.SizeDialog.currentColumns; ++j) {
Cells[i][j]= new Cell();
}
}
}
public void reset(){
Cells = new Cell[GUI.SizeDialog.currentRows][GUI.SizeDialog.currentColumns];
for (int i = 0; i < GUI.SizeDialog.currentRows; ++i) {
for (int j = 0; j < GUI.SizeDialog.currentColumns; ++j) {
Cells[i][j]= new Cell();
}
}
}
public void setDifficultyFactor(int level) {
switch (level) {
case EASY:
difficultyFactor = EASY;
break;
case NORMAL:
difficultyFactor = NORMAL;
break;
case HARD:
difficultyFactor = HARD;
break;
default:
difficultyFactor = NORMAL;
}
}
}
SizeDialog
package GUI;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import Model.Field;
public class SizeDialog extends JDialog {
private static final long serialVersionUID = 1L;
private Model.Field field;
private GUI.GameDisplay GameDisplay;
public static Integer currentRows = 4;
public static Integer currentColumns = 4;
public SizeDialog() {
JPanel dialogBoxPanel = new JPanel();
BorderLayout dialogLayout = new BorderLayout();
dialogBoxPanel.setLayout(dialogLayout);
JSlider rowCount = new JSlider(4, 10, 4);
JSlider columnCount = new JSlider(4, 10, 4);
Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
labelTable.put(new Integer(4), new JLabel("4"));
labelTable.put(new Integer(10), new JLabel("10"));
rowCount.setLabelTable(labelTable);
rowCount.setPaintLabels(true);
columnCount.setLabelTable(labelTable);
columnCount.setPaintLabels(true);
JLabel row = new JLabel();
JLabel column = new JLabel();
JPanel rowPanel = new JPanel();
JPanel columnPanel = new JPanel();
JLabel rowPosition = new JLabel();
rowPosition.setText("Rows: 4");
JLabel columnPosition = new JLabel();
columnPosition.setText("Columns: 4");
rowCount.addChangeListener(getCurrentRows(rowCount, "row", rowPosition));
columnCount.addChangeListener(getCurrentColumns(columnCount, "column", columnPosition));
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
field.reset();
SizeDialog.this.dispose();
}
});
rowPanel.add(rowPosition, BorderLayout.WEST);
rowPanel.add(row, BorderLayout.CENTER);
rowPanel.add(rowCount, BorderLayout.EAST);
columnPanel.add(columnPosition, BorderLayout.WEST);
columnPanel.add(column, BorderLayout.CENTER);
columnPanel.add(columnCount, BorderLayout.EAST);
dialogBoxPanel.add(rowPanel, BorderLayout.NORTH);
dialogBoxPanel.add(columnPanel, BorderLayout.CENTER);
dialogBoxPanel.add(okButton, BorderLayout.SOUTH);
setContentPane(dialogBoxPanel);
setMinimumSize(new Dimension(300, 150));
setLocationRelativeTo(null);
}
private ChangeListener getCurrentColumns(final JSlider columnCount, final String string, final JLabel columnPosition) {
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JSlider theSlider = (JSlider) changeEvent.getSource();
if (!theSlider.getValueIsAdjusting()) {
int sliderValue = theSlider.getValue();
columnPosition.setText("columns:" + sliderValue);
SizeDialog.currentColumns = sliderValue;
}
}
};
return changeListener;
}
private ChangeListener getCurrentRows(final JSlider rowCount, final String string, final JLabel rowPosition) {
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JSlider theSlider = (JSlider) changeEvent.getSource();
if (!theSlider.getValueIsAdjusting()) {
int sliderValue = theSlider.getValue();
rowPosition.setText("Rows:" + sliderValue);
SizeDialog.currentRows = sliderValue;
}
}
};
return changeListener;
}
}
MainFrame
package GUI;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static JPanel centerPanel;
public static final String EASY = "Easy";
public static final String NORMAL = "Normal";
public static final String HARD = "Hard";
public MainFrame(JPanel centerPanel) {
// Setup of Main
super("The True/False Game");
this.centerPanel = centerPanel;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setup();
}
private void setup() {
// Setup of centerPanel
add(centerPanel);
pack();
}
public JMenuBar setUpMenuBar(ActionListener listener) {
// create the necessary menu items and menus
JMenuBar menuBar = new JMenuBar();
JMenu settingsMenu = new JMenu("Settings");
JMenuItem size = new JMenuItem("Size");
size.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object valueBox = new SizeDialog();
((Window) valueBox).setVisible(true);
}
});
JMenu difficultyMenu = new JMenu("Difficulty Level");
ButtonGroup levelGroup = new ButtonGroup();
JCheckBoxMenuItem easyDifficultyItem = new JCheckBoxMenuItem(EASY);
JCheckBoxMenuItem normalDifficultyItem = new JCheckBoxMenuItem(NORMAL);
JCheckBoxMenuItem hardDifficultyItem = new JCheckBoxMenuItem(HARD);
easyDifficultyItem.setSelected(true); // Default difficulty setting
// this button group ensures that only one check box is selected
levelGroup.add(easyDifficultyItem);
levelGroup.add(normalDifficultyItem);
levelGroup.add(hardDifficultyItem);
difficultyMenu.add(easyDifficultyItem);
difficultyMenu.add(normalDifficultyItem);
difficultyMenu.add(hardDifficultyItem);
settingsMenu.add(size);
settingsMenu.add(difficultyMenu);
menuBar.add(settingsMenu);
// use a single listener to handle all menu item selections
for (int i = 0; i < menuBar.getMenuCount(); ++i) {
for (JMenuItem item : getMenuItems(menuBar.getMenu(i))) {
item.addActionListener(listener);
}
}
return menuBar;
}
// this recursion works because JMenu is a subclass of JMenuItem!
private static List<JMenuItem> getMenuItems(JMenuItem item) {
List<JMenuItem> items = new ArrayList<>();
if (item instanceof JMenu) {
JMenu menu = (JMenu) item;
for (int i = 0; i < menu.getItemCount(); ++i) {
items.addAll(getMenuItems(menu.getItem(i)));
}
} else {
items.add(item);
}
return items;
}
}
App
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuBar;
import Model.Cell;
import Model.Field;
import GUI.MainFrame;
import GUI.SizeDialog;
public class App {
// Main Objects
private static GUI.MainFrame MainFrame;
private static GUI.GameDisplay GameDisplay;
private static Model.Field field;
public static GUI.SizeDialog SizeDialog;
public static void main(String[] args) {
// Setup of the Main Objects
field = new Model.Field();
GameDisplay = new GUI.GameDisplay(field);
MainFrame = new MainFrame(GameDisplay);
MainFrame.setMinimumSize(new Dimension(500, 500));
MainFrame.getContentPane().add(GameDisplay);
// Setup of the Menu Bar
JMenuBar menuBar = MainFrame.setUpMenuBar(menuListener);
MainFrame.setJMenuBar(menuBar);
// Show the Mainframe in the center of the desktop screen
MainFrame.setLocationRelativeTo(null);
MainFrame.setVisible(true);
}
// listener for the menu items in MainFrame
private static ActionListener menuListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case GUI.MainFrame.EASY:
field.setDifficultyFactor(Field.EASY);
GameDisplay.setActive(true);
break;
case GUI.MainFrame.NORMAL:
field.setDifficultyFactor(Field.NORMAL);
GameDisplay.setActive(true);
break;
case GUI.MainFrame.HARD:
field.setDifficultyFactor(Field.HARD);
GameDisplay.setActive(true);
break;
}
}
};
};
An instance of your Field class is currently being created in the App class. Your private field field in the SizeDialog class is currently not being set to the instance known to the App class. A solution for this problem is to provide the Field instance as a parameter.
A possible solution is as follows:
App:
public class App {
// Main Objects
private static GUI.MainFrame MainFrame;
private static GUI.GameDisplay GameDisplay;
private static Model.Field field;
public static GUI.SizeDialog SizeDialog;
public static void main(String[] args) {
// Setup of the Main Objects
field = new Model.Field();
GameDisplay = new GUI.GameDisplay(field);
MainFrame = new MainFrame(GameDisplay, field);
/* the rest of your code */
}
/* more App code */
}
MainFrame:
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static JPanel centerPanel;
public static final String EASY = "Easy";
public static final String NORMAL = "Normal";
public static final String HARD = "Hard";
private Model.Field field;
public MainFrame(JPanel centerPanel, Model.Field field) {
this.field = field;
/* the rest of your code */
}
public JMenuBar setUpMenuBar(ActionListener listener) {
// create the necessary menu items and menus
JMenuBar menuBar = new JMenuBar();
JMenu settingsMenu = new JMenu("Settings");
JMenuItem size = new JMenuItem("Size");
size.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object valueBox = new SizeDialog(field);
((Window) valueBox).setVisible(true);
}
});
/* the rest of your code */
}
/* more MainFrame code */
}
SizeDialog:
public class SizeDialog extends JDialog {
private static final long serialVersionUID = 1L;
private final Model.Field field;
private GUI.GameDisplay GameDisplay;
public static Integer currentRows = 4;
public static Integer currentColumns = 4;
public SizeDialog(final Model.Field field) {
this.field = field;
/* the rest of your code */
}
/* more SizeDialog code */
}
EDIT: As mentioned in this answer, objects that are referenced in anonymous classes must be final.

Java keeps adding buttons! - JFrames - [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Alright, so, I am trying to make a game using a JFrame that, when you click a button, adds money, and show update some text(a JLabel).
You see, it should just update the Windows.Money variable and display ONLY the new variable on the screen, but, it adds more buttons. Please note: The money part works fine.
It's just that Java doesn't want to replace, only add.
Code:
package dev.bobdabiulder.classic.main;
import javax.swing.JLabel;
public class Bucket extends Window{
private static final long serialVersionUID = 1L;
public Bucket() throws InterruptedException {
for(int i = 0; !Window.Paused; ) {
Window.Money += 2;
this.wait(1000);
}
Window.Buckets++;
Window.BucketCounter = new JLabel("You have: " + Window.Buckets + " buckets!");
}
}
In the Windows class...
package dev.bobdabiulder.classic.main;
import java.awt.Button;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Window extends JFrame{
public static long Money = 0;
private static final long serialVersionUID = 1L;
private static Thread t1 = new Thread();
private static Thread t2 = new Thread();
private static Thread t3 = new Thread();
private static Thread t4 = new Thread();
private static Thread t5 = new Thread();
public JButton bucket = new JButton("Buy a Bucket!");
public JButton add$ = new JButton("Add Money!");
public JLabel money = new JLabel(Money + "");
public static long Buckets = 0;
public static JLabel BucketCounter = new JLabel("You have: " + Buckets + " buckets!");
public static JPanel buck = new JPanel();
public static boolean Paused = false;
static JFrame jf = new JFrame("Lol!");
//Window Method
public Window() {
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buck.add(bucket);
buck.add(money);
buck.add(add$);
buck.add(BucketCounter);
jf.setSize(500, 500);
jf.add(buck);
bucket.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(bucket)) {
try {
new Bucket();
} catch (Exception e1) {
e1.printStackTrace();
}
}
System.out.println("Action Performed!");
}
});
pack();
}
//End of ActionPerformed
//Start of start()
public static void start() {
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
//
//
//
#SuppressWarnings("deprecation")
public static void stop() {
t1.stop();
t2.stop();
t3.stop();
t4.stop();
t5.stop();
}
}
I made some requested edits, and I get errors where I put *, the errors all read:
Cannot make a static reference to the non-static field Window.(anything),
Error Spots:
for(int i = 0; !*Window.Paused; ) {
*Window.Money += 2;
this.wait(1000);
}
*Window.Buckets++;
*Window.BucketCounter = new JLabel("You have: " + *Window.Buckets + " buckets!");
Brief example of MVC, well really Model-View. What this does is uses a Swing Timer and not Threads (not directly that is) to increment a JLabel held in a different class. It also uses PropertyChangeListener and support to notify the view (the GUI) of changes in the state of the model.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;
/**
* http://stackoverflow.com/q/22620807/522444
* http://stackoverflow.com/a/22621767/522444
* #author Pete
*
*/
#SuppressWarnings("serial")
public class ShortMvc extends JPanel {
private JTextField moneyField = new JTextField(10);
private JTextField bucketField = new JTextField(10);
private MoneyModel model = new MoneyModel();
private Timer timer = new Timer(model.getTimerDelay(), new TimerListener());
private JButton moneyButton = new JButton("Add Money");
private JButton bucketButton = new JButton("Add Bucket");
public ShortMvc() {
moneyField.setEditable(false);
moneyField.setFocusable(false);
bucketField.setEditable(false);
bucketField.setFocusable(false);
bucketField.setText(String.valueOf(model.getBuckets()));
add(new JLabel("Money:"));
add(moneyField);
add(moneyButton);
add(new JLabel("Buckets:"));
add(bucketField);
add(bucketButton);
moneyButton.getModel().addChangeListener(new MoneyBtnModelListener());
moneyButton.setMnemonic(KeyEvent.VK_M);
bucketButton.addActionListener(new BucketButtonListener());
bucketButton.setMnemonic(KeyEvent.VK_B);
model.addPropertyChangeListener(new ModelListener());
timer.setInitialDelay(0);
}
private class BucketButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
model.incrementBuckets();
}
}
private class MoneyBtnModelListener implements ChangeListener {
private boolean pressed = false;
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (pressed == model.isPressed()) {
return;
}
pressed = model.isPressed();
if (pressed) {
timer.start();
} else {
timer.stop();
}
}
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
model.incrementMoney(model.getMoneyIncrementAmount());
}
}
private class ModelListener implements PropertyChangeListener {
private NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
public ModelListener() {
moneyField.setText(moneyFormat.format(model.getMoney()));
}
#Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (MoneyModel.MONEY.equals(pcEvt.getPropertyName())) {
moneyField.setText(moneyFormat.format(model.getMoney()));
}
else if (MoneyModel.BUCKETS.equals(pcEvt.getPropertyName())) {
int buckets = model.getBuckets();
bucketField.setText(String.valueOf(buckets));
timer.setDelay(model.getTimerDelay());
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Short Mvc");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ShortMvc());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MoneyModel {
public static final String MONEY = "money";
public static final String BUCKETS = "buckets";
private static final int INIT_TIMER_DELAY = 500;
public static final long MONEY_INCREMENT_AMOUNT = 2L;
private long money = 0L;
private int buckets = 1;
private int timerDelay = INIT_TIMER_DELAY;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
this);
public void setMoney(long money) {
long oldValue = this.money;
long newValue = money;
this.money = money;
pcSupport.firePropertyChange(MONEY, oldValue, newValue);
}
public long getMoneyIncrementAmount() {
return MONEY_INCREMENT_AMOUNT;
}
public void incrementMoney(long addToMoney) {
long oldValue = this.money;
long newValue = money + addToMoney;
this.money = newValue;
pcSupport.firePropertyChange(MONEY, oldValue, newValue);
}
public long getMoney() {
return money;
}
public void setBuckets(int buckets) {
int oldValue = this.buckets;
int newValue = buckets;
this.buckets = newValue;
timerDelay = INIT_TIMER_DELAY / buckets;
pcSupport.firePropertyChange(BUCKETS, oldValue, newValue);
}
public void incrementBuckets(int incrementAmount) {
int newValue = this.buckets + incrementAmount;
setBuckets(newValue);
}
// increment by one
public void incrementBuckets() {
incrementBuckets(1);
}
public int getBuckets() {
return buckets;
}
public int getTimerDelay() {
return timerDelay;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
}
Note that I can't use an ActionListener for this to work since an ActionListener only gets activated when the button is released. I'm assuming that you want to accumulate money when the button is pressed, and then stop accumulating when it is released. To do this, you must extract the JButton's model, add a ChangeListener to it, and then react to changes to the model's isPressed() method. I use it to start and stop the Swing Timer that increments the model.
Edit
Next iteration with better MVC (model-view-control) separation:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;
import javax.swing.text.JTextComponent;
/**
* http://stackoverflow.com/q/22620807/522444
* http://stackoverflow.com/a/22621767/522444
* #author Pete
*
*/
public class ShortMvc {
private static void createAndShowGui() {
ShortView view = new ShortView();
MoneyModel model = new MoneyModel();
ShortControl control = new ShortControl(model, view);
control.init();
JFrame frame = new JFrame("Short MVC");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(view.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ShortView {
private JTextField moneyField = new JTextField(10);
private JTextField bucketField = new JTextField(10);
private JButton moneyButton = new JButton();
private JButton bucketButton = new JButton();
private JPanel mainPanel = new JPanel();
public ShortView() {
moneyField.setEditable(false);
moneyField.setFocusable(false);
bucketField.setEditable(false);
bucketField.setFocusable(false);
mainPanel.add(new JLabel("Money:"));
mainPanel.add(moneyField);
mainPanel.add(moneyButton);
mainPanel.add(new JLabel("Buckets:"));
mainPanel.add(bucketField);
mainPanel.add(bucketButton);
}
public JComponent getMainPanel() {
return mainPanel;
}
public JTextComponent getMoneyField() {
return moneyField;
}
public JTextComponent getBucketField() {
return bucketField;
}
public AbstractButton getMoneyButton() {
return moneyButton;
}
public AbstractButton getBucketButton() {
return bucketButton;
}
}
#SuppressWarnings("serial")
class ShortControl {
private MoneyModel model;
private ShortView view;
private Timer timer;
private MoneyBtnAction moneyBtnAction = new MoneyBtnAction("Add Money", KeyEvent.VK_M);
private BucketButtonAction bucketAction = new BucketButtonAction("Add Buckets", KeyEvent.VK_B);
public ShortControl(MoneyModel model, ShortView view) {
this.model = model;
this.view = view;
timer = new Timer(model.getTimerDelay(), new TimerListener());
}
public void init() {
timer.setInitialDelay(0);
view.getBucketField().setText(String.valueOf(model.getBuckets()));
view.getMoneyButton().setAction(moneyBtnAction);
view.getMoneyButton().getModel().addChangeListener(moneyBtnAction);
view.getBucketButton().setAction(bucketAction);
model.addPropertyChangeListener(new ModelListener());
}
private class BucketButtonAction extends AbstractAction {
public BucketButtonAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
model.incrementBuckets();
}
}
private class MoneyBtnAction extends AbstractAction implements ChangeListener {
private boolean pressed = false;
public MoneyBtnAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
// empty
}
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (pressed == model.isPressed()) {
return;
}
pressed = model.isPressed();
if (pressed) {
timer.start();
} else {
timer.stop();
}
}
}
private class ModelListener implements PropertyChangeListener {
private NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
public ModelListener() {
view.getMoneyField().setText(moneyFormat.format(model.getMoney()));
}
#Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (MoneyModel.MONEY.equals(pcEvt.getPropertyName())) {
view.getMoneyField().setText(moneyFormat.format(model.getMoney()));
}
else if (MoneyModel.BUCKETS.equals(pcEvt.getPropertyName())) {
int buckets = model.getBuckets();
view.getBucketField().setText(String.valueOf(buckets));
timer.setDelay(model.getTimerDelay());
}
}
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
model.incrementMoney(model.getMoneyIncrementAmount());
}
}
}
class MoneyModel {
public static final String MONEY = "money";
public static final String BUCKETS = "buckets";
private static final int INIT_TIMER_DELAY = 500;
public static final long MONEY_INCREMENT_AMOUNT = 2L;
private long money = 0L;
private int buckets = 1;
private int timerDelay = INIT_TIMER_DELAY;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
this);
public void setMoney(long money) {
long oldValue = this.money;
long newValue = money;
this.money = money;
pcSupport.firePropertyChange(MONEY, oldValue, newValue);
}
public long getMoneyIncrementAmount() {
return MONEY_INCREMENT_AMOUNT;
}
public void incrementMoney(long addToMoney) {
long oldValue = this.money;
long newValue = money + addToMoney;
this.money = newValue;
pcSupport.firePropertyChange(MONEY, oldValue, newValue);
}
public long getMoney() {
return money;
}
public void setBuckets(int buckets) {
int oldValue = this.buckets;
int newValue = buckets;
this.buckets = newValue;
timerDelay = INIT_TIMER_DELAY / buckets;
pcSupport.firePropertyChange(BUCKETS, oldValue, newValue);
}
public void incrementBuckets(int incrementAmount) {
int newValue = this.buckets + incrementAmount;
setBuckets(newValue);
}
// increment by one
public void incrementBuckets() {
incrementBuckets(1);
}
public int getBuckets() {
return buckets;
}
public int getTimerDelay() {
return timerDelay;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
}
Ok, this should help a bit... missing Timer but you can get an idea of how to manage ActionListeners:
Main class
public class WindowInit{
public static void main(String args[]){
WindowHelp wh = new WindowHelp();
}
}
WindowHelp (This one does the magic)
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.event.*;
public class WindowHelp{
JButton b1;
JButton b2;
JLabel label;
long money;
long buckets;
JFrame jf;
Timer timer;
public WindowHelp(){
money = 0;
buckets = 0;
b1 = new JButton("Add buckets");
b2 = new JButton("Add money");
label = new JLabel("");
jf = new JFrame("My Game");
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
buckets += 2;
label.setText("Buckets: " + buckets);
}
});
b1.setBounds(50,50,100,30);
b2.setBounds(200,50,100,30);
label.setBounds(300,50,200,30);
jf.add(b1);
jf.add(b2);
jf.add(label);
jf.setSize(500,600);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Yes, one is the main class you compile "WindowInit" and run that one.
For timers, gimme some time while I implement it.

why JRadioButton is not selected even by using setSelected(true)

i only want one of the above button to be selected by default
but setSelected(true) is not working .
when i run the below program none of the JRadoiButton is selected
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RadioDemo implements ActionListener {
String buttonName;
JPanel radioPanel=new JPanel();
ButtonGroup group = new ButtonGroup();
Enumeration enl;
int result;
ActionEvent e;
JRadioButton birdButton[];
int i;
Vector<JComponent> list;
Vector<String> listName;
public RadioDemo(Vector<JComponent> list,Vector<String> listName,Enumeration en,Enumeration enl)
{
birdButton=new JRadioButton[list.size()];
this.enl=enl;
this.list=list;
this.listName=listName;
for(i=0;i<list.size()-1;i++)
{
buttonName=(String)enl.nextElement();
birdButton[i] = new JRadioButton(buttonName);
birdButton[i].setSelected(false);
birdButton[i].setActionCommand(buttonName);
group.add(birdButton[i]);
birdButton[i].addActionListener(this);
radioPanel.add(birdButton[i]);
}
buttonName=(String)enl.nextElement();
birdButton[i] = new JRadioButton(buttonName);
birdButton[i].setSelected(true);
birdButton[i].setActionCommand(buttonName);
group.add(birdButton[i]);
birdButton[i].addActionListener(this);
radioPanel.add(birdButton[i]);
radioPanel.setLayout(new BoxLayout(radioPanel,BoxLayout.Y_AXIS));
//birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
result = JOptionPane.showConfirmDialog(null, radioPanel,
"Please choose", JOptionPane.OK_CANCEL_OPTION);
show();
}
/** Listens to the radio buttons. */
public void actionPerformed(ActionEvent e)
{
this.e=e;
}
public void show()
{
if (result == JOptionPane.OK_OPTION)
{ i=0;
while(!birdButton[i].isSelected())
{
i++;
System.out.println(i);
}
//list.removeElementAt(i);
//listName.removeElementAt(i);
System.out.println(i);
System.out.println(e.getActionCommand());
}
}
i also try birdButton[0].setSelected(true);
out of loop
You haven't posted how you call your constructor, so maybe there is something there. I slightly modified your code, added a main method and it seems to work ok. Take a look at it:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
public class RadioDemo implements ActionListener {
String buttonName;
JPanel radioPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
int result;
JRadioButton birdButton[];
Vector<String> listName;
private JRadioButton selectedButton;
public RadioDemo(Vector<String> listName) {
birdButton = new JRadioButton[listName.size()];
this.listName = listName;
int i = 0;
for (String buttonName : listName) {
birdButton[i] = new JRadioButton(buttonName);
if (i == 0) {
birdButton[i].setSelected(true);
selectedButton = birdButton[i];
}
birdButton[i].setActionCommand(buttonName);
group.add(birdButton[i]);
birdButton[i].addActionListener(this);
radioPanel.add(birdButton[i]);
i++;
}
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
// birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
result = JOptionPane.showConfirmDialog(null, radioPanel, "Please choose", JOptionPane.OK_CANCEL_OPTION);
show();
}
/** Listens to the radio buttons. */
#Override
public void actionPerformed(ActionEvent e) {
JRadioButton rb = (JRadioButton) e.getSource();
System.err.println(rb.getText() + " is selected");
selectedButton = rb;
}
public void show() {
if (result == JOptionPane.OK_OPTION) {
System.err.println(selectedButton.getText() + " is selected and approved");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Vector<String> buttonNames = new Vector<String>();
buttonNames.add("Show");
buttonNames.add("Something");
buttonNames.add("Else");
buttonNames.add("Beep");
new RadioDemo(buttonNames);
}
});
}
}

Categories