Button must add 50 to integer but its not . I dont know well about Jframe so help me guys.
int money = 0;
...
JButton verlan = new JButton("50 kr\u015F");
verlan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int moremoney = money + 50;
String x=Integer.toString(moremoney);
textArea.setText(x + " cent");
}
});
In your ActionListener you define a new variable on which you add up the value of money and 50, however you never update the initial value of money. Instead you can update money, however you have to make sure that the variable is available in the scope of the ActionListener, e.g. by declaring it as a member variable.
private int money = 0;
...
JButton verlan = new JButton("50 kr\u015F");
verlan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
money += 50;
String x=Integer.toString(money);
textArea.setText(x + " cent");
}
});
Related
I need to create a lottery program that will allow user to select 4 numbers (image of the buttons here)
which then should be inputed into an array, I cannot get the array to fill fully, I have tried different versions of adding an int into array and I just can't seem to find the right one..
int hold;
int userNumbers[] = new int[3];
public lotteryGUI() {
initComponents();
}
private void twentyoneActionPerformed(java.awt.event.ActionEvent evt) {
hold = 21;
hold += userNumbers[0];
}
private void nineActionPerformed(java.awt.event.ActionEvent evt) {
hold = 9;
userNumbers[0]= hold + userNumbers[0];
}
private void oneActionPerformed(java.awt.event.ActionEvent evt) {
hold = 1;
userNumbers[0] = userNumbers[0] + hold ;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(Arrays.toString(userNumbers));
}
This is just a sample with 3 buttons, As you can probably tell the output i get is (10,0,0).
#edit
I have contacted my lecturer about this project and I received a little hint. I am supposed to add ActionListener and if the button is checked I am supposed to add it to an array. Not really sure how to do that, I'm going to look at some tutorials and edit this post if necessary
I would hate to hard code it, but it is really up to you to figure out how to do that for 28 buttons. Here is example for 1 button:
int counter = 0;
int[] userNumbers = new int[3];
public lotteryGUI() {
initComponents();
JButton number1 = new JButton("1");
JButton number2 = new JButton("2");
number1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userNumbers[counter] = 1;
counter = counter + 1;
if (counter == 3)
// disable all buttons and display result maybe?
}
}
// ... the rest 27 buttons maybe?
}
I'm making a math flashcard application that takes 20 answers and randomizes the question each time.
My Problem: The for loop does not wait until the button is pressed. I need to find a way to wait for the ActionListener to be executed before it goes through the loop again.
Please bear with me as I am quite new to Java.
Here is my current code:
for(int i = 0; i < 20; i++){
Random numberOne = new Random();
Random numberTwo = new Random();
firstNumberInt = numberOne.nextInt(12) + 1;
secondNumberInt = numberTwo.nextInt(12) + 1;
firstNumber = Integer.toString(firstNumberInt);
secondNumber = Integer.toString(secondNumberInt);
firstNumberPrint.setText(firstNumber);
secondNumberPrint.setText(secondNumber);
answerBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String answerString = answerField.getText();
try {
int answer = Integer.parseInt(answerString);
} catch(NumberFormatException e){
error.setText("Number Cannot Be A Word");
}
}
});
}
The ActionListener should be outside the loop and the loop should be within it.
Not the opposite.
answerBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// Loop somewhere inside here
String answerString = answerField.getText();
try {
int answer = Integer.parseInt(answerString);
} catch(NumberFormatException e){
error.setText("Number Cannot Be A Word");
}
}
});
The code inside the overridden method inside the ActionListener class is being invoked every time the Component is interacted.
I want to add scroll bar to my text area so that if the user inputs a number greater than 20 the text are should have a scroll bar.
Basically I am trying to make a application where user inputs a number he wants the multiplication table and also he inputs up to what number he wants the table to be displayed.But my application show table up to 20 e.g 12 X 20 = 240. and the rest is hidden.
public class LayoutM extends JFrame implements ActionListener {
private JTextField num1;
private JTextField num2;
private JTextArea answer;
private JButton go;
private int num11;
private int num22;
public LayoutM(){
super("Multiplication");
setLayout(new FlowLayout());
Dimension numDim = new Dimension(60,20);
Dimension ansDim = new Dimension(200,300);
Dimension goDim = new Dimension(60,20);
num1 = new JTextField("Number");
num1.setPreferredSize(numDim);
num2 = new JTextField("Upto");
num2.setPreferredSize(numDim);
go = new JButton("GO");
num2.setPreferredSize(goDim);
answer = new JTextArea(20,20);
answer.setPreferredSize(ansDim);
answer.setEditable(false);
add(num1, BorderLayout.CENTER);
add(num2,BorderLayout.CENTER);
add(go,BorderLayout.CENTER);
add(answer,BorderLayout.SOUTH);
go.addActionListener(this);
}
public static void main(String[] args){
LayoutM ob = new LayoutM();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setVisible(true);
ob.setSize(300,400);
ob.setResizable(false);
ob.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent event){
try{
answer.setText(" ");
num11 = Integer.parseInt(num1.getText());
num22 = Integer.parseInt(num2.getText());
for(int count = 1; count < num22+1;count++){
answer.append(num11+ " X "+ count+" = " + num11*count+" \n");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "No decimals allowed");
}
}
}
You should put the answer object into a new JScrollPane object, and add the scroll pane to your LayoutM.
So, in your fields you should add:
private JScrollPane scroll;
Instead of using
add(answer,BorderLayout.SOUTH);
You should use
add(scroll,BorderLayout.SOUTH);
And in your actionPerformed() method, you should change the number of rows according to the number you got from the user. Put this before the for loop:
if ( num22 > 20 ) {
answer.setRows(num22);
} else {
answer.setRows(20);
}
to get to the point, i can't seam to write to a static double vareable, or JTextField from an external ActionListener class.
i am making an advanced calculator, and to make things easier, i am trying to create the GUI, imlementing its buttons, and other features, and i am trying to put the ActionListeners in anouther class. in eclipse, it says i need to have the vareables for the calculator static, but making them static, i can no longer write to them, and display answers.
this is the code i have:
public static JButton num0, num1, num2, num3, num4, num5, num6, num7, num8, num9;
public static double tempNum1;
public static double tempNum2;
public static boolean pointOn = false;
public static int APC = 1;
public GUI(){
GUINumListener numListener = new GUINumListener();
num0.addActionListener(numListener);
num1.addActionListener(numListener);
num2.addActionListener(numListener);
num3.addActionListener(numListener);
num4.addActionListener(numListener);
num5.addActionListener(numListener);
num6.addActionListener(numListener);
num7.addActionListener(numListener);
num8.addActionListener(numListener);
num9.addActionListener(numListener);
}
and in the GUINumListener class:
public class GUINumListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals(GUI.num0)){
GUI.tempNum2 *= 10;
}else if (e.getActionCommand().equals(GUI.num1)){
if (GUI.pointOn = false){
GUI.tempNum2 = (GUI.tempNum2 * 10) + 1;
}else{
GUI.tempNum2 = (GUI.tempNum2 * Math.pow(10, GUI.APC) + 1) / Math.pow(10, GUI.APC);
GUI.APC++;
}
GUI.ansField.setText(Double.toString(GUI.tempNum2));
}
}
clicking a number in the program does not output it in the ansField field.
HELP!
thanks
Don't use static fields, and also encapsulate them.
For ActionListener i always use inner private classes if the only scope is the outside class or Anoymous Classes.
Also it seems you have a collection of Button, you may consider grouping into collection.
Example (I comment in code):
private List<JButton> buttons;
private double tempNum1;
private double tempNum2;
private boolean pointOn = false;
private int APC = 1;
//make them private why public and static?
public GUI(){
ActionListener numListener = new GUINumListener();
//initiliatze buttons
int size=10;
buttons= new ArrayList<>(size);
for(int i=0;i<size;i++){
JButton button = new JButton();
button.addActionListener(numListener);
buttons.add(button);
}
}
private class GUINumListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == buttons.get(0)){ // actionCommand returns string you have to use getSource() or setting an actionCommand to the button and compare num0.getActionCommand()
tempNum2 *= 10;
}else if (e.getSource() == buttons.get(1)){
if (!pointOn){ // u were assigning pointOn = false
tempNum2 = (tempNum2 * 10) + 1;
}else{
tempNum2 = (tempNum2 * Math.pow(10, APC) + 1) / Math.pow(10, APC);
APC++;
}
ansField.setText(Double.toString(tempNum2));
}
}
The problem is your if checks: e.getActionCommand().equals(GUI.num0)
getActionCommand() returns a String and num0 is a JButton. So equals will always return false since they're not the same class type.
One way to solve this is to check the label of the button: getActionCommand()
So the if statement should be:
e.getActionCommand().equals(GUI.num0.getActionCommand())
I have these lines of code. I know you can not pass a non final variable to an inner class but I need to pass the variable i to the anonymous inner class to be used as a seatingID. Can you suggest ways of doing that ?
JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
for (int i = 0; i < 40; i++)
{
seats[i] = new JButton();//creating the buttons
seats[i].setPreferredSize(new Dimension(50,25));//button width
panel4seating.add(seats[i]);//adding the buttons to the panels
seats[i].addActionListener(new ActionListener()
{ //anonymous inner class
public void actionPerformed(ActionEvent evt)
{
String firstName = (String)JOptionPane.showInputDialog("Enter First Name");
String lastName = (String)JOptionPane.showInputDialog("Enter Last Name");
sw101.AddPassenger(firstName, lastName, seatingID);
}
});
}
The simple way is to create a local final variable and initialize it with the value of the loop variable; e.g.
JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
for (int i = 0; i < 40; i++)
{
seats[i] = new JButton();//creating the buttons
seats[i].setPreferredSize(new Dimension(50,25));//button width
panel4seating.add(seats[i]);//adding the buttons to the panels
final int ii = i; // Create a local final variable ...
seats[i].addActionListener(new ActionListener()
{ //anonymous inner class
public void actionPerformed(ActionEvent evt)
{
String firstName = (String)JOptionPane.showInputDialog("Enter First Name");
String lastName = (String)JOptionPane.showInputDialog("Enter Last Name");
sw101.AddPassenger(firstName, lastName, ii);
}
});
}
You can't directly, but you can make a (static private) subclass of ActionListener that takes a seatingID in its constructor.
Then rather than
seats[i].addActionListener(new ActionListener() { ... });
you'd have
seats[i].addActionListener(new MySpecialActionListener(i));
[Edit] Actually, there's so much else wrong with your code that I'm not really sure that this advice is good. How about presenting code that would compile.