View Single Post
  #2 (permalink)  
Old 11-29-2007, 08:08 PM
hardwired hardwired is online now
Senior Member
 
Join Date: Jul 2007
Posts: 1,193
hardwired is on a distinguished road
Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TimesRx /*extends JFrame*/ implements ActionListener { private JTextArea txtTable; private JLabel enterNumber; private JButton calc, reset; private JTextField numberText; private int number; private int times=1; // You do not need to extend JFrame if you are // going to create a new instance like this. JFrame wind = new JFrame("Times Table"); public TimesRx() { //Set the window's bounds, centring the window Toolkit myKit = wind.getToolkit(); int width=250; //sets the JFrame width int height=400; //sets the JFrame height //gets the screen size currently being used by user Dimension wndSize = myKit.getScreenSize(); //calculates the x and y coordinates for the JFrame int x = (wndSize.width - width) / 2; int y = (wndSize.height - height) / 2; //sets the coordinates and size of the JFrame wind.setBounds(x,y,width,height); // Another option to the above centering code: // wind.setLocationRelativeTo(null); //sets default close operation wind.setDefaultCloseOperation(//EXIT_ON_CLOSE); JFrame.EXIT_ON_CLOSE); wind.setResizable(false); //builds the container Container c = wind.getContentPane(); //sets containers background colour c.setBackground(Color.pink); //sets containers layout c.setLayout(new FlowLayout()); //creates the new JLabel enterNumber = new JLabel("Enter Number between 1 " + "and 20"); //adds the JLabel to the container c.add(enterNumber); //creates new JTextField numberText = new JTextField(3); numberText.setBackground(Color.pink); //adds the JTextField to container c.add(numberText); //creates new JButton calc = new JButton("Calculate"); calc.setBackground(Color.pink); //adds the new JButton to the container c.add(calc); //adds an ActionListener to the JButton calc.addActionListener(this); //creates new JButton reset = new JButton("Reset"); reset.setBackground(Color.pink); //adds the new JButton to the container c.add(reset); //adds an ActionListener to the JButton reset.addActionListener(this); //creates a new JTextArea txtTable = new JTextArea(13, 10); txtTable.setBackground(Color.pink); txtTable.setFont(new Font(//"Papyrus",Font.PLAIN,14)); "Monospaced", Font.PLAIN,14)); txtTable.setEditable(false); txtTable.setBorder(BorderFactory.createLineBorder( Color.black)); //adds the new JTextArea to the container c.add(txtTable); //this sets the JFrame to be visible wind.setVisible(true); } public static void main(String[] args) { //creates a new "times()" TimesRx myTimes = new TimesRx(); } public void actionPerformed(ActionEvent arg0) { //a try/catch statement try { //an if statement, what the calc JButton does if (arg0.getSource() == calc) { //local variable int total; /*when calc button pressed, "number" = the text * inside the "numberText" JTextField, and then * must be an integer to be "number" */ number = Integer.parseInt(numberText.getText()); /*if there is already 12 lines in the JTextArea, * then a error message comes up */ if(times>=12) error("Please hit reset and try again"); /*If a number below 1, or higher than 20 is entered * into the JTextField, then an error message will * come up */ if(number<1||number>20) error("Number out of range!!"); /* If "number" passes the last "If" statements, * then it will passed down to the "else" statement */ else { // a while loop while (times <= 12) { //calculation for the total total = number * times; // this inputs text to the JTextArea // String.format was introduced in j2se 1.5 String s = String.format("%2s x %2d = %3d%n", times, number, total); System.out.print(s); txtTable.append(s); // txtTable.append(" " + times + " x " + number + // " = " + total + "\n"); //times = times + 1 times++; } } // The JTextField is set to nothing numberText.setText(""); // The cursor is put in the JTextField numberText.requestFocus(); } //An "if" statement for the reset button if (arg0.getSource() == reset) { //sets the JTextArea to nothing txtTable.setText(""); //sets the JTextField to nothing numberText.setText(""); //the cursor is put in the JTextField numberText.requestFocus(); //times is set back to 1 times=1; } } /*if the "numberText couldn't be converted to a integer * for "number" at the beginning of the "try/catch" statement, * then it misses out the rest and is thrown to the "catch", * which will come up with an error message */ catch(NumberFormatException e) { error("Invalid character, please try again"); } } //the error method private void error(String message) { //dialog box comes up with a message JOptionPane.showMessageDialog(null,message); //the JTextField is set to nothing numberText.setText(""); //the cursor is put in the JTextField numberText.requestFocus(); } }
Reply With Quote