Creating the buttons on a calculator using a for-loop

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
How can I make the for-loop more compact by using functions for actionlistener?
package räknare;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.Font;
import java.awt.event.ActionEvent;
public class miniräknare {
static JFrame frame;
static JTextField textField;
static JButton nummerButton;
static String Inmatning= null;
static double talOne;
static double talTwo;
static double resultat;
static String räknesätt;
static String svar;
public static void main(String args) {
Inmatning = new String();
nummerButton=new JButton [10];
frame=new JFrame("Kalkylatorn");
frame.setBounds(100,100,270,450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField= new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10,22,234,62);
frame.getContentPane().add(textField);
textField.setColumns(10);
for (int i=1 ; i<10 ; i++)
nummerButton[i]= new JButton(String.valueOf(i));
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
nummerButton[i].setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
nummerButton[i].setFont(new Font("Tahoma", Font.BOLD, 20));
frame.getContentPane().add(nummerButton[i]);
nummerButton[i].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
JButton myButton = (JButton)e.getSource();
String Inmatning= textField.getText() + myButton.getText();
textField.setText(Inmatning);
);
java calculator
add a comment |Â
up vote
0
down vote
favorite
How can I make the for-loop more compact by using functions for actionlistener?
package räknare;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.Font;
import java.awt.event.ActionEvent;
public class miniräknare {
static JFrame frame;
static JTextField textField;
static JButton nummerButton;
static String Inmatning= null;
static double talOne;
static double talTwo;
static double resultat;
static String räknesätt;
static String svar;
public static void main(String args) {
Inmatning = new String();
nummerButton=new JButton [10];
frame=new JFrame("Kalkylatorn");
frame.setBounds(100,100,270,450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField= new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10,22,234,62);
frame.getContentPane().add(textField);
textField.setColumns(10);
for (int i=1 ; i<10 ; i++)
nummerButton[i]= new JButton(String.valueOf(i));
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
nummerButton[i].setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
nummerButton[i].setFont(new Font("Tahoma", Font.BOLD, 20));
frame.getContentPane().add(nummerButton[i]);
nummerButton[i].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
JButton myButton = (JButton)e.getSource();
String Inmatning= textField.getText() + myButton.getText();
textField.setText(Inmatning);
);
java calculator
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How can I make the for-loop more compact by using functions for actionlistener?
package räknare;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.Font;
import java.awt.event.ActionEvent;
public class miniräknare {
static JFrame frame;
static JTextField textField;
static JButton nummerButton;
static String Inmatning= null;
static double talOne;
static double talTwo;
static double resultat;
static String räknesätt;
static String svar;
public static void main(String args) {
Inmatning = new String();
nummerButton=new JButton [10];
frame=new JFrame("Kalkylatorn");
frame.setBounds(100,100,270,450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField= new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10,22,234,62);
frame.getContentPane().add(textField);
textField.setColumns(10);
for (int i=1 ; i<10 ; i++)
nummerButton[i]= new JButton(String.valueOf(i));
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
nummerButton[i].setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
nummerButton[i].setFont(new Font("Tahoma", Font.BOLD, 20));
frame.getContentPane().add(nummerButton[i]);
nummerButton[i].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
JButton myButton = (JButton)e.getSource();
String Inmatning= textField.getText() + myButton.getText();
textField.setText(Inmatning);
);
java calculator
How can I make the for-loop more compact by using functions for actionlistener?
package räknare;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.Font;
import java.awt.event.ActionEvent;
public class miniräknare {
static JFrame frame;
static JTextField textField;
static JButton nummerButton;
static String Inmatning= null;
static double talOne;
static double talTwo;
static double resultat;
static String räknesätt;
static String svar;
public static void main(String args) {
Inmatning = new String();
nummerButton=new JButton [10];
frame=new JFrame("Kalkylatorn");
frame.setBounds(100,100,270,450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField= new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10,22,234,62);
frame.getContentPane().add(textField);
textField.setColumns(10);
for (int i=1 ; i<10 ; i++)
nummerButton[i]= new JButton(String.valueOf(i));
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
nummerButton[i].setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
nummerButton[i].setFont(new Font("Tahoma", Font.BOLD, 20));
frame.getContentPane().add(nummerButton[i]);
nummerButton[i].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
JButton myButton = (JButton)e.getSource();
String Inmatning= textField.getText() + myButton.getText();
textField.setText(Inmatning);
);
java calculator
edited Apr 7 at 12:31
Mast
7,32863484
7,32863484
asked Apr 7 at 11:45
javanoob
41
41
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
Basics
Classes in Java naming conventions should start with an upper-case letter: miniräknare should be Miniräknare.
Variables in Java naming conventions should start with a lower-case letter: Inmatning should be inmatning.
Refactoring
There are a few things in your code that can be refactored to improve the readability. Functions are a part of that. Consider a function that adds a digits to the display:
private void addDigit(String digit)
textField.setText(textField.getText() + digit);
That function removes the display logic from the action listener, and puts it in the miniräknare class itself.
Another trick in Java is that interfaces with a single method, like an ActionListener are implicitly a functional interface. This allows instances of them to be declared in functional shorthand....
nummerButton[i].addActionListener(e -> addDigit(((JButton)e.getSource()).getText())
One more thing is that the font can be extracted to a constant...
private static final Font keyFont = new Font("Tahoma", Font.BOLD, 20);
Code order
I know this one is pedantic, but when you have "setup code" and "use code", you should put all the setup code first before you use the instance you have set up.
In your case, you have the line of code:
frame.getContentPane().add(nummerButton[i]);
before you add the ActionListener to nummerButton[i].
Also, I would avoid the nummerButton array entirely until the button is complete....
Result
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
JButton key = new JButton(String.valueOf(i));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
nummerButton[i] = key;
frame.getContentPane().add(key);
If you want, you can put all the JButton creation code in a new function too, but that may be too much refactoring in your case depending on how much other code is in your method....
private JButton createKey(int value)
int kolumn =((value-1)%3);
int horisont=((value-1)/3);
JButton key = new JButton(String.valueOf(value));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
return key;
and then your calling code becomes:
nummerButton[i] = createKey(i);
frame.getContentPane().add(nummerButton[i]);
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Basics
Classes in Java naming conventions should start with an upper-case letter: miniräknare should be Miniräknare.
Variables in Java naming conventions should start with a lower-case letter: Inmatning should be inmatning.
Refactoring
There are a few things in your code that can be refactored to improve the readability. Functions are a part of that. Consider a function that adds a digits to the display:
private void addDigit(String digit)
textField.setText(textField.getText() + digit);
That function removes the display logic from the action listener, and puts it in the miniräknare class itself.
Another trick in Java is that interfaces with a single method, like an ActionListener are implicitly a functional interface. This allows instances of them to be declared in functional shorthand....
nummerButton[i].addActionListener(e -> addDigit(((JButton)e.getSource()).getText())
One more thing is that the font can be extracted to a constant...
private static final Font keyFont = new Font("Tahoma", Font.BOLD, 20);
Code order
I know this one is pedantic, but when you have "setup code" and "use code", you should put all the setup code first before you use the instance you have set up.
In your case, you have the line of code:
frame.getContentPane().add(nummerButton[i]);
before you add the ActionListener to nummerButton[i].
Also, I would avoid the nummerButton array entirely until the button is complete....
Result
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
JButton key = new JButton(String.valueOf(i));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
nummerButton[i] = key;
frame.getContentPane().add(key);
If you want, you can put all the JButton creation code in a new function too, but that may be too much refactoring in your case depending on how much other code is in your method....
private JButton createKey(int value)
int kolumn =((value-1)%3);
int horisont=((value-1)/3);
JButton key = new JButton(String.valueOf(value));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
return key;
and then your calling code becomes:
nummerButton[i] = createKey(i);
frame.getContentPane().add(nummerButton[i]);
add a comment |Â
up vote
4
down vote
Basics
Classes in Java naming conventions should start with an upper-case letter: miniräknare should be Miniräknare.
Variables in Java naming conventions should start with a lower-case letter: Inmatning should be inmatning.
Refactoring
There are a few things in your code that can be refactored to improve the readability. Functions are a part of that. Consider a function that adds a digits to the display:
private void addDigit(String digit)
textField.setText(textField.getText() + digit);
That function removes the display logic from the action listener, and puts it in the miniräknare class itself.
Another trick in Java is that interfaces with a single method, like an ActionListener are implicitly a functional interface. This allows instances of them to be declared in functional shorthand....
nummerButton[i].addActionListener(e -> addDigit(((JButton)e.getSource()).getText())
One more thing is that the font can be extracted to a constant...
private static final Font keyFont = new Font("Tahoma", Font.BOLD, 20);
Code order
I know this one is pedantic, but when you have "setup code" and "use code", you should put all the setup code first before you use the instance you have set up.
In your case, you have the line of code:
frame.getContentPane().add(nummerButton[i]);
before you add the ActionListener to nummerButton[i].
Also, I would avoid the nummerButton array entirely until the button is complete....
Result
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
JButton key = new JButton(String.valueOf(i));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
nummerButton[i] = key;
frame.getContentPane().add(key);
If you want, you can put all the JButton creation code in a new function too, but that may be too much refactoring in your case depending on how much other code is in your method....
private JButton createKey(int value)
int kolumn =((value-1)%3);
int horisont=((value-1)/3);
JButton key = new JButton(String.valueOf(value));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
return key;
and then your calling code becomes:
nummerButton[i] = createKey(i);
frame.getContentPane().add(nummerButton[i]);
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Basics
Classes in Java naming conventions should start with an upper-case letter: miniräknare should be Miniräknare.
Variables in Java naming conventions should start with a lower-case letter: Inmatning should be inmatning.
Refactoring
There are a few things in your code that can be refactored to improve the readability. Functions are a part of that. Consider a function that adds a digits to the display:
private void addDigit(String digit)
textField.setText(textField.getText() + digit);
That function removes the display logic from the action listener, and puts it in the miniräknare class itself.
Another trick in Java is that interfaces with a single method, like an ActionListener are implicitly a functional interface. This allows instances of them to be declared in functional shorthand....
nummerButton[i].addActionListener(e -> addDigit(((JButton)e.getSource()).getText())
One more thing is that the font can be extracted to a constant...
private static final Font keyFont = new Font("Tahoma", Font.BOLD, 20);
Code order
I know this one is pedantic, but when you have "setup code" and "use code", you should put all the setup code first before you use the instance you have set up.
In your case, you have the line of code:
frame.getContentPane().add(nummerButton[i]);
before you add the ActionListener to nummerButton[i].
Also, I would avoid the nummerButton array entirely until the button is complete....
Result
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
JButton key = new JButton(String.valueOf(i));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
nummerButton[i] = key;
frame.getContentPane().add(key);
If you want, you can put all the JButton creation code in a new function too, but that may be too much refactoring in your case depending on how much other code is in your method....
private JButton createKey(int value)
int kolumn =((value-1)%3);
int horisont=((value-1)/3);
JButton key = new JButton(String.valueOf(value));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
return key;
and then your calling code becomes:
nummerButton[i] = createKey(i);
frame.getContentPane().add(nummerButton[i]);
Basics
Classes in Java naming conventions should start with an upper-case letter: miniräknare should be Miniräknare.
Variables in Java naming conventions should start with a lower-case letter: Inmatning should be inmatning.
Refactoring
There are a few things in your code that can be refactored to improve the readability. Functions are a part of that. Consider a function that adds a digits to the display:
private void addDigit(String digit)
textField.setText(textField.getText() + digit);
That function removes the display logic from the action listener, and puts it in the miniräknare class itself.
Another trick in Java is that interfaces with a single method, like an ActionListener are implicitly a functional interface. This allows instances of them to be declared in functional shorthand....
nummerButton[i].addActionListener(e -> addDigit(((JButton)e.getSource()).getText())
One more thing is that the font can be extracted to a constant...
private static final Font keyFont = new Font("Tahoma", Font.BOLD, 20);
Code order
I know this one is pedantic, but when you have "setup code" and "use code", you should put all the setup code first before you use the instance you have set up.
In your case, you have the line of code:
frame.getContentPane().add(nummerButton[i]);
before you add the ActionListener to nummerButton[i].
Also, I would avoid the nummerButton array entirely until the button is complete....
Result
int kolumn =((i-1)%3);
int horisont=((i-1)/3);
JButton key = new JButton(String.valueOf(i));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
nummerButton[i] = key;
frame.getContentPane().add(key);
If you want, you can put all the JButton creation code in a new function too, but that may be too much refactoring in your case depending on how much other code is in your method....
private JButton createKey(int value)
int kolumn =((value-1)%3);
int horisont=((value-1)/3);
JButton key = new JButton(String.valueOf(value));
key.setBounds((10+(kolumn*60)),(220-(horisont*60)),50,50);
key.setFont(keyFont);
key.addActionListener(e -> addDigit(((JButton)e.getSource()).getText());
return key;
and then your calling code becomes:
nummerButton[i] = createKey(i);
frame.getContentPane().add(nummerButton[i]);
edited Apr 7 at 12:34
answered Apr 7 at 12:19
rolflâ¦
90.2k13186390
90.2k13186390
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191472%2fcreating-the-buttons-on-a-calculator-using-a-for-loop%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password