GRAEVEN YURI VIDEZ :
I am creating a GUI-based Calculator program. I've separated the areas into panels, which have the ff purposes:
- inputPanel: Where users will input numbers;
- numericPanel: Panel that holds the digits 0-9
- operatorPanel: Panel that holds operator symbols(+, -, *, /)
- formatPanel: Panel that will hold CLEAR, ENTER, and EXIT buttons
I added all these panels to the frame before making it visible, but when I press play, nothing shows up except for the background color. Should I be formatting them a different way?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Flow;
/**
* @author Graeven Yuri Videz
* This is a program that simulates a simple calculator with arithmetic functions.
*/
public class calculator extends Frame implements ActionListener{
private static JTextField firstField= new JTextField(25);
private static JTextField num2 = new JTextField(25);
//------------------------------------------------------------------------------------
private static JButton b0 = new JButton("0");
private static JButton b1 = new JButton("1");
private static JButton b2 = new JButton("2");
private static JButton b3 = new JButton("3");
private static JButton b4 = new JButton("4");
private static JButton b5 = new JButton("5");
private static JButton b6 = new JButton("6");
private static JButton b7 = new JButton("7");
private static JButton b8 = new JButton("8");
private static JButton b9 = new JButton("9");
//-----------------------------------------------------------------------------------
private static JButton enterButton = new JButton("Enter");
private static JButton clearButton = new JButton("Clear");
private static JButton exitButton = new JButton("Close");
private static JButton addB = new JButton("+");
private static JButton subB = new JButton("-");
private static JButton multiB = new JButton("x");
private static JButton divideB = new JButton("÷");
private static JPanel inputPanel;
private static JPanel numericPanel;
private static JPanel operatorPanel;
private static JPanel formatPanel;
private static JLabel resultLabel;
private static JLabel nameLabel = new JLabel("Created by: Yuri Videz");
//-----------------------------------------------------------------------------------
public void setInputPanel(JPanel panel) {
panel.setLayout(new FlowLayout());
JLabel firstNum = new JLabel("Enter your number:");
panel.add(firstNum);
panel.add(firstField);
}
public void setNumericPanel(JPanel panel) {
panel.setLayout(new GridLayout(6, 3));
panel.add(b0);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
}
public void setOperatorPanel(JPanel panel) {
JPanel operatorPanel = new JPanel();
//operatorPanel.setLayout(new FlowLayout());
panel.add(addB);
panel.add(subB);
panel.add(multiB);
panel.add(divideB);
}
public void setFormatPanel(JPanel panel) {
JPanel formatPanel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(enterButton);
panel.add(clearButton);
}
public calculator() {
inputPanel = new JPanel();
setInputPanel(inputPanel);
numericPanel = new JPanel();
setNumericPanel(numericPanel);
operatorPanel = new JPanel();
setOperatorPanel(operatorPanel);
formatPanel = new JPanel();
setFormatPanel(formatPanel);
setTitle("Calculator Program");
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new GridLayout(3, 3));
frame.add(nameLabel);
frame.add(inputPanel);
frame.add(numericPanel);
frame.add(operatorPanel);
frame.add(formatPanel);
setBackground(Color.gray);
setSize(350, 500);
frame.pack();
setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
calculator calcuObject = new calculator();
}
@Override
public void actionPerformed(ActionEvent ac) {
}
}
c0der :
Side notes:
1. see Java naming conventions
2. Follow this link for a refactored version of your code
You have two different JFrame
instances in your code.
You add all components to JFrame frame = new JFrame();
but you make the other one visible by setVisible(true);
Solution:
change setVisible(true);
to frame.setVisible(true);
There is no need for calculator to extend JFrame
.
Side notes:
1. see Java naming conventions
2. Follow this link for a refactored version of your code
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=291180&siteId=1