Results 1 to 2 of 2
Thread: JTextArea - text align
- 11-29-2007, 04:14 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
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
- 11-29-2007, 07:08 PM #2
Java 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(); } }
Similar Threads
-
Need Help showing text in JTextArea
By GuyFawkes in forum AWT / SwingReplies: 3Last Post: 05-05-2008, 09:19 AM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM -
problem trying to view the contents of a text file in JTextArea
By warship in forum AWT / SwingReplies: 0Last Post: 07-17-2007, 03:30 PM -
viewing the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 0Last Post: 07-17-2007, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks