I have a question I have a program where I want to test the users ability to remember a random list of colors. Based off if the users input is right or wrong it will ask for the next color.
So I got it all work up to where the user inputs the first color. Before it has the user input on the first color. The program is already assuming the user input is wrong, even tho it hasn't asked for any input.
I know from previously knowledge I could like flush the buffer, can you do that with JOptionPane?
Or is this another issue I'm not seeing?
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Testing
{
//Intialization of the whole program for everything to pass information
public JFrame main;
public JLabel lbInsturctions;
public JLabel welcomeMessage;
public JLabel homeScreen;
public JLabel homeScreenColors;
public JTextField txtInput;
public int num = 1;
public String colorList = "";
public String[] color = {"red","green","blue","brown","yellow", "gold", "orange", "silver"};
public String[] solution = new String[5];
//sets up the window and random colors
public Testing ()
{
Random r = new Random();
for (int i = 0; i<solution.length; i++)
{
solution[i] = color[r.nextInt(7)];
colorList = Arrays.toString(solution);
}
JOptionPane.showMessageDialog(null, "Lets test your memory. Memorize these colors: " + colorList);
main = new JFrame ();
main.setSize (500,300);
main.setTitle ("Ultimate Colors");
main.setDefaultCloseOperation(main.EXIT_ON_CLOSE);
main.setLayout(new FlowLayout());
intializeGame();
main.setVisible(true);
}
public void intializeGame ()
{
//All Intiazations
lbInsturctions = new JLabel();
homeScreen = new JLabel();
txtInput= new JTextField(null, 15);
//Need to delete or make new window if user pushes ok then
lbInsturctions.setText("Enter color number " + num + ":");
main.add(lbInsturctions);
main.add(txtInput);
txtInput.addActionListener(new colorTester());
}
public class colorTester implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//Need to delete or make new window if user pushes ok then
lbInsturctions.setText("Enter color number " + num + ":");
//grabs the users input to see if it is corect
String guess= "";
guess = txtInput.getText();
System.out.println(guess);
//Checks to see if the users input is the same as the initalizaton
if (color[num+1].equalsIgnoreCase(guess) || num > 6)
{
System.out.println("You got it!");
++num;
lbInsturctions.setText("Enter color number " + num + ":");
txtInput.setText("");
}
//if the User input is wrong
else
{
System.out.println("It's a good thing your not graded!");
txtInput.setVisible(false);
lbInsturctions.setText("It's a good thing this is not graded!");
}
if (num == 5)
{
lbInsturctions.setText("You memory is perfect good job!");
txtInput.setVisible(false);
}
}
}
}//end of program
This has nothing to do with flushing buffers.
You're getting user input here: guess = txtInput.getText(); which is in the intializeGame method. Meaning you're getting the text from the txtInput JTextField on its creation, before the user has had a chance to enter anything into the field. I think that you're used to programming linear console programs, where you get the user's input immediately, but that's not how event-driven GUI's work. Instead you must get and react to the user's input on event, here perhaps the ActionListener of some button. Perhaps your code needs a "submit" JButton or something similar, and in its ActionListener, extract the input from the JTextField and respond to it. Do this and your code has a better chance of working.
Other issues:
you don't ever appear to have added your txtInput JTextField into the GUI.
same for the homeScreen JLabel
Edit your new code posted at the bottom of your question has the same problem.
Related
I've been looking for the past hour, but I haven't been able to find the solution I am looking for.
I'm wanting to take multiple inputs from the user using JOptionPane, but I don't want them to all be in one dialog window. I'm wanting it to transition to the next or just make the next one pop up. Is there a way to do that using JOptionPane?
Here's what I have so far:
import java.util.Scanner;
import javax.swing.*;
public class HomeWork2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
int days, assignments;
double temperature;
boolean isRaining;
JOptionPane.showInputDialog("How many days are left?");
days = input.nextInt();
JOptionPane.showInputDialog("How many assignments are due?");
assignments = input2.nextInt();
JOptionPane.showInputDialog("What is the temperature outside?");
temperature = input3.nextDouble();
JOptionPane.showInputDialog("Is it raining today?");
isRaining = input4.nextBoolean();
if(assignments<=0)
JOptionPane.showMessageDialog(null, "Why are you asking in the first place?");
else
if(days<5)
JOptionPane.showMessageDialog(null, "You need to hurry up, time is short.");
else
if(assignments>4)
JOptionPane.showMessageDialog(null, "You need to hurry up before the assignments pile up. Oh wait...");
else
if(temperature<50)
JOptionPane.showMessageDialog(null, "You should start working, it's not like it's warm eoungh to do anything.");
else
if(isRaining==true)
JOptionPane.showMessageDialog(null, "It's raining, you might as well start on your assignments.");
else
JOptionPane.showMessageDialog(null, "It's nice out and you have some time to spare, go have fun.");
input.close();
input2.close();
input3.close();
input4.close();
}
}
Apart from my above recommendations, here are some others that will be needed to understand the below code (PLEASE READ THEM ALL BEFORE GOING FOR THE CODE PART ONLY)
Read what a layout manager is and how they work, especially take a look at Grid Layout and Box Layout, Google for examples and explanations if you don't understand the tutorial.
Read what methods are and how they work.
Read about the Event Dispatch Thread (EDT) and its function.
Be careful to not mix console application paradigm and GUI application paradigm. Use one or the other.
Learn How to use Dialogs
Read how to convert a String o a int and look how to convert to double.
For your boolean field I would use a JRadioButton including a ButtonGroup and how to get which radiobutton was selected in a buttongroup:
This code should give you a starting point on your way to finish it yourself
The annoyingGui while shorter, is not my favorite since it opens a new dialog for the user each time you want to get an imput from them, which is annoying.
The singleDialogInformation() displays a more complex GUI using a JPanel and GridLayout for requesting user information and a BoxLayout to show it back to the user, note that I'm not using 2 different variables, but reassigning the pane variable to a new instance of a JPanel with a different layout.
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UsingDialogsExample {
private JFrame frame;
private JPanel pane;
private JTextField daysField;
private JTextField assignmentField;
private int days = 0;
private int assignments = 0;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Comment / uncomment one of them to see the output related to each sample method.
// new UsingDialogsExample().annoyingGui();
new UsingDialogsExample().singleDialogInformation();
}
});
}
public void annoyingGui() {
frame = new JFrame("My Frame's Title");
String daysInput = JOptionPane.showInputDialog(frame, "How many days are left?"); //Get user input on the textfield as a String
String assignmentsInput = JOptionPane.showInputDialog(frame, "How many assignments are due?");
try {
days = Integer.parseInt(daysInput); //Convert the string gotten above to an int
assignments = Integer.parseInt(assignmentsInput);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
JOptionPane.showMessageDialog(frame, "The number of days left is: " + days);
JOptionPane.showMessageDialog(frame, "The number of assignments due is: " + assignments);
}
public void singleDialogInformation() {
pane = new JPanel();
pane.setLayout(new GridLayout(0, 2, 2, 2));
daysField = new JTextField(5);
assignmentField = new JTextField(5);
pane.add(new JLabel("How many days are left?"));
pane.add(daysField);
pane.add(new JLabel("How many assignments are due?"));
pane.add(assignmentField);
int option = JOptionPane.showConfirmDialog(frame, pane, "Please fill all the fields", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (option == JOptionPane.YES_OPTION) {
String daysInput = daysField.getText();
String assignmentsInput = assignmentField.getText();
try {
days = Integer.parseInt(daysInput);
assignments = Integer.parseInt(assignmentsInput);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
pane.add(new JLabel("Days left: " + days));
pane.add(new JLabel("Assignments due: " + assignments));
JOptionPane.showMessageDialog(frame, pane);
}
}
}
Screenshots of the annoyingGui:
Screenshots of the singleDialogInformation:
Simplified: How to make String value to call specific existed JButton variable name in java?
I'm trying to make a non-ordinary Tic-Tac-Toe game...
Anyway, what I will post here is not really the whole concept of that. I just want to make it simple: I have 9 square jButtons named (3 by 3) (and maybe allow user to make 4x4, 5x5, 10x10 etc. via settings in future):
[markbox_00_00] / [markbox_00_01] / [markbox_00_02]
[markbox_01_00] / [markbox_01_01] / [markbox_01_02]
[markbox_02_00] / [markbox_02_01] / [markbox_02_02]
[btnSave] / [btnUndoActions]
where the first two digit represent the row and the next two is the column; and a save button (btnSave) and undo button(btnUndoActions).
Each markbox have default spring value of "0", when I click it turns "1"; and when I click "1" it turns "0". When you press undo button it will reset to last save.
Here is some of my simplified line of codes:
private byte markboxColLimit = 3, markboxRowLimit = 3, row, col;
private byte[][] saveNumber = new byte[markboxRowLimit][markboxColLimit];
private String buttonName;
public Astral_TicTacToe() {
initComponents();
/* I want something like this, but using a for loop based on markboxColLimit and
markboxRowLimit as limits */
markbox_00_00.setText("0");
markbox_00_01.setText("0");
markbox_00_02.setText("0");
markbox_01_00.setText("0");
markbox_01_01.setText("0");
markbox_01_02.setText("0");
markbox_02_00.setText("0");
markbox_02_01.setText("0");
markbox_02_02.setText("0");
/* I know the line below is wrong... what I'm trying is to avoid
* repetitiveness by looping and dynamically calling the variable
* name of JButtons, or in other ways...
*/
/* Attempting to make an alternative code from above (trying to make a loop instead) */
for(row = 0; row < markboxRowLimit; row++){
for(col = 0; col < markboxColLimit; col++){
buttonName = "markbox_0" + Byte.toString(row) + "_0" + Byte.toString(col);
buttonName.setText("0");
}
}
}
private void btnUndoActionsActionPerformed(java.awt.event.ActionEvent evt) {
markbox_00_00.setText(Byte.toString(saveNumber[0][0]));
markbox_00_01.setText(Byte.toString(saveNumber[0][1]));
markbox_00_02.setText(Byte.toString(saveNumber[0][2]));
markbox_01_00.setText(Byte.toString(saveNumber[1][0]));
markbox_01_01.setText(Byte.toString(saveNumber[1][1]));
markbox_01_02.setText(Byte.toString(saveNumber[1][2]));
markbox_02_00.setText(Byte.toString(saveNumber[2][0]));
markbox_02_01.setText(Byte.toString(saveNumber[2][1]));
markbox_02_02.setText(Byte.toString(saveNumber[2][2]));
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
saveNumber[0][0] = Byte.parseByte(markbox_00_00.getText());
saveNumber[0][1] = Byte.parseByte(markbox_00_01.getText());
saveNumber[0][2] = Byte.parseByte(markbox_00_02.getText());
saveNumber[1][0] = Byte.parseByte(markbox_01_00.getText());
saveNumber[1][1] = Byte.parseByte(markbox_01_01.getText());
saveNumber[1][2] = Byte.parseByte(markbox_01_00.getText());
saveNumber[2][0] = Byte.parseByte(markbox_02_00.getText());
saveNumber[2][1] = Byte.parseByte(markbox_02_01.getText());
saveNumber[2][2] = Byte.parseByte(markbox_02_02.getText());
}
private void markbox_00_00ActionPerformed(java.awt.event.ActionEvent evt) {
if("0".equals(markbox_00_00.getText()))
markbox_00_00.setText("1");
else
markbox_00_00.setText("0");
}
private void markbox_00_01ActionPerformed(java.awt.event.ActionEvent evt) {
if("0".equals(markbox_00_01.getText()))
markbox_00_00.setText("1");
else
markbox_00_00.setText("0");
}
....
private void markbox_02_02ActionPerformed(java.awt.event.ActionEvent evt) {
if("0".equals(markbox_00_00.getText()))
markbox_02_02.setText("1");
else
markbox_02_02.setText("0");
}
In short: how can I make String a specific variable name of JButton for calling/accessing/editing for their properties?
Example:
buttonName = markbox_01_02;
buttonName.setText("2");
is equavalent to markbox_01_02.getText("2");
I really appreciate the help, thank you...
P.S. I use to make JFrame in NetBeans Design (just click and drag the objects in palette window like JPanel, JButton, etc., so I don't type the code manually except making my own logical Method).
You probably need to redo your program and rephrase your question because it's kind of unclear where you're stuck that's why I wrote this answer as a Community Wiki.
The following program creates a GridLayout for the board and add 2 JButtons below it which contain "Save" and "Undo" buttons.
Whenever you press a button it will change it's text to 0 or 1 depending on the previous state of the button, and "Undo" button will undo last clic the same way, if it was 0 it will become 1 and viceversa.
I guess you should read How to write an ActionListener before copy-pasting this example, understand what it says and try to understand how this program works.
The logic to "Save" button is up to you 'cause I'm not sure what you wanna do there and I'm not gonna write all the code for you. This is made only for you to get an idea on how to handle events.
Also, the logic to end the game is left to you for the same reasons as the "Save" button.
I wish I knew how to record my screen in Ubuntu and save as GIF but here's a screenshot on how this program looks like:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TicTacToe implements ActionListener {
JFrame frame;
JButton buttons[];
JPanel pane, buttonPane;
boolean pressed[];
JButton save, undo;
int n = -1;
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < buttons.length; i++) {
if(e.getSource() == buttons[i]) {
pressed[i] = !pressed[i];
buttons[i].setText(pressed[i] ? "1" : "0");
n = i;
break;
}
}
}
public TicTacToe () {
frame = new JFrame("Tic Tac Toe Game");
buttons = new JButton[9];
pane = new JPanel(new GridLayout(3, 3));
pressed = new boolean[9];
buttonPane = new JPanel(new FlowLayout());
save = new JButton("Save");
undo = new JButton("Undo");
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton("0");
pressed[i] = false;
}
for (JButton b : buttons) {
b.addActionListener(this);
pane.add(b);
}
undo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (n == -1) {
return;
}
pressed[n] = !pressed[n];
buttons[n].setText(pressed[n] ? "1" : "0");
}
});
buttonPane.add(save);
buttonPane.add(undo);
frame.add(pane, BorderLayout.PAGE_START);
frame.add(buttonPane, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main (String args[]) {
new TicTacToe();
}
}
so my problem is that i'm making a really simple number guessing game in java swing, i added a JButton and a JTextField, i want to click the button to get the value written in my text field and then pass it to a do while loop that's gonna make the operation, then i'm going to display it in a JLabel, But i don't know how. I have no JFrame cause i'm gonna send this class to the class that has the JFrame, so i'm adding everything to a JPanel.
hope I was clear, would appreciate some help.
code:
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameDat extends JPanel {
JPanel panel = new JPanel();
FlowLayout layout = new FlowLayout();
Random random = new Random();
JButton enter;
JTextField display;
JLabel result;
int MIN = 1;
int MAX = 50;
int compare = random.nextInt(MAX - MIN + 1) + MIN;
int player;
int guessIt = 0;
public void iniGame(){
enter = new JButton("Pick a number:");
panel.setSize(300,300);
panel.setLayout(layout);
panel.add(enter);
panel.add(display);
panel.add(result);
enter.addActionListener(new CustomActionListenerEnter());
do {
//here should go the value captured from the JTextField instead of a scanner
player = input nextInt();
guessIt++;
if (player > compare)
//this text should go to the jlabel instead of printing it
System.out.println("Number is less than " + player);
else if (player < compare)
//this text should go to the jlabel instead of printing it
System.out.println("Number is greater than " + player);
else
//this text should go to the jlabel instead of printing it
System.out.println(compare + " was my number, it took you " + guessIt + " guesses.");
} while (player != compare);
}
class CustomActionListenerEnter implements ActionListener{
public void actionPerformed(ActionEvent e){
//I wanna pass this value to my do while.
display.getText();
}
}
}
My main recommendation: get rid of the while loop. This loop works with a linear console type program, but this is not how event-driven GUI's work, and that loop will tie up the Swing event thread, rendering your GUI frozen and useless. So, again, get rid of that loop, and instead change the state of a variable in this class based on user's response, and base behavior on that state. So in your ActionListener, get the text from the JTextField, convert it to an int by parsing it, check if it's a decent guess, and notify the user of the results of their guess.
Other issues:
You understand that your class above creates two JPanels, one the JPanel which is the instance of the class itself, and which you add nothing to, and the other one that is referenced by the panel variable that you do add components to. If you're only going to use the panel variable JPanel, then the class should probably not extend JPanel.
What's with the,... //here should go the value captured from the JTextField instead of a scanner and player = input nextInt();? Get rid of this non-compiling code and not helpful code and use the JTextField. It looks like you're trying to shoe-horn console code into a GUI, and this won't work.
Do not use System.out.println(...) or similar code for user interaction, but only for temporary debugging if that. All user interaction should be through the GUI itself, and final production code should not have printlns.
Well it turned out like this at the end, and it works fine. Thanks.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
player = Integer.parseInt(jTextField3.getText());
guessIt++;
if (player > compare)
jTextField1.setText("Number is less than " + player);
else if (player < compare)
jTextField1.setText("Number is greater than " + player);
else
jTextField1.setText(compare + " was my number, it took you " +
guessIt + " guesses.");
}
I'm trying to create a Java AWT program with these codes:
import javax.swing.*;
import java.awt.*;
public class Exer1 extends JFrame {
public Exer1(){
super ("Addition");
JLabel add1 = new JLabel("Enter 1st Integer: ");
JTextField jtf1 = new JTextField(10);
JLabel add2 = new JLabel("Enter 2nd Integer: ");
JTextField jtf2 = new JTextField(10);
JButton calculate = new JButton("Calculate");
FlowLayout flo = new FlowLayout();
setLayout(flo);
add(add1);
add(jtf1);
add(add2);
add(jtf2);
add(calculate);
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] a){
Exer1 ex1 = new Exer1();
}
}
My problem is HOW to add these 2 integers using JTextField. Can someone help me? Thank you so much. :)
You need to use ActionListener on your JButton.
Then you need to get int's from JTextField's and sum them like next:
calculate.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
int i1 = Integer.valueOf(jtf1.getText());
int i2 = Integer.valueOf(jtf2.getText());
System.out.println("sum=" + (i1 + i2));
} catch (Exception e1){
e1.printStackTrace();
}
}
});
Generally, you should create an event listener for click events on your button: Lesson: Writing Event Listeners. In that handler, you would take contents of your two text fields, convert them to integers:
Integer i1 = Integer.valueOf(jtf1.getText());
Then you can add those two integers and display them in another control or do anything else with them.
Start with How to Use Buttons, Check Boxes, and Radio Buttons and
How to Write an Action Listeners
This will provide you with the information you need to be able to tell when the user presses the button.
JTextField#getText then return's String. The problem then becomes a problem of converting a String to a int, which if you take the time, there are thousands of examples demonstrating how to achieve that
Once you've played around with oddities of converting String to a int, you could take a look at How to Use Spinners and How to Use Formatted Text Fields which perform there own validation on the values been entered
My assignment is to input 20 numbers via a text field then out the mean, the median and the total using a while loop. I should be able to figure out the while loop myself, but I can't get the text field to input numbers into an array. Please help, here is my code so far:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
public class whileloopq extends Applet implements ActionListener
{
Label label;
TextField input;
int[] numArray = new int[20];
int num;
public void init ()
{
Label label = new Label("Enter numbers");
TextField input = new TextField(5);
add(label);
add(input);
input.addActionListener(this);
}
public void actionPerformed (ActionEvent ev)
{
int num = Integer.parseInt(input.getText());
int index = 0;
numArray[index] = num;
index++;
input.setText("");
}
public void paint (Graphics graf)
{
graf.drawString("Array" + numArray, 25, 85);
}
}
Any help would be much appreciated.
(Answer written under the assumption that this is a homework assignment.)
You know how to parse an integer from a string, as you show with your usage of Integer.parseInt, but you are calling it to parse the entire 20 characters as one integer. You need to get each character individually to be parsed.
I recommend using a for loop, and String#substring to substring the input text into several strings of length one.
Alternatively, you can split the input text around an empty string and then iterate through the resulting array (note that the first string in the array will be empty), but the other approach is more likely the one expected from someone new to Java, so you'll have to use your judgement here.
In actionPerformed() you are trying to read from class filed input.setText("");
but in init() you didn't initialized that field but created and added to applet local variable
TextField input = new TextField(5);
so class field is steal null. Change it to
input = new TextField(5);
import java.awt.*;
public class frame4array extends Frame
{
Checkbox c1[];
TextField t1[];
int i;
frame4array(String p)
{
super(p);
c1=new Checkbox[2];
t1=new TextField[2];
for(i=0;i<2;i++)
{
t1[0]=new TextField();
t1[0].setBounds(200, 50, 150, 30);
t1[1]=new TextField();
t1[1].setBounds(200, 80, 150, 30);
c1[0]=new Checkbox("Singing");
c1[0].setBackground(Color.red);
c1[0].setBounds(430,200,120,40);
c1[1]=new Checkbox("Cricket",true);
}
for(i=0;i<2;i++)
{
add(t1[i]);
add(c1[i]);
}
setFont(new Font("Arial",Font.ITALIC,40));
}
public static void main(String s[])
{
frame4array f1=new frame4array("hello");
f1.setSize(600,500);
f1.setVisible(true);
}
}