Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2004, 05:29 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default 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.
 
Old January 5th, 2005, 04:03 PM
Authorized User
 
Join Date: Nov 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to sonicDace Send a message via MSN to sonicDace Send a message via Yahoo to sonicDace
Default

If you can't set the applet size permanently in the html, try using the JApplet.setSize(int x, inty) method

Cheers,

David Glass





Similar Threads
Thread Thread Starter Forum Replies Last Post
what the heck is a container?[:D] lcsgeek General .NET 0 January 24th, 2005 12:56 PM
changing text size on screen markhardiman Classic ASP Professional 4 August 31st, 2004 07:01 AM
Form Container shortynelson General .NET 1 August 13th, 2004 01:27 PM
Changing the page width and size at runtime janet_35 Crystal Reports 0 February 17th, 2004 06:24 AM
Changing the font size in a statusbar xgbnow C++ Programming 0 February 3rd, 2004 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.