NOTE: Use JAVA code only please. Questions: I have built a GUI interface in JAVA
ID: 3640333 • Letter: N
Question
NOTE: Use JAVA code only please.Questions: I have built a GUI interface in JAVA for a simulated calculator. I have gotten all the buttons in the proper locations and color coded accordingly but i can't seem to get the text field at the top to show up properly. I used all grid layout to set up the buttons and am trying to set up one border layout set to North for the Text field that will display input and output. Note the code that is commented out is the text field code but when it is used it displays the text field correctly at the top but eliminated all the buttons except the addition ( you may want to run code with and without commented out section to see the problem). I believe the border layout is overriding my grid layout and forcing all the buttons to overlap until it gets to the final one (the addition button) and it displays it, i could use some help here guys and sorry if this is a bad explanation of the problem it may be easier for you to copy and paste code and run to see the problem yourself.
CODE BELOW:
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Calculator extends JFrame
{
public Calculator(){
setTitle("Calculator");
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(6,4));
/*
**THIS CODE IS NOT FUNCTIONING PROPERLY****
setLayout(new BorderLayout());
JTextField displayCalc = new JTextField(10);
JPanel panel25 = new JPanel();
panel25.add(displayCalc);
add(panel25, BorderLayout.NORTH);
*/
//create 24 buttons
//column 1 buttons and color
JButton sqrRoot = new JButton("SQRT");
sqrRoot.setForeground(Color.WHITE); sqrRoot.setBackground(Color.DARK_GRAY);
JButton MC = new JButton("MC");
MC.setForeground(Color.WHITE); MC.setBackground(Color.DARK_GRAY);
JButton num7 = new JButton("7");
num7.setForeground(Color.WHITE); num7.setBackground(Color.BLACK);
JButton num4 = new JButton("4");
num4.setForeground(Color.WHITE); num4.setBackground(Color.BLACK);
JButton num1 = new JButton("1");
num1.setForeground(Color.WHITE); num1.setBackground(Color.BLACK);
JButton num0 = new JButton("0");
num0.setForeground(Color.WHITE); num0.setBackground(Color.BLACK);
//column 2 buttons and color
JButton percent = new JButton ("%");
percent.setForeground(Color.WHITE); percent.setBackground(Color.DARK_GRAY);
JButton MR = new JButton ("MR");
MR.setForeground(Color.WHITE); MR.setBackground(Color.DARK_GRAY);
JButton num8 = new JButton("8");
num8.setForeground(Color.WHITE); num8.setBackground(Color.BLACK);
JButton num5 = new JButton("5");
num5.setForeground(Color.WHITE); num5.setBackground(Color.BLACK);
JButton num2 = new JButton("2");
num2.setForeground(Color.WHITE); num2.setBackground(Color.BLACK);
JButton decSymbol = new JButton(".");
decSymbol.setForeground(Color.WHITE); decSymbol.setBackground(Color.BLACK);
//column 3 buttons and color
JButton backspace = new JButton ("BKSP");
backspace.setForeground(Color.WHITE); backspace.setBackground(Color.DARK_GRAY);
JButton mNeg = new JButton ("M-");
mNeg.setForeground(Color.WHITE); mNeg.setBackground(Color.DARK_GRAY);
JButton num9 = new JButton("9");
num9.setForeground(Color.WHITE); num9.setBackground(Color.BLACK);
JButton num6 = new JButton("6");
num6.setForeground(Color.WHITE); num6.setBackground(Color.BLACK);
JButton num3 = new JButton("3");
num3.setForeground(Color.WHITE); num3.setBackground(Color.BLACK);
JButton equalsSymbol = new JButton("=");
equalsSymbol.setForeground(Color.WHITE); equalsSymbol.setBackground(Color.GRAY);
//column 4 buttons and color
JButton C = new JButton("C");
C.setForeground(Color.WHITE); C.setBackground(Color.DARK_GRAY);
JButton mPos = new JButton("M+");
mPos.setForeground(Color.WHITE); mPos.setBackground(Color.DARK_GRAY);
JButton division = new JButton("/");
division.setForeground(Color.WHITE); division.setBackground(Color.DARK_GRAY);
JButton multiplication = new JButton("x");
multiplication.setForeground(Color.WHITE); multiplication.setBackground(Color.DARK_GRAY);
JButton subtraction = new JButton("-");
subtraction.setForeground(Color.WHITE); subtraction.setBackground(Color.DARK_GRAY);
JButton addition = new JButton("+");
addition.setForeground(Color.WHITE); addition.setBackground(Color.DARK_GRAY);
//Create 24 panels
JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel(); JPanel panel5 = new JPanel(); JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel(); JPanel panel8 = new JPanel(); JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel12 = new JPanel();
JPanel panel13 = new JPanel(); JPanel panel14 = new JPanel(); JPanel panel15 = new JPanel();
JPanel panel16 = new JPanel(); JPanel panel17 = new JPanel(); JPanel panel18 = new JPanel();
JPanel panel19 = new JPanel(); JPanel panel20 = new JPanel(); JPanel panel21= new JPanel();
JPanel panel22 = new JPanel(); JPanel panel23 = new JPanel(); JPanel panel24 = new JPanel();
//Add buttons to corresponding panels
panel1.add(sqrRoot); panel2.add(percent); panel3.add(backspace); panel4.add(C);
panel5.add(MC); panel6.add(MR); panel7.add(mNeg); panel8.add(mPos); panel9.add(num7);
panel10.add(num8); panel11.add(num9); panel12.add(division); panel13.add(num4);
panel14.add(num5); panel15.add(num6); panel16.add(multiplication); panel17.add(num1);
panel18.add(num2); panel19.add(num3); panel20.add(subtraction); panel21.add(num0);
panel22.add(decSymbol); panel23.add(equalsSymbol); panel24.add(addition);
//add panels to window
add(panel1); add(panel2); add(panel3); add(panel4); add(panel5); add(panel6); add(panel7);
add(panel8); add(panel9); add(panel10); add(panel11); add(panel12); add(panel13); add(panel14);
add(panel15); add(panel16); add(panel17); add(panel18); add(panel19); add(panel20); add(panel21);
add(panel22); add(panel23); add(panel24);
//setColors for panel background
panel1.setBackground(Color.LIGHT_GRAY); panel2.setBackground(Color.LIGHT_GRAY); panel3.setBackground(Color.LIGHT_GRAY);
panel4.setBackground(Color.LIGHT_GRAY); panel5.setBackground(Color.LIGHT_GRAY); panel6.setBackground(Color.LIGHT_GRAY);
panel7.setBackground(Color.LIGHT_GRAY); panel8.setBackground(Color.LIGHT_GRAY); panel9.setBackground(Color.LIGHT_GRAY);
panel10.setBackground(Color.LIGHT_GRAY); panel11.setBackground(Color.LIGHT_GRAY); panel12.setBackground(Color.LIGHT_GRAY);
panel13.setBackground(Color.LIGHT_GRAY); panel14.setBackground(Color.LIGHT_GRAY); panel15.setBackground(Color.LIGHT_GRAY);
panel16.setBackground(Color.LIGHT_GRAY); panel17.setBackground(Color.LIGHT_GRAY); panel18.setBackground(Color.LIGHT_GRAY);
panel19.setBackground(Color.LIGHT_GRAY); panel20.setBackground(Color.LIGHT_GRAY); panel21.setBackground(Color.LIGHT_GRAY);
panel22.setBackground(Color.LIGHT_GRAY); panel23.setBackground(Color.LIGHT_GRAY); panel24.setBackground(Color.LIGHT_GRAY);
//make visible
setVisible(true);
}
public static void main(String[] Args)
{
new Calculator();
}
}
Explanation / Answer
I made a seperate container for the buttons. import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class Calculator extends JFrame { public Calculator(){ setTitle("Calculator"); setSize(200,250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JTextField displayCalc = new JTextField(10); JPanel panel25 = new JPanel(); panel25.add(displayCalc); add(panel25, BorderLayout.PAGE_START); JPanel bot= new JPanel(); bot.setLayout(new GridLayout(6,4)); //create 24 buttons //column 1 buttons and color JButton sqrRoot = new JButton("SQRT"); sqrRoot.setForeground(Color.WHITE); sqrRoot.setBackground(Color.DARK_GRAY); JButton MC = new JButton("MC"); MC.setForeground(Color.WHITE); MC.setBackground(Color.DARK_GRAY); JButton num7 = new JButton("7"); num7.setForeground(Color.WHITE); num7.setBackground(Color.BLACK); JButton num4 = new JButton("4"); num4.setForeground(Color.WHITE); num4.setBackground(Color.BLACK); JButton num1 = new JButton("1"); num1.setForeground(Color.WHITE); num1.setBackground(Color.BLACK); JButton num0 = new JButton("0"); num0.setForeground(Color.WHITE); num0.setBackground(Color.BLACK); //column 2 buttons and color JButton percent = new JButton ("%"); percent.setForeground(Color.WHITE); percent.setBackground(Color.DARK_GRAY); JButton MR = new JButton ("MR"); MR.setForeground(Color.WHITE); MR.setBackground(Color.DARK_GRAY); JButton num8 = new JButton("8"); num8.setForeground(Color.WHITE); num8.setBackground(Color.BLACK); JButton num5 = new JButton("5"); num5.setForeground(Color.WHITE); num5.setBackground(Color.BLACK); JButton num2 = new JButton("2"); num2.setForeground(Color.WHITE); num2.setBackground(Color.BLACK); JButton decSymbol = new JButton("."); decSymbol.setForeground(Color.WHITE); decSymbol.setBackground(Color.BLACK); //column 3 buttons and color JButton backspace = new JButton ("BKSP"); backspace.setForeground(Color.WHITE); backspace.setBackground(Color.DARK_GRAY); JButton mNeg = new JButton ("M-"); mNeg.setForeground(Color.WHITE); mNeg.setBackground(Color.DARK_GRAY); JButton num9 = new JButton("9"); num9.setForeground(Color.WHITE); num9.setBackground(Color.BLACK); JButton num6 = new JButton("6"); num6.setForeground(Color.WHITE); num6.setBackground(Color.BLACK); JButton num3 = new JButton("3"); num3.setForeground(Color.WHITE); num3.setBackground(Color.BLACK); JButton equalsSymbol = new JButton("="); equalsSymbol.setForeground(Color.WHITE); equalsSymbol.setBackground(Color.GRAY); //column 4 buttons and color JButton C = new JButton("C"); C.setForeground(Color.WHITE); C.setBackground(Color.DARK_GRAY); JButton mPos = new JButton("M+"); mPos.setForeground(Color.WHITE); mPos.setBackground(Color.DARK_GRAY); JButton division = new JButton("/"); division.setForeground(Color.WHITE); division.setBackground(Color.DARK_GRAY); JButton multiplication = new JButton("x"); multiplication.setForeground(Color.WHITE); multiplication.setBackground(Color.DARK_GRAY); JButton subtraction = new JButton("-"); subtraction.setForeground(Color.WHITE); subtraction.setBackground(Color.DARK_GRAY); JButton addition = new JButton("+"); addition.setForeground(Color.WHITE); addition.setBackground(Color.DARK_GRAY); //Create 24 panels JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel4 = new JPanel(); JPanel panel5 = new JPanel(); JPanel panel6 = new JPanel(); JPanel panel7 = new JPanel(); JPanel panel8 = new JPanel(); JPanel panel9 = new JPanel(); JPanel panel10 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel12 = new JPanel(); JPanel panel13 = new JPanel(); JPanel panel14 = new JPanel(); JPanel panel15 = new JPanel(); JPanel panel16 = new JPanel(); JPanel panel17 = new JPanel(); JPanel panel18 = new JPanel(); JPanel panel19 = new JPanel(); JPanel panel20 = new JPanel(); JPanel panel21= new JPanel(); JPanel panel22 = new JPanel(); JPanel panel23 = new JPanel(); JPanel panel24 = new JPanel(); //Add buttons to corresponding panels panel1.add(sqrRoot); panel2.add(percent); panel3.add(backspace); panel4.add(C); panel5.add(MC); panel6.add(MR); panel7.add(mNeg); panel8.add(mPos); panel9.add(num7); panel10.add(num8); panel11.add(num9); panel12.add(division); panel13.add(num4); panel14.add(num5); panel15.add(num6); panel16.add(multiplication); panel17.add(num1); panel18.add(num2); panel19.add(num3); panel20.add(subtraction); panel21.add(num0); panel22.add(decSymbol); panel23.add(equalsSymbol); panel24.add(addition); //add panels to window bot.add(panel1); bot.add(panel2); bot.add(panel3); bot.add(panel4); bot.add(panel5); bot.add(panel6); bot.add(panel7); bot.add(panel8); bot.add(panel9); bot.add(panel10); bot.add(panel11); bot.add(panel12); bot.add(panel13); bot.add(panel14); bot.add(panel15); bot.add(panel16); bot.add(panel17); bot.add(panel18); bot.add(panel19); bot.add(panel20); bot.add(panel21); bot.add(panel22); bot.add(panel23); bot.add(panel24); add(bot); //setColors for panel background panel1.setBackground(Color.LIGHT_GRAY); panel2.setBackground(Color.LIGHT_GRAY); panel3.setBackground(Color.LIGHT_GRAY); panel4.setBackground(Color.LIGHT_GRAY); panel5.setBackground(Color.LIGHT_GRAY); panel6.setBackground(Color.LIGHT_GRAY); panel7.setBackground(Color.LIGHT_GRAY); panel8.setBackground(Color.LIGHT_GRAY); panel9.setBackground(Color.LIGHT_GRAY); panel10.setBackground(Color.LIGHT_GRAY); panel11.setBackground(Color.LIGHT_GRAY); panel12.setBackground(Color.LIGHT_GRAY); panel13.setBackground(Color.LIGHT_GRAY); panel14.setBackground(Color.LIGHT_GRAY); panel15.setBackground(Color.LIGHT_GRAY); panel16.setBackground(Color.LIGHT_GRAY); panel17.setBackground(Color.LIGHT_GRAY); panel18.setBackground(Color.LIGHT_GRAY); panel19.setBackground(Color.LIGHT_GRAY); panel20.setBackground(Color.LIGHT_GRAY); panel21.setBackground(Color.LIGHT_GRAY); panel22.setBackground(Color.LIGHT_GRAY); panel23.setBackground(Color.LIGHT_GRAY); panel24.setBackground(Color.LIGHT_GRAY); //make visible setVisible(true); } public static void main(String[] Args) { new Calculator(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.