Friday, April 13, 2007

ForLoop

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import javax.swing.border.*;
import java.util.*;
import java.text.DecimalFormat;
import java.awt.event.*;

public class ForLoop extends JFrame{
//cdm
String output="";
int row,col;
Container pane;
JTextArea textarea;
JPanel topPanel, bottomPanel;
//define fonts
Font tisFont = new Font("Arial", Font.PLAIN, 14);
Font PanelFont = new Font ("Tahma", Font.BOLD,15);
//format
DecimalFormat precision = new DecimalFormat("0.00000");

public static void main(String[] args){
ForLoop frameDemo = new ForLoop();
frameDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameDemo.setBounds(150, 100, 400, 300);
frameDemo.setVisible(true);
}

public ForLoop(){
super("Making Frames More Attractive");
//display
buildDisplay();
//topDisplay
topDisplay();
}

void buildDisplay(){
//local var
int i, starVal = 1, endVal = 50;
long square, cube;
double squareRoot, cubeRoot, pwrt = 0.3333;

pane = getContentPane();
textarea = new JTextArea (row, col);
//heading
output += "number\tsqaure\tcube\tsquare root\tcube root\n";

for (i=starVal; i <=endVal; i++){
square = i * i;
cube = i * i * 1;
squareRoot = Math.pow(i,0.5);
cubeRoot = Math.pow(i,pwrt);

output +=" " + i + "\t" + square + "\t" + cube + "\t";
output += precision.format(squareRoot) + "\t";
output += precision.format(cubeRoot) + "\t";
}

textarea.setText(output);
textarea.setFont(tisFont);
textarea.setBackground(Color.white);
textarea.setEditable(false);
textarea.setBorder(new CompoundBorder(

// BorderFactory.createLineBorder(Color.white),
BorderFactory.createBevelBorder(BevelBorder.LOWERED)));
textarea.setToolTipText(">output area<");
pane.add(textarea,BorderLayout.CENTER);
JScrollPane scrollpane = new JScrollPane(textarea);
}

void topDisplay(){
topPanel = new JPanel();
topPanel.setBackground(Color.gray);
topPanel.setToolTipText("Top Panel");
//label

JLabel label = new JLabel ("SQUARE, CUBE, AND ROOTS");
label.setFont(PanelFont);
//label.setBorder(BorderFactory.createBevelBorder(BevelBorder,RAISED));
label.setBackground(Color.red);

topPanel.add(label);
pane.add(topPanel,BorderLayout.NORTH);
}
}

No comments: