|
JTextArea - text align
I made a times table application in java, and i now need to know how to set the layput of the text that is outputed into the JTextArea.
this is how it outputs:
9 x 1 = 9
10 x 1 = 10
11 x 1 = 11 ect...
i want it so all the charaters are in line with each other.
this is the code i got:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
* Creator: Bradley Deane
* Programme: Times Table
* Version: 1
* Date Completed: 26-11-07
*/
//This is the class>>>
public class Times extends JFrame implements ActionListener
{
private JTextArea txtTable;
private JLabel enterNumber;
private JButton calc, reset;
private JTextField numberText;
private int number;
private int times=1;
JFrame wind = new JFrame("Times Table");
//constructor, setting the layout and presentation
public Times()
{
//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);
//sets default close operation
wind.setDefaultCloseOperation(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));
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()"
Times myTimes = new Times();
}
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
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();
}
}//end of class
|