frame.setResizable(false);
in my TestCalculator class makes my JFrame for my Calculator class unresizable (not a word), but it adds a few pixels within the frame, on the bottom and right sides... or removes some from a panel or two. i dont know exactly how to word it, but I want to make the frame a fixed size without the extra pixels and junk.
Yes, I did ask on another site, but they could not help me, and this is due tomorrow morning and I've been working on this for about two weeks and just made the calculator work yesterday. I have been googling it all day and night, and can't find a specific solution.
I'm not sure exactly what is causing it to not act right (duh), but it could be anywhere in this code, maybe the order of it, idk.
CALCULATOR CLASS:
public class SimpleArithmeticCalculator extends JPanel implements ActionListener {
private static final int BTN_ONE = 0;
private static final int BTN_TWO = 1;
private static final int BTN_THREE = 2;
private static final int BTN_DIV = 3;
private static final int BTN_FOUR = 4;
private static final int BTN_FIVE = 5;
private static final int BTN_SIX = 6;
private static final int BTN_MULT = 7;
private static final int BTN_SEVEN = 8;
private static final int BTN_EIGHT = 9;
private static final int BTN_NINE = 10;
private static final int BTN_MINUS = 11;
private static final int BTN_ZERO = 12;
private static final int BTN_DECIMAL = 13;
private static final int BTN_POWER = 14;
private static final int BTN_PLUS = 15;
private static final int BTN_CLEAR = 16;
private static final int BTN_PERCENT = 17;
private static final int BTN_BACKSPACE = 18;
private static final int BTN_CALC = 19;
private static final int NUM_BUTTONS = 20;
private static final int OP_MULT = 0;
private static final int OP_DIV = 1;
private static final int OP_PLUS = 2;
private static final int OP_MINUS = 3;
private static final int OP_POWER = 4;
private static final int OP_PERCENT = 5;
private static char[] buttonTexts = {'7' , '8' , '9' , '/' , '4' ,
'5' , '6' , '*' , '1' , '2' ,
'3' , '-' , '0' , '.' , '^' ,
'+' , 'C' , '%' , '<' , '='};
private static final int[] buttonKeys = {
KeyEvent.VK_NUMPAD7 , KeyEvent.VK_NUMPAD8 , KeyEvent.VK_NUMPAD9 , KeyEvent.VK_DIVIDE , KeyEvent.VK_NUMPAD4 ,
KeyEvent.VK_NUMPAD5 , KeyEvent.VK_NUMPAD6 , KeyEvent.VK_MULTIPLY , KeyEvent.VK_NUMPAD1 , KeyEvent.VK_NUMPAD2 ,
KeyEvent.VK_NUMPAD3 , KeyEvent.VK_MINUS , KeyEvent.VK_NUMPAD0 , KeyEvent.VK_STOP, '^',
KeyEvent.VK_PLUS, KeyEvent.VK_C , '%' , KeyEvent.VK_BACK_SPACE , KeyEvent.VK_ENTER
};
private Font font;
private JButton[] buttons;
private JTextField displayText;
public JPanel calcPanel;
private JPanel textPanel;
private JPanel buttonPanel;
private String buffer;
private int op;
private int opStored;
private double lhs;
private double rhs;
private double rhsStored;
private boolean left;
private boolean clear;
private char lastAction;
public SimpleArithmeticCalculator() {
super();
buffer = new String("0.0");
op = -1;
opStored = -1;
lhs = 0.0;
rhs = 0.0;
rhsStored = 0.0;
left = true;
clear = true;
lastAction = '=';
font = Font.decode("Courier PLAIN 24");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
calcPanel = new JPanel();
textPanel = new JPanel();
buttonPanel = new JPanel();
buttonPanel.setForeground(null);
textPanel.setForeground(null);
calcPanel.setForeground(null);
textPanel.setLayout(new GridLayout(1,1,0,0));
buttonPanel.setLayout(new GridLayout(5 , 10 , 10 , 10));
displayText = new JTextField("" , 20);
displayText.setHorizontalAlignment(JTextField.RIGHT);
displayText.setFont(font);
displayText.setEditable(false);
textPanel.add(displayText);
buttons = new JButton[NUM_BUTTONS];
for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
buttons[i] = new JButton("" + buttonTexts[i]);
buttons[i].setMnemonic(buttonKeys[i]);
buttons[i].setFont(font);
buttons[i].setMinimumSize(new Dimension(50,50));
buttons[i].setActionCommand("" + buttonTexts[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
buttons[BTN_POWER].setText("^");
buttons[BTN_PERCENT].setText("%");
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
textPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
calcPanel.setLayout(new BorderLayout());
calcPanel.add(textPanel , BorderLayout.NORTH);
calcPanel.add(buttonPanel , BorderLayout.CENTER);
add(calcPanel);
for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
buttons[i].setMaximumSize(buttons[i].getSize());
}
}
public void SetColors(Color bg , Color textbg , Color textcolor) {
calcPanel.setBackground(bg);
calcPanel.setVisible(true);
calcPanel.setOpaque(false);
buttonPanel.setBackground(bg);
buttonPanel.setVisible(true);
buttonPanel.setOpaque(false);
textPanel.setBackground(bg);
textPanel.setOpaque(false);
textPanel.setVisible(true);
displayText.setBackground(textbg);
displayText.setForeground(textcolor);
for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
buttons[i].setForeground(textcolor);
}
}
public void actionPerformed(ActionEvent ev) {
String action = ev.getActionCommand();
char c = action.charAt(0);
boolean calc = false;
if (c == buttonTexts[BTN_CLEAR]) {
buffer = "";
left = true;
}
else if (c == buttonTexts[BTN_BACKSPACE]) {
if (buffer.length() > 0) {
buffer = buffer.substring(0 , buffer.length() - 1);
}
if (lastAction == buttonTexts[BTN_CALC]) {
left = true;
}
clear = false;
}
else if ((c == buttonTexts[BTN_CALC]) || isOpCharacter(c)) {
int opCurrent = op;
if (c == buttonTexts[BTN_CALC]) {
if (!left) {
calc = true;
if (isOpCharacter(lastAction)) {
rhs = GetBufferValue();
rhsStored = rhs;
opCurrent = op;
opStored = op;
}
else if (lastAction == buttonTexts[BTN_CALC]) {
rhs = rhsStored;
opCurrent = opStored;
}
else {
rhs = GetBufferValue();
rhsStored = rhs;
opCurrent = op;
opStored = op;
}
}
}
else if (isOpCharacter(c)) {
opStored = op;
if (c == buttonTexts[BTN_MULT]) {op = OP_MULT;}
if (c == buttonTexts[BTN_DIV]) {op = OP_DIV;}
if (c == buttonTexts[BTN_PLUS]) {op = OP_PLUS;}
if (c == buttonTexts[BTN_MINUS]) {op = OP_MINUS;}
if (c == buttonTexts[BTN_POWER]) {op = OP_POWER;}
if (c == buttonTexts[BTN_PERCENT]) {op = OP_PERCENT;}
if (lastAction == buttonTexts[BTN_CALC]) {
lhs = GetBufferValue();
clear = true;
}
else if (isOpCharacter(lastAction)) {
}
else {
if (left) {
lhs = GetBufferValue();
left = false;
}
else {
calc = true;
rhs = GetBufferValue();
opCurrent = opStored;
opStored = op;
}
clear = true;
}
}
if (calc) {
double result = 0.0;
if (opCurrent == OP_MULT) {result = lhs * rhs;}
if (opCurrent == OP_DIV) {result = lhs / rhs;}
if (opCurrent == OP_PLUS) {result = lhs + rhs;}
if (opCurrent == OP_MINUS) {result = lhs - rhs;}
if (opCurrent == OP_POWER) {result = Math.pow(lhs,rhs);}
if (opCurrent == OP_PERCENT) {result = lhs / 100;}
lhs = result;
buffer = "" + result;
clear = true;
}
}
else {
if (lastAction == buttonTexts[BTN_CALC]) {
left = true;
}
if (clear) {
buffer = "";
clear = false;
}
if (c == buttonTexts[BTN_DECIMAL]) {
if (buffer.indexOf('.') == -1) {
if (buffer.length() == 0 || (buffer.length() == 1 && buffer.charAt(0) == '-')) {
buffer += "0";
}
buffer += ".";
}
}
else if (c == buttonTexts[BTN_ZERO]) {
if (buffer.length() == 0 || (buffer.length() == 1 && buffer.charAt(0) == '-')) {
buffer += "0.";
}
else if (buffer.length() > 0 && (isNonZeroNumber(buffer.charAt(0)) ||
(buffer.charAt(0) == '-' && isNonZeroNumber(buffer.charAt(1))))) {
buffer += "0";
}
else if (buffer.compareTo("0") == 0 || buffer.compareTo("-0") == 0) {
}
else {
buffer += "0";
}
}
else {
if (!buffer.equals("0") && !buffer.equals("-0")) {
buffer += c;
}
}
}
lastAction = c;
displayText.setText(buffer);
}
public double GetBufferValue() {
double d = 0.0;
try {
d = Double.parseDouble(buffer);
}
catch (NumberFormatException e) {
System.out.println("NumberFormatException in GetBufferValue()");
}
return d;
}
public boolean isOpCharacter(char c) {
return ((c == buttonTexts[BTN_MULT]) ||
(c == buttonTexts[BTN_DIV]) ||
(c == buttonTexts[BTN_PLUS]) ||
(c == buttonTexts[BTN_MINUS]) ||
(c == buttonTexts[BTN_POWER]) ||
(c == buttonTexts[BTN_PERCENT]));
}
public boolean isNumericCharacter(char c) {
return ((c == buttonTexts[BTN_ZERO]) ||
(c == buttonTexts[BTN_ONE]) ||
(c == buttonTexts[BTN_TWO]) ||
(c == buttonTexts[BTN_THREE]) ||
(c == buttonTexts[BTN_FOUR]) ||
(c == buttonTexts[BTN_FIVE]) ||
(c == buttonTexts[BTN_SIX]) ||
(c == buttonTexts[BTN_SEVEN]) ||
(c == buttonTexts[BTN_EIGHT]) ||
(c == buttonTexts[BTN_NINE]) ||
(c == buttonTexts[BTN_DECIMAL]));
}
public boolean isNonZeroNumber(char c) {
return (c == buttonTexts[BTN_ONE] ||
c == buttonTexts[BTN_TWO] ||
c == buttonTexts[BTN_THREE] ||
c == buttonTexts[BTN_FOUR] ||
c == buttonTexts[BTN_FIVE] ||
c == buttonTexts[BTN_SIX] ||
c == buttonTexts[BTN_SEVEN] ||
c == buttonTexts[BTN_EIGHT] ||
c == buttonTexts[BTN_NINE]);
}
public static void main(String[] args) {
}
}
TEST CLASS:
public class TestCalculator {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new ImageIcon("01_Crane_AGweb.jpg").getImage());
SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();
JFrame frame = new JFrame("Simple Arithmetic Calculator");
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.getContentPane().add(calc);
**frame.setPreferredSize(new Dimension(343,343));**
frame.pack();
frame.setVisible(true);
**frame.setMinimumSize(new Dimension(343,371));**
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
panel.setOpaque(true);
panel.add(calc);
calc.SetColors(null , Color.white , new Color(72,61,139));
calc.setVisible(true);
calc.setOpaque(false);
}
}
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
this.setPreferredSize(new Dimension(size));
this.setMinimumSize(new Dimension(size));
this.setMaximumSize(new Dimension(size));
this.setSize(new Dimension(size));
**this.setLayout(null);**
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this.getWidth(),this.getHeight(),this);
}
}
Frames have borders. If done right,
pack
will attempt to create a viewable area (the space within the frame borders) to match the preferred size of the content pane.Instead of worrying about the size of the frame, let the two components and the layout manager make determination themselves...
The first value is the size of the image panel/image, the second is the size of the window.
Make sure you always call
setResizable
BEFORE callingpack
...Update with simple layout example
Updated from extended example
The order in which you do things is very important. Generally speaking, you want to initialize the interface, add all the components you need, pack the frame and make it visible...
This, generally isn't what you are doing. Instead of doing something like...
You could do something more like...
Instead...
Full running example...