The following code creates a calculator but it is not the correct color. What do
ID: 3527449 • Letter: T
Question
The following code creates a calculator but it is not the correct color. What do I change to make it a checkerboard pattern?
import javax.swing.*;
import java.awt.*;
public class myCalculator extends JFrame{
private static final long serialVersionUID = 1L;
private Color lightCyan;
private Font font1;
public myCalculator()
{
// Call the super class JFrame and set the title of the window to My Calculator Class
super.setTitle(" My Calculator Class");
Color paleCyan = new Color(109, 207, 246);
setLightCyan(new Color(68, 140, 203));
//Set the font type and style
setFont1(new Font("SansSerif", Font.BOLD, 16));
// In a real calculator these is where your calculations appear!
JTextField jtext = new JTextField(" 0 ");
jtext.setSize(250,25);
jtext.setHorizontalAlignment(JTextField.RIGHT);
JPanel textPanel = new JPanel(new BorderLayout());
/* Main Panel holding the JText field for the calculations area is used with the BorderLayout because the NORTH portion stretches
the JTextfield Horizontal (side-to-side) */
textPanel.add(jtext, BorderLayout.NORTH);
JPanel buttonsPanel = new JPanel(new GridLayout(4,4,5,5));
buttonsPanel.setBackground(paleCyan);
JButton jTwo = new JButton("2");
JButton jOne = new JButton("1");
JButton jThree = new JButton("3");
JButton jFour = new JButton("4");
JButton jFive = new JButton("5");
JButton jSix = new JButton("6");
JButton jSeven = new JButton("7");
JButton jEight = new JButton("8");
JButton jNine = new JButton("9");
JButton jZero = new JButton("0");
JButton jDot = new JButton(".");
JButton jEqual = new JButton("=");
JButton jMultiply = new JButton("*");
JButton jDivide = new JButton("/");
JButton jMinus = new JButton("-");
JButton jAdd = new JButton("+");
buttonsPanel.add(jSeven);
buttonsPanel.add(jEight);
buttonsPanel.add(jNine);
buttonsPanel.add(jMultiply);
buttonsPanel.add(jFour);
buttonsPanel.add(jFive);
buttonsPanel.add(jSix);
buttonsPanel.add(jDivide);
buttonsPanel.add(jOne);
buttonsPanel.add(jTwo);
buttonsPanel.add(jThree);
buttonsPanel.add(jMinus);
buttonsPanel.add(jDot);
buttonsPanel.add(jZero);
buttonsPanel.add(jEqual);
buttonsPanel.add(jAdd);
JPanel mainPanel = new JPanel(new BorderLayout());
//Final step add the panels to the Frame (Container)
mainPanel.add(textPanel, BorderLayout.NORTH);
mainPanel.add(buttonsPanel, BorderLayout.CENTER);
add(mainPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
myCalculator frame = new myCalculator();
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable( false );
frame.setVisible(true);
}
public Font getFont1() {
return font1;
}
public void setFont1(Font font1) {
this.font1 = font1;
}
public Color getLightCyan() {
return lightCyan;
}
public void setLightCyan(Color lightCyan) {
this.lightCyan = lightCyan;
}
}
Need to change color from of first picture to the second.
Explanation / Answer
//Rate me first then only I can get points
import javax.swing.*;
import java.awt.*;
public class myCalculator extends JFrame{
private static final long serialVersionUID = 1L;
private Color lightCyan;
private Font font1;
//------code by Ravi-----------------
public myCalculator()
{
// Call the super class JFrame and set the title of the window to My Calculator Class
super.setTitle(" My Calculator Class");
//Color paleCyan = new Color(109, 207, 246);
//setLightCyan(new Color(68, 140, 203));
//Set the font type and style
//------code by Ravi-----------------
setFont1(new Font("SansSerif", Font.BOLD, 16));
// In a real calculator these is where your calculations appear!
JTextField jtext = new JTextField(" 0 ");
jtext.setSize(250,25);
jtext.setHorizontalAlignment(JTextField.RIGHT);
JPanel textPanel = new JPanel(new BorderLayout());
/* Main Panel holding the JText field for the calculations area is used with the BorderLayout because the NORTH portion stretches
the JTextfield Horizontal (side-to-side) */
textPanel.add(jtext, BorderLayout.NORTH);
JPanel buttonsPanel = new JPanel(new GridLayout(4,4,5,5));
buttonsPanel.setBackground(Color.lightGray);
//------code by Ravi-----------------
JButton jTwo = new JButton("2");
jTwo.setBackground(Color.black);
jTwo.setForeground(Color.white);
JButton jOne = new JButton("1");
jOne.setBackground(Color.white);
jOne.setForeground(Color.black);
JButton jThree = new JButton("3");
jThree.setBackground(Color.white);
jThree.setForeground(Color.black);
JButton jFour = new JButton("4");
jFour.setBackground(Color.black);
jFour.setForeground(Color.white);
JButton jFive = new JButton("5");
jFive.setBackground(Color.white);
jFive.setForeground(Color.black);
JButton jSix = new JButton("6");
jSix.setBackground(Color.black);
jSix.setForeground(Color.white);
JButton jSeven = new JButton("7");
jSeven.setBackground(Color.white);
jSeven.setForeground(Color.black);
JButton jEight = new JButton("8");
jEight.setBackground(Color.black);
jEight.setForeground(Color.white);
JButton jNine = new JButton("9");
jNine.setBackground(Color.white);
jNine.setForeground(Color.black);
JButton jZero = new JButton("0");
jZero.setBackground(Color.black);
jZero.setForeground(Color.white);
JButton jDot = new JButton(".");
jDot.setBackground(Color.white);
jDot.setForeground(Color.black);
JButton jEqual = new JButton("=");
jEqual.setBackground(Color.black);
jEqual.setForeground(Color.white);
JButton jMultiply = new JButton("*");
jMultiply.setBackground(Color.white);
jMultiply.setForeground(Color.black);
JButton jDivide = new JButton("/");
jDivide.setBackground(Color.black);
jDivide.setForeground(Color.white);
JButton jMinus = new JButton("-");
jMinus.setBackground(Color.black);
jMinus.setForeground(Color.white);
JButton jAdd = new JButton("+");
jAdd.setBackground(Color.white);
jAdd.setForeground(Color.black);
buttonsPanel.add(jSeven);
buttonsPanel.add(jEight);
buttonsPanel.add(jNine);
buttonsPanel.add(jDivide);
buttonsPanel.add(jFour);
buttonsPanel.add(jFive);
buttonsPanel.add(jSix);
buttonsPanel.add(jMultiply);
buttonsPanel.add(jOne);
buttonsPanel.add(jTwo);
buttonsPanel.add(jThree);
buttonsPanel.add(jMinus);
buttonsPanel.add(jZero);
buttonsPanel.add(jDot);
buttonsPanel.add(jEqual);
buttonsPanel.add(jAdd);
//------code by Ravi-----------------
JPanel mainPanel = new JPanel(new BorderLayout());
//Final step add the panels to the Frame (Container)
mainPanel.add(textPanel, BorderLayout.NORTH);
mainPanel.add(buttonsPanel, BorderLayout.CENTER);
add(mainPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
myCalculator frame = new myCalculator();
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable( false );
frame.setVisible(true);
}
public Font getFont1() {
return font1;
}
public void setFont1(Font font1) {
this.font1 = font1;
}
public Color getLightCyan() {
return lightCyan;
}
//------code by Ravi-----------------
public void setLightCyan(Color lightCyan) {
this.lightCyan = lightCyan;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.