GUI Calculator using JFrames and layouts
up vote
0
down vote
favorite
I'm currently working on a calculator which should perform basic calculations such as addition, subtraction, multiplication, and division. To achieve the final outcome, I've to follow a certain design for the calculator. The design of the calculator is provided with this question. I've given my best on how to match the official design of the calculator but it's not matching it.
This is the OFFICIAL design of the calculator. This is how it should look.
This is WHAT I'm getting when I run the code.
The CODE:
package patel.Jainam;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalculatorFrame extends JFrame {
/**
* All the buttons that will be used in the calculator have been initialized
*/
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button0;
private JButton buttonEqual;
private JButton buttonDot;
private JButton buttonClearLast;
private JButton buttonClearAll;
private JButton buttonAdd;
private JButton buttonSub;
private JButton buttonMul;
private JButton buttonDiv;
private JTextArea textArea;
public CalculatorFrame(){
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
panel2.add(buttonClearLast = new JButton ("Clear Last"));
panel2.add(buttonClearAll = new JButton ("Clear All"));
add(panel2, BorderLayout.PAGE_START);
JPanel panel3 = new JPanel();
textArea = new JTextArea(2,10);
// textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel3.add(scrollPane);
add(panel3, BorderLayout.LINE_START);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,4));
panel1.add(button7 = new JButton ("7"));
panel1.add(button8 = new JButton ("8"));
panel1.add(button9 = new JButton ("9"));
panel1.add(buttonAdd = new JButton ("+"));
panel1.add(button4 = new JButton ("4"));
panel1.add(button5 = new JButton ("5"));
panel1.add(button6 = new JButton ("6"));
panel1.add(buttonSub = new JButton ("-"));
panel1.add(button1 = new JButton ("1"));
panel1.add(button2 = new JButton ("2"));
panel1.add(button3 = new JButton ("3"));
panel1.add(buttonMul = new JButton ("*"));
panel1.add(button0 = new JButton ("0"));
panel1.add(buttonDot = new JButton ("."));
panel1.add(buttonEqual = new JButton ("="));
panel1.add(buttonDiv = new JButton ("/"));
add(panel1, BorderLayout.PAGE_END);
}
}
Thank you.
java swing jframe grid-layout
add a comment |
up vote
0
down vote
favorite
I'm currently working on a calculator which should perform basic calculations such as addition, subtraction, multiplication, and division. To achieve the final outcome, I've to follow a certain design for the calculator. The design of the calculator is provided with this question. I've given my best on how to match the official design of the calculator but it's not matching it.
This is the OFFICIAL design of the calculator. This is how it should look.
This is WHAT I'm getting when I run the code.
The CODE:
package patel.Jainam;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalculatorFrame extends JFrame {
/**
* All the buttons that will be used in the calculator have been initialized
*/
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button0;
private JButton buttonEqual;
private JButton buttonDot;
private JButton buttonClearLast;
private JButton buttonClearAll;
private JButton buttonAdd;
private JButton buttonSub;
private JButton buttonMul;
private JButton buttonDiv;
private JTextArea textArea;
public CalculatorFrame(){
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
panel2.add(buttonClearLast = new JButton ("Clear Last"));
panel2.add(buttonClearAll = new JButton ("Clear All"));
add(panel2, BorderLayout.PAGE_START);
JPanel panel3 = new JPanel();
textArea = new JTextArea(2,10);
// textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel3.add(scrollPane);
add(panel3, BorderLayout.LINE_START);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,4));
panel1.add(button7 = new JButton ("7"));
panel1.add(button8 = new JButton ("8"));
panel1.add(button9 = new JButton ("9"));
panel1.add(buttonAdd = new JButton ("+"));
panel1.add(button4 = new JButton ("4"));
panel1.add(button5 = new JButton ("5"));
panel1.add(button6 = new JButton ("6"));
panel1.add(buttonSub = new JButton ("-"));
panel1.add(button1 = new JButton ("1"));
panel1.add(button2 = new JButton ("2"));
panel1.add(button3 = new JButton ("3"));
panel1.add(buttonMul = new JButton ("*"));
panel1.add(button0 = new JButton ("0"));
panel1.add(buttonDot = new JButton ("."));
panel1.add(buttonEqual = new JButton ("="));
panel1.add(buttonDiv = new JButton ("/"));
add(panel1, BorderLayout.PAGE_END);
}
}
Thank you.
java swing jframe grid-layout
See also this calculator example. It usesScriptEngine
to evaluate the expression in the text field.
– Andrew Thompson
Nov 10 at 22:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm currently working on a calculator which should perform basic calculations such as addition, subtraction, multiplication, and division. To achieve the final outcome, I've to follow a certain design for the calculator. The design of the calculator is provided with this question. I've given my best on how to match the official design of the calculator but it's not matching it.
This is the OFFICIAL design of the calculator. This is how it should look.
This is WHAT I'm getting when I run the code.
The CODE:
package patel.Jainam;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalculatorFrame extends JFrame {
/**
* All the buttons that will be used in the calculator have been initialized
*/
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button0;
private JButton buttonEqual;
private JButton buttonDot;
private JButton buttonClearLast;
private JButton buttonClearAll;
private JButton buttonAdd;
private JButton buttonSub;
private JButton buttonMul;
private JButton buttonDiv;
private JTextArea textArea;
public CalculatorFrame(){
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
panel2.add(buttonClearLast = new JButton ("Clear Last"));
panel2.add(buttonClearAll = new JButton ("Clear All"));
add(panel2, BorderLayout.PAGE_START);
JPanel panel3 = new JPanel();
textArea = new JTextArea(2,10);
// textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel3.add(scrollPane);
add(panel3, BorderLayout.LINE_START);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,4));
panel1.add(button7 = new JButton ("7"));
panel1.add(button8 = new JButton ("8"));
panel1.add(button9 = new JButton ("9"));
panel1.add(buttonAdd = new JButton ("+"));
panel1.add(button4 = new JButton ("4"));
panel1.add(button5 = new JButton ("5"));
panel1.add(button6 = new JButton ("6"));
panel1.add(buttonSub = new JButton ("-"));
panel1.add(button1 = new JButton ("1"));
panel1.add(button2 = new JButton ("2"));
panel1.add(button3 = new JButton ("3"));
panel1.add(buttonMul = new JButton ("*"));
panel1.add(button0 = new JButton ("0"));
panel1.add(buttonDot = new JButton ("."));
panel1.add(buttonEqual = new JButton ("="));
panel1.add(buttonDiv = new JButton ("/"));
add(panel1, BorderLayout.PAGE_END);
}
}
Thank you.
java swing jframe grid-layout
I'm currently working on a calculator which should perform basic calculations such as addition, subtraction, multiplication, and division. To achieve the final outcome, I've to follow a certain design for the calculator. The design of the calculator is provided with this question. I've given my best on how to match the official design of the calculator but it's not matching it.
This is the OFFICIAL design of the calculator. This is how it should look.
This is WHAT I'm getting when I run the code.
The CODE:
package patel.Jainam;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalculatorFrame extends JFrame {
/**
* All the buttons that will be used in the calculator have been initialized
*/
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button0;
private JButton buttonEqual;
private JButton buttonDot;
private JButton buttonClearLast;
private JButton buttonClearAll;
private JButton buttonAdd;
private JButton buttonSub;
private JButton buttonMul;
private JButton buttonDiv;
private JTextArea textArea;
public CalculatorFrame(){
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
panel2.add(buttonClearLast = new JButton ("Clear Last"));
panel2.add(buttonClearAll = new JButton ("Clear All"));
add(panel2, BorderLayout.PAGE_START);
JPanel panel3 = new JPanel();
textArea = new JTextArea(2,10);
// textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel3.add(scrollPane);
add(panel3, BorderLayout.LINE_START);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,4));
panel1.add(button7 = new JButton ("7"));
panel1.add(button8 = new JButton ("8"));
panel1.add(button9 = new JButton ("9"));
panel1.add(buttonAdd = new JButton ("+"));
panel1.add(button4 = new JButton ("4"));
panel1.add(button5 = new JButton ("5"));
panel1.add(button6 = new JButton ("6"));
panel1.add(buttonSub = new JButton ("-"));
panel1.add(button1 = new JButton ("1"));
panel1.add(button2 = new JButton ("2"));
panel1.add(button3 = new JButton ("3"));
panel1.add(buttonMul = new JButton ("*"));
panel1.add(button0 = new JButton ("0"));
panel1.add(buttonDot = new JButton ("."));
panel1.add(buttonEqual = new JButton ("="));
panel1.add(buttonDiv = new JButton ("/"));
add(panel1, BorderLayout.PAGE_END);
}
}
Thank you.
java swing jframe grid-layout
java swing jframe grid-layout
edited Nov 10 at 21:03
asked Nov 10 at 19:19
J patel
45229
45229
See also this calculator example. It usesScriptEngine
to evaluate the expression in the text field.
– Andrew Thompson
Nov 10 at 22:16
add a comment |
See also this calculator example. It usesScriptEngine
to evaluate the expression in the text field.
– Andrew Thompson
Nov 10 at 22:16
See also this calculator example. It uses
ScriptEngine
to evaluate the expression in the text field.– Andrew Thompson
Nov 10 at 22:16
See also this calculator example. It uses
ScriptEngine
to evaluate the expression in the text field.– Andrew Thompson
Nov 10 at 22:16
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
The problem is you are over using the GridLayout.
I would suggest you want to keep using the default layout manager of the frame which is a BorderLayout
.
Then you would do the following:
Create a panel using a
GridLayout
for the two buttons. Add this panel to theBorderLayout.PAGE_START
of the frame.Add your scroll pane containing the text area to the
BorderLayout.CENTER
of the frame.Create a panel using the
GridLayout
for the buttons on the bottom. Add this panel to theBorderLayout.PAGE_END
of the frame.
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples of the Borderlayout
and GridLayout
.
so I tried what u told me to and came up with the following code. It still doesn't match the design. The code has been updated.
– J patel
Nov 10 at 19:33
I said that you were overusing the GridLayout. Why did you create a panel "p" with a GridLayout?. I suggested (3 times) that you add the components to the frame (not the panel), using the BorderLayout constraint. Get rid of the panel you called "p". Also, I suggested you should be adding the scroll pane directly to the frame, not to a panel first. Take the time to read the tutorial and understand how the layout managers work. Then think logically about breaking down your layout to take advantage of the layout features of different layout managers to achieve your desired layout.
– camickr
Nov 10 at 19:52
Okay so I was able to match the design but the TextArea is not setting up properly with the frame along with the JScroll Pane. I've updated the code and the image also. Thank you.
– J patel
Nov 10 at 21:02
@Jpatel Why do I have to keep repeating myself?. I have already told you the scroll pane should be added to the frame. Why are you creating extra components??? Did you not read the tutorial yet? It explains how the BorderLayout works.
– camickr
Nov 10 at 21:11
add a comment |
up vote
0
down vote
camickr's answer is optimal here.
Here is an SSCCE:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class CalculatorFrame extends JFrame {
public CalculatorFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(createClearPanel(), BorderLayout.PAGE_START);
getContentPane().add(createTextArea(), BorderLayout.CENTER);
getContentPane().add(createNumberPanels(), BorderLayout.PAGE_END);
setSize(300, 300);
pack();
}
private JPanel createNumberPanels() {
JPanel main = new JPanel();
main.setLayout(new GridLayout(4, 0));
for (int i = 0; i < 16; i++) {
JButton button = new JButton("" + i);
main.add(button);
}
return main;
}
private JScrollPane createTextArea() {
JTextArea area = new JTextArea(5, 10);
JScrollPane sp = new JScrollPane(area);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return sp;
}
private JPanel createClearPanel() {
JButton clearAll = new JButton("clear all");
JButton clearLast = new JButton("Clear last");
JPanel panel = new JPanel(new GridLayout(1, 0));
panel.add(clearLast);
panel.add(clearAll);
return panel;
}
public static void main(String args) {
SwingUtilities.invokeLater(() -> new CalculatorFrame().setVisible(true));
}
}
Preview:
P.S: Ignore the fact i used only numbers instead of operators like *,?,+,= etc.
Thank you so much the help.
– J patel
Nov 10 at 21:11
@J patel No problem. camickr had already help you. I just did the SSCCE, after i saw you still having problem to get to the point.
– George Zougianos
Nov 10 at 21:12
1) there is no need to set the layout for the content pane. The default layout is the BorderLayout as I stated in my answer. 2) there is no need to use getContentPane(). When you add a component to the frame it will be added to the content pane for you. 3) When you create a text area you should usenew JTextArea(5, 10)
to suggest the row/columns of the text area so the text area can calculate its preferred size. 4) use pack() so the frame can be sized at its preferred size.
– camickr
Nov 10 at 21:16
@camickr 1) I always prefer to have the layout of the container in front of my eyes. Plus, this question is all about border layout. 2) Before java 1.5 (i think) you had to use ContentPane, it is just habbit i have inherited from old docs/tutorials. 3) & 4) Editing right now, i thought i pack(), which would show me the cols & rows of the textarea. Thanks for noticing :)
– George Zougianos
Nov 10 at 21:21
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The problem is you are over using the GridLayout.
I would suggest you want to keep using the default layout manager of the frame which is a BorderLayout
.
Then you would do the following:
Create a panel using a
GridLayout
for the two buttons. Add this panel to theBorderLayout.PAGE_START
of the frame.Add your scroll pane containing the text area to the
BorderLayout.CENTER
of the frame.Create a panel using the
GridLayout
for the buttons on the bottom. Add this panel to theBorderLayout.PAGE_END
of the frame.
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples of the Borderlayout
and GridLayout
.
so I tried what u told me to and came up with the following code. It still doesn't match the design. The code has been updated.
– J patel
Nov 10 at 19:33
I said that you were overusing the GridLayout. Why did you create a panel "p" with a GridLayout?. I suggested (3 times) that you add the components to the frame (not the panel), using the BorderLayout constraint. Get rid of the panel you called "p". Also, I suggested you should be adding the scroll pane directly to the frame, not to a panel first. Take the time to read the tutorial and understand how the layout managers work. Then think logically about breaking down your layout to take advantage of the layout features of different layout managers to achieve your desired layout.
– camickr
Nov 10 at 19:52
Okay so I was able to match the design but the TextArea is not setting up properly with the frame along with the JScroll Pane. I've updated the code and the image also. Thank you.
– J patel
Nov 10 at 21:02
@Jpatel Why do I have to keep repeating myself?. I have already told you the scroll pane should be added to the frame. Why are you creating extra components??? Did you not read the tutorial yet? It explains how the BorderLayout works.
– camickr
Nov 10 at 21:11
add a comment |
up vote
2
down vote
accepted
The problem is you are over using the GridLayout.
I would suggest you want to keep using the default layout manager of the frame which is a BorderLayout
.
Then you would do the following:
Create a panel using a
GridLayout
for the two buttons. Add this panel to theBorderLayout.PAGE_START
of the frame.Add your scroll pane containing the text area to the
BorderLayout.CENTER
of the frame.Create a panel using the
GridLayout
for the buttons on the bottom. Add this panel to theBorderLayout.PAGE_END
of the frame.
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples of the Borderlayout
and GridLayout
.
so I tried what u told me to and came up with the following code. It still doesn't match the design. The code has been updated.
– J patel
Nov 10 at 19:33
I said that you were overusing the GridLayout. Why did you create a panel "p" with a GridLayout?. I suggested (3 times) that you add the components to the frame (not the panel), using the BorderLayout constraint. Get rid of the panel you called "p". Also, I suggested you should be adding the scroll pane directly to the frame, not to a panel first. Take the time to read the tutorial and understand how the layout managers work. Then think logically about breaking down your layout to take advantage of the layout features of different layout managers to achieve your desired layout.
– camickr
Nov 10 at 19:52
Okay so I was able to match the design but the TextArea is not setting up properly with the frame along with the JScroll Pane. I've updated the code and the image also. Thank you.
– J patel
Nov 10 at 21:02
@Jpatel Why do I have to keep repeating myself?. I have already told you the scroll pane should be added to the frame. Why are you creating extra components??? Did you not read the tutorial yet? It explains how the BorderLayout works.
– camickr
Nov 10 at 21:11
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The problem is you are over using the GridLayout.
I would suggest you want to keep using the default layout manager of the frame which is a BorderLayout
.
Then you would do the following:
Create a panel using a
GridLayout
for the two buttons. Add this panel to theBorderLayout.PAGE_START
of the frame.Add your scroll pane containing the text area to the
BorderLayout.CENTER
of the frame.Create a panel using the
GridLayout
for the buttons on the bottom. Add this panel to theBorderLayout.PAGE_END
of the frame.
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples of the Borderlayout
and GridLayout
.
The problem is you are over using the GridLayout.
I would suggest you want to keep using the default layout manager of the frame which is a BorderLayout
.
Then you would do the following:
Create a panel using a
GridLayout
for the two buttons. Add this panel to theBorderLayout.PAGE_START
of the frame.Add your scroll pane containing the text area to the
BorderLayout.CENTER
of the frame.Create a panel using the
GridLayout
for the buttons on the bottom. Add this panel to theBorderLayout.PAGE_END
of the frame.
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples of the Borderlayout
and GridLayout
.
edited Nov 10 at 19:47
answered Nov 10 at 19:25
camickr
272k14124237
272k14124237
so I tried what u told me to and came up with the following code. It still doesn't match the design. The code has been updated.
– J patel
Nov 10 at 19:33
I said that you were overusing the GridLayout. Why did you create a panel "p" with a GridLayout?. I suggested (3 times) that you add the components to the frame (not the panel), using the BorderLayout constraint. Get rid of the panel you called "p". Also, I suggested you should be adding the scroll pane directly to the frame, not to a panel first. Take the time to read the tutorial and understand how the layout managers work. Then think logically about breaking down your layout to take advantage of the layout features of different layout managers to achieve your desired layout.
– camickr
Nov 10 at 19:52
Okay so I was able to match the design but the TextArea is not setting up properly with the frame along with the JScroll Pane. I've updated the code and the image also. Thank you.
– J patel
Nov 10 at 21:02
@Jpatel Why do I have to keep repeating myself?. I have already told you the scroll pane should be added to the frame. Why are you creating extra components??? Did you not read the tutorial yet? It explains how the BorderLayout works.
– camickr
Nov 10 at 21:11
add a comment |
so I tried what u told me to and came up with the following code. It still doesn't match the design. The code has been updated.
– J patel
Nov 10 at 19:33
I said that you were overusing the GridLayout. Why did you create a panel "p" with a GridLayout?. I suggested (3 times) that you add the components to the frame (not the panel), using the BorderLayout constraint. Get rid of the panel you called "p". Also, I suggested you should be adding the scroll pane directly to the frame, not to a panel first. Take the time to read the tutorial and understand how the layout managers work. Then think logically about breaking down your layout to take advantage of the layout features of different layout managers to achieve your desired layout.
– camickr
Nov 10 at 19:52
Okay so I was able to match the design but the TextArea is not setting up properly with the frame along with the JScroll Pane. I've updated the code and the image also. Thank you.
– J patel
Nov 10 at 21:02
@Jpatel Why do I have to keep repeating myself?. I have already told you the scroll pane should be added to the frame. Why are you creating extra components??? Did you not read the tutorial yet? It explains how the BorderLayout works.
– camickr
Nov 10 at 21:11
so I tried what u told me to and came up with the following code. It still doesn't match the design. The code has been updated.
– J patel
Nov 10 at 19:33
so I tried what u told me to and came up with the following code. It still doesn't match the design. The code has been updated.
– J patel
Nov 10 at 19:33
I said that you were overusing the GridLayout. Why did you create a panel "p" with a GridLayout?. I suggested (3 times) that you add the components to the frame (not the panel), using the BorderLayout constraint. Get rid of the panel you called "p". Also, I suggested you should be adding the scroll pane directly to the frame, not to a panel first. Take the time to read the tutorial and understand how the layout managers work. Then think logically about breaking down your layout to take advantage of the layout features of different layout managers to achieve your desired layout.
– camickr
Nov 10 at 19:52
I said that you were overusing the GridLayout. Why did you create a panel "p" with a GridLayout?. I suggested (3 times) that you add the components to the frame (not the panel), using the BorderLayout constraint. Get rid of the panel you called "p". Also, I suggested you should be adding the scroll pane directly to the frame, not to a panel first. Take the time to read the tutorial and understand how the layout managers work. Then think logically about breaking down your layout to take advantage of the layout features of different layout managers to achieve your desired layout.
– camickr
Nov 10 at 19:52
Okay so I was able to match the design but the TextArea is not setting up properly with the frame along with the JScroll Pane. I've updated the code and the image also. Thank you.
– J patel
Nov 10 at 21:02
Okay so I was able to match the design but the TextArea is not setting up properly with the frame along with the JScroll Pane. I've updated the code and the image also. Thank you.
– J patel
Nov 10 at 21:02
@Jpatel Why do I have to keep repeating myself?. I have already told you the scroll pane should be added to the frame. Why are you creating extra components??? Did you not read the tutorial yet? It explains how the BorderLayout works.
– camickr
Nov 10 at 21:11
@Jpatel Why do I have to keep repeating myself?. I have already told you the scroll pane should be added to the frame. Why are you creating extra components??? Did you not read the tutorial yet? It explains how the BorderLayout works.
– camickr
Nov 10 at 21:11
add a comment |
up vote
0
down vote
camickr's answer is optimal here.
Here is an SSCCE:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class CalculatorFrame extends JFrame {
public CalculatorFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(createClearPanel(), BorderLayout.PAGE_START);
getContentPane().add(createTextArea(), BorderLayout.CENTER);
getContentPane().add(createNumberPanels(), BorderLayout.PAGE_END);
setSize(300, 300);
pack();
}
private JPanel createNumberPanels() {
JPanel main = new JPanel();
main.setLayout(new GridLayout(4, 0));
for (int i = 0; i < 16; i++) {
JButton button = new JButton("" + i);
main.add(button);
}
return main;
}
private JScrollPane createTextArea() {
JTextArea area = new JTextArea(5, 10);
JScrollPane sp = new JScrollPane(area);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return sp;
}
private JPanel createClearPanel() {
JButton clearAll = new JButton("clear all");
JButton clearLast = new JButton("Clear last");
JPanel panel = new JPanel(new GridLayout(1, 0));
panel.add(clearLast);
panel.add(clearAll);
return panel;
}
public static void main(String args) {
SwingUtilities.invokeLater(() -> new CalculatorFrame().setVisible(true));
}
}
Preview:
P.S: Ignore the fact i used only numbers instead of operators like *,?,+,= etc.
Thank you so much the help.
– J patel
Nov 10 at 21:11
@J patel No problem. camickr had already help you. I just did the SSCCE, after i saw you still having problem to get to the point.
– George Zougianos
Nov 10 at 21:12
1) there is no need to set the layout for the content pane. The default layout is the BorderLayout as I stated in my answer. 2) there is no need to use getContentPane(). When you add a component to the frame it will be added to the content pane for you. 3) When you create a text area you should usenew JTextArea(5, 10)
to suggest the row/columns of the text area so the text area can calculate its preferred size. 4) use pack() so the frame can be sized at its preferred size.
– camickr
Nov 10 at 21:16
@camickr 1) I always prefer to have the layout of the container in front of my eyes. Plus, this question is all about border layout. 2) Before java 1.5 (i think) you had to use ContentPane, it is just habbit i have inherited from old docs/tutorials. 3) & 4) Editing right now, i thought i pack(), which would show me the cols & rows of the textarea. Thanks for noticing :)
– George Zougianos
Nov 10 at 21:21
add a comment |
up vote
0
down vote
camickr's answer is optimal here.
Here is an SSCCE:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class CalculatorFrame extends JFrame {
public CalculatorFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(createClearPanel(), BorderLayout.PAGE_START);
getContentPane().add(createTextArea(), BorderLayout.CENTER);
getContentPane().add(createNumberPanels(), BorderLayout.PAGE_END);
setSize(300, 300);
pack();
}
private JPanel createNumberPanels() {
JPanel main = new JPanel();
main.setLayout(new GridLayout(4, 0));
for (int i = 0; i < 16; i++) {
JButton button = new JButton("" + i);
main.add(button);
}
return main;
}
private JScrollPane createTextArea() {
JTextArea area = new JTextArea(5, 10);
JScrollPane sp = new JScrollPane(area);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return sp;
}
private JPanel createClearPanel() {
JButton clearAll = new JButton("clear all");
JButton clearLast = new JButton("Clear last");
JPanel panel = new JPanel(new GridLayout(1, 0));
panel.add(clearLast);
panel.add(clearAll);
return panel;
}
public static void main(String args) {
SwingUtilities.invokeLater(() -> new CalculatorFrame().setVisible(true));
}
}
Preview:
P.S: Ignore the fact i used only numbers instead of operators like *,?,+,= etc.
Thank you so much the help.
– J patel
Nov 10 at 21:11
@J patel No problem. camickr had already help you. I just did the SSCCE, after i saw you still having problem to get to the point.
– George Zougianos
Nov 10 at 21:12
1) there is no need to set the layout for the content pane. The default layout is the BorderLayout as I stated in my answer. 2) there is no need to use getContentPane(). When you add a component to the frame it will be added to the content pane for you. 3) When you create a text area you should usenew JTextArea(5, 10)
to suggest the row/columns of the text area so the text area can calculate its preferred size. 4) use pack() so the frame can be sized at its preferred size.
– camickr
Nov 10 at 21:16
@camickr 1) I always prefer to have the layout of the container in front of my eyes. Plus, this question is all about border layout. 2) Before java 1.5 (i think) you had to use ContentPane, it is just habbit i have inherited from old docs/tutorials. 3) & 4) Editing right now, i thought i pack(), which would show me the cols & rows of the textarea. Thanks for noticing :)
– George Zougianos
Nov 10 at 21:21
add a comment |
up vote
0
down vote
up vote
0
down vote
camickr's answer is optimal here.
Here is an SSCCE:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class CalculatorFrame extends JFrame {
public CalculatorFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(createClearPanel(), BorderLayout.PAGE_START);
getContentPane().add(createTextArea(), BorderLayout.CENTER);
getContentPane().add(createNumberPanels(), BorderLayout.PAGE_END);
setSize(300, 300);
pack();
}
private JPanel createNumberPanels() {
JPanel main = new JPanel();
main.setLayout(new GridLayout(4, 0));
for (int i = 0; i < 16; i++) {
JButton button = new JButton("" + i);
main.add(button);
}
return main;
}
private JScrollPane createTextArea() {
JTextArea area = new JTextArea(5, 10);
JScrollPane sp = new JScrollPane(area);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return sp;
}
private JPanel createClearPanel() {
JButton clearAll = new JButton("clear all");
JButton clearLast = new JButton("Clear last");
JPanel panel = new JPanel(new GridLayout(1, 0));
panel.add(clearLast);
panel.add(clearAll);
return panel;
}
public static void main(String args) {
SwingUtilities.invokeLater(() -> new CalculatorFrame().setVisible(true));
}
}
Preview:
P.S: Ignore the fact i used only numbers instead of operators like *,?,+,= etc.
camickr's answer is optimal here.
Here is an SSCCE:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class CalculatorFrame extends JFrame {
public CalculatorFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(createClearPanel(), BorderLayout.PAGE_START);
getContentPane().add(createTextArea(), BorderLayout.CENTER);
getContentPane().add(createNumberPanels(), BorderLayout.PAGE_END);
setSize(300, 300);
pack();
}
private JPanel createNumberPanels() {
JPanel main = new JPanel();
main.setLayout(new GridLayout(4, 0));
for (int i = 0; i < 16; i++) {
JButton button = new JButton("" + i);
main.add(button);
}
return main;
}
private JScrollPane createTextArea() {
JTextArea area = new JTextArea(5, 10);
JScrollPane sp = new JScrollPane(area);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return sp;
}
private JPanel createClearPanel() {
JButton clearAll = new JButton("clear all");
JButton clearLast = new JButton("Clear last");
JPanel panel = new JPanel(new GridLayout(1, 0));
panel.add(clearLast);
panel.add(clearAll);
return panel;
}
public static void main(String args) {
SwingUtilities.invokeLater(() -> new CalculatorFrame().setVisible(true));
}
}
Preview:
P.S: Ignore the fact i used only numbers instead of operators like *,?,+,= etc.
edited Nov 10 at 21:22
answered Nov 10 at 21:05
George Zougianos
359210
359210
Thank you so much the help.
– J patel
Nov 10 at 21:11
@J patel No problem. camickr had already help you. I just did the SSCCE, after i saw you still having problem to get to the point.
– George Zougianos
Nov 10 at 21:12
1) there is no need to set the layout for the content pane. The default layout is the BorderLayout as I stated in my answer. 2) there is no need to use getContentPane(). When you add a component to the frame it will be added to the content pane for you. 3) When you create a text area you should usenew JTextArea(5, 10)
to suggest the row/columns of the text area so the text area can calculate its preferred size. 4) use pack() so the frame can be sized at its preferred size.
– camickr
Nov 10 at 21:16
@camickr 1) I always prefer to have the layout of the container in front of my eyes. Plus, this question is all about border layout. 2) Before java 1.5 (i think) you had to use ContentPane, it is just habbit i have inherited from old docs/tutorials. 3) & 4) Editing right now, i thought i pack(), which would show me the cols & rows of the textarea. Thanks for noticing :)
– George Zougianos
Nov 10 at 21:21
add a comment |
Thank you so much the help.
– J patel
Nov 10 at 21:11
@J patel No problem. camickr had already help you. I just did the SSCCE, after i saw you still having problem to get to the point.
– George Zougianos
Nov 10 at 21:12
1) there is no need to set the layout for the content pane. The default layout is the BorderLayout as I stated in my answer. 2) there is no need to use getContentPane(). When you add a component to the frame it will be added to the content pane for you. 3) When you create a text area you should usenew JTextArea(5, 10)
to suggest the row/columns of the text area so the text area can calculate its preferred size. 4) use pack() so the frame can be sized at its preferred size.
– camickr
Nov 10 at 21:16
@camickr 1) I always prefer to have the layout of the container in front of my eyes. Plus, this question is all about border layout. 2) Before java 1.5 (i think) you had to use ContentPane, it is just habbit i have inherited from old docs/tutorials. 3) & 4) Editing right now, i thought i pack(), which would show me the cols & rows of the textarea. Thanks for noticing :)
– George Zougianos
Nov 10 at 21:21
Thank you so much the help.
– J patel
Nov 10 at 21:11
Thank you so much the help.
– J patel
Nov 10 at 21:11
@J patel No problem. camickr had already help you. I just did the SSCCE, after i saw you still having problem to get to the point.
– George Zougianos
Nov 10 at 21:12
@J patel No problem. camickr had already help you. I just did the SSCCE, after i saw you still having problem to get to the point.
– George Zougianos
Nov 10 at 21:12
1) there is no need to set the layout for the content pane. The default layout is the BorderLayout as I stated in my answer. 2) there is no need to use getContentPane(). When you add a component to the frame it will be added to the content pane for you. 3) When you create a text area you should use
new JTextArea(5, 10)
to suggest the row/columns of the text area so the text area can calculate its preferred size. 4) use pack() so the frame can be sized at its preferred size.– camickr
Nov 10 at 21:16
1) there is no need to set the layout for the content pane. The default layout is the BorderLayout as I stated in my answer. 2) there is no need to use getContentPane(). When you add a component to the frame it will be added to the content pane for you. 3) When you create a text area you should use
new JTextArea(5, 10)
to suggest the row/columns of the text area so the text area can calculate its preferred size. 4) use pack() so the frame can be sized at its preferred size.– camickr
Nov 10 at 21:16
@camickr 1) I always prefer to have the layout of the container in front of my eyes. Plus, this question is all about border layout. 2) Before java 1.5 (i think) you had to use ContentPane, it is just habbit i have inherited from old docs/tutorials. 3) & 4) Editing right now, i thought i pack(), which would show me the cols & rows of the textarea. Thanks for noticing :)
– George Zougianos
Nov 10 at 21:21
@camickr 1) I always prefer to have the layout of the container in front of my eyes. Plus, this question is all about border layout. 2) Before java 1.5 (i think) you had to use ContentPane, it is just habbit i have inherited from old docs/tutorials. 3) & 4) Editing right now, i thought i pack(), which would show me the cols & rows of the textarea. Thanks for noticing :)
– George Zougianos
Nov 10 at 21:21
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242564%2fgui-calculator-using-jframes-and-layouts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
See also this calculator example. It uses
ScriptEngine
to evaluate the expression in the text field.– Andrew Thompson
Nov 10 at 22:16