|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-29-2007, 05:14 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 2
|
|
|
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, 08:08 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,022
|
|
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();
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|