Changing the size of a container.
Hiya Guys,
I am trying to change the size of a applet container so that when it executes it shows all numbers and button text. When this class is run, the container shows the text on the buttons as three dots. I was wondering if anyone knew how to make the container bigger so that my text would show. Here is my code: -
/*
* Created on 27-Dec-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Chapter4;
import java.awt.Color;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
* @author Chris Wallis
*
* This is a class created to mimic a calculator.
*/
public class TestCalculator extends JApplet implements ActionListener{
// Create the text fields and the buttons for the user
// interface
private JTextField textField;
private JButton numberZero;
private JButton numberOne;
private JButton numberTwo;
private JButton numberThree;
private JButton numberFour;
private JButton numberFive;
private JButton numberSix;
private JButton numberSeven;
private JButton numberEight;
private JButton numberNine;
private JButton plus;
private JButton equals;
private JButton minus;
private JButton divide;
private JButton multiply;
private JButton clearScreen;
private JButton memoryClear;
private JButton addToMemory;
private JButton memoryRecall;
// Create a temporary storage for the arithmetic operations
private String displayTemp = "";
// Create the boolean flags for the equals method
private boolean addition = false;
private boolean subtract = false;
private boolean divideBy = false;
private boolean multiplyBy = false;
// Create a double variable for the number do so the arithmetic on.
private double number;
//Create a String variable for the memory fuction
private String memoryNumber;
public void init(){
Container myContainer = getContentPane();
myContainer.setSize(200,200);
myContainer.setVisible(true);
GridLayout myGridLayout = new GridLayout(6, 0);
myContainer.setLayout(myGridLayout);
textField = new JTextField();
myContainer.add(textField);
GridLayout gridLayout = new GridLayout(0, 5);
GridLayout gridLayout1 = new GridLayout(0, 4);
Panel panel = new Panel();
panel.setLayout(gridLayout1);
clearScreen = new JButton("C");
clearScreen.setBackground(Color.RED);
panel.add(clearScreen);
memoryClear = new JButton("MC");
memoryClear.setBackground(Color.RED);
panel.add(memoryClear);
addToMemory = new JButton("M+");
addToMemory.setBackground(Color.RED);
panel.add(addToMemory);
memoryRecall = new JButton("MR");
memoryRecall.setBackground(Color.RED);
panel.add(memoryRecall);
myContainer.add(panel);
//Add the test field
Panel panel1 = new Panel();
panel1.setLayout(gridLayout);
numberZero = new JButton("0");
numberZero.setSize(10, 10);
numberZero.setBackground(Color.BLUE);
panel1.add(numberZero);
numberOne = new JButton("1");
numberOne.setBackground(Color.BLUE);
panel1.add(numberOne);
numberTwo = new JButton("2");
numberTwo.setBackground(Color.BLUE);
panel1.add(numberTwo);
numberThree = new JButton("3");
numberThree.setBackground(Color.BLUE);
panel1.add(numberThree);
numberFour = new JButton("4");
numberFour.setBackground(Color.BLUE);
panel1.add(numberFour);
myContainer.add(panel1);
Panel panel2 = new Panel();
panel2.setLayout(gridLayout);
numberFive = new JButton("5");
numberFive.setBackground(Color.BLUE);
panel2.add(numberFive);
numberSix = new JButton("6");
numberSix.setBackground(Color.BLUE);
panel2.add(numberSix);
numberSeven = new JButton("7");
numberSeven.setBackground(Color.BLUE);
panel2.add(numberSeven);
numberEight = new JButton("8");
numberEight.setBackground(Color.BLUE);
panel2.add(numberEight);
numberNine = new JButton("9");
numberNine.setBackground(Color.BLUE);
panel2.add(numberNine);
myContainer.add(panel2);
Panel panel3 = new Panel();
panel3.setLayout(gridLayout1);
plus = new JButton("+");
plus.setBackground(Color.BLUE);
panel3.add(plus);
minus = new JButton("-");
minus.setBackground(Color.BLUE);
panel3.add(minus);
divide = new JButton("/");
divide.setBackground(Color.BLUE);
panel3.add(divide);
multiply = new JButton("*");
multiply.setBackground(Color.BLUE);
panel3.add(multiply);
myContainer.add(panel3);
equals = new JButton("=");
plus.setBackground(Color.BLUE);
myContainer.add(equals);
numberZero.addActionListener(this);
numberOne.addActionListener(this);
numberTwo.addActionListener(this);
numberThree.addActionListener(this);
numberFour.addActionListener(this);
numberFive.addActionListener(this);
numberSix.addActionListener(this);
numberSeven.addActionListener(this);
numberEight.addActionListener(this);
numberNine.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
divide.addActionListener(this);
multiply.addActionListener(this);
equals.addActionListener(this);
clearScreen.addActionListener(this);
addToMemory.addActionListener(this);
memoryClear.addActionListener(this);
memoryRecall.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source.equals(numberZero)){
displayTemp += numberZero.getLabel();
textField.setText(displayTemp);
} else if(source.equals(numberOne)){
displayTemp += numberOne.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberTwo)) {
displayTemp += numberTwo.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberThree)) {
displayTemp += numberThree.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberFour)) {
displayTemp += numberFour.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberFive)) {
displayTemp += numberFive.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberSix)) {
displayTemp += numberSix.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberSeven)) {
displayTemp += numberSeven.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberEight)) {
displayTemp += numberEight.getLabel();
textField.setText(displayTemp);
} else if (source.equals(numberNine)) {
displayTemp += numberNine.getLabel();
textField.setText(displayTemp);
} else if (source.equals(plus)){
addition = true;
number = Double.parseDouble(textField.getText());
displayTemp = "";
textField.setText("");
} else if (source.equals(minus)){
subtract = true;
number = Double.parseDouble(textField.getText());
displayTemp = "";
textField.setText("");
} else if (source.equals(divide)){
divideBy = true;
number = Double.parseDouble(textField.getText());
displayTemp = "";
textField.setText("");
} else if (source.equals(multiply)){
multiplyBy = true;
number = Double.parseDouble(textField.getText());
displayTemp = "";
textField.setText("");
} else if (source.equals(clearScreen)){
textField.setText("");
displayTemp = "";
} else if (source.equals(addToMemory)) {
memoryNumber = textField.getText();
} else if (source.equals(memoryRecall)) {
textField.setText(memoryNumber);
} else if (source.equals(memoryClear)) {
memoryNumber = "";
}
if(source.equals(equals)){
if (addition) {
double addition = number + Double.parseDouble(textField.getText());;
displayTemp = Double.toString(addition);
textField.setText(displayTemp);
this.addition = false;
} else if (subtract){
double addition = number - Double.parseDouble(textField.getText());;
displayTemp = Double.toString(addition);
textField.setText(displayTemp);
this.subtract = false;
} else if (divideBy){
double addition = number / Double.parseDouble(textField.getText());
displayTemp = Double.toString(addition);
textField.setText(displayTemp);
this.divideBy = false;
} else if (multiplyBy){
double addition = number * Double.parseDouble(textField.getText());;
displayTemp = Double.toString(addition);
textField.setText(displayTemp);
this.multiplyBy = false;
}
}
}
public static void main(String[] args) {
TestCalculator myCalculator = new TestCalculator();
myCalculator.init();
}
}
any ideas
Thanks :)
Adz - The World is not enough
__________________
Adz - Learning The J2EE Ways.
|