Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Problems with JFrame

Posted on 2009-02-23
2
Medium Priority
?
574 Views
Last Modified: 2012-05-06
I created a program in Java that allows you to create a phone list. Everything works properly but when the JFrame appears the title name is not aligned with the box, its right next to the title. I want "name" to be next to the box, just like "number" is next to the box.
first code:
 
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreatePhoneList extends JFrame implements ActionListener
{
private JLabel friendsList = new JLabel("Friend Contact List");
 
Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
 
 
	private JTextField name = new JTextField(8);
	private JTextField number = new JTextField(4);
 
	private JLabel nLabel = new JLabel("Name");
	private JLabel pLabel = new JLabel("Number");
 
	private JButton enterDataButton= new JButton("Enter data");
 
	private Container con = getContentPane();
 
	DataOutputStream ostream;
 
	public CreatePhoneList()
	{
 
super("Create Phone List");
 
		try
		{
			ostream = new DataOutputStream(new FileOutputStream("phone.dat"));
		}
		catch(IOException e)
		{
			System.err.println("File not opened");
			System.exit(1);
		}
 
		setSize(320,2000);
		con.setLayout(new FlowLayout());
		friendsList.setFont(bigFont);
		con.add(friendsList);
 
		con.add(nLabel);
		con.add(name);
		con.add(pLabel);
		con.add(number);
		con.add(enterDataButton);
 
		enterDataButton.addActionListener(this);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
 
public void actionPerformed(ActionEvent e1)
{
	try
	{
		ostream.writeUTF(name.getText());
		ostream.writeUTF(number.getText());
 
		name.setText("");
		number.setText("");
	}
 
	catch(NumberFormatException e2)
	{
		System.err.println("Invalid data inputted, only numeric data");
	}
 
	catch(IOException e3)
	{
		System.err.println("Error writing file");
		System.exit(1);
	}
}
}
 
second code:
 
public class UserPhoneList
{
public static void main(String[] args)
{
CreatePhoneList cpl = new CreatePhoneList();
}
}

Open in new window

0
Comment
Question by:bdiddy05
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 750 total points
ID: 23711925
You'll need to use a layout manager that supports that. Use a BoxLayout - take the Sun BoxLayout tutorial
0
 
LVL 6

Expert Comment

by:javaexperto
ID: 23712584
An easy way is to use GridLayout. Note that you need 5 columns for your labels, textfields, and button. Also you need only one column for the friend contact title.
I created two JPanels, one for the title and one for textfields, labels and button.
Remember GridLayout specifies the number of columns and rows that you want to use.
Finally add these panels to your container that also has gridLayout (35 rows, 1 column).
Your panels will fit only the two first rows.
You can also add extras spaces to your Strings in order to move closer to the fields.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreatePhoneList extends JFrame implements ActionListener
{
private JLabel friendsList = new JLabel("       Friend Contact List");
 
Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
 
 
	private JTextField name = new JTextField(8);
	private JTextField number = new JTextField(4);
 
	private JLabel nLabel = new JLabel("       Name");
	private JLabel pLabel = new JLabel("    Number");
 
	private JButton enterDataButton= new JButton("Enter data");
 
	private Container con = getContentPane();
 
	DataOutputStream ostream;
 
	public CreatePhoneList()
	{
 
super("Create Phone List");
 
		try
		{
			ostream = new DataOutputStream(new FileOutputStream("phone.dat"));
		}
		catch(IOException e)
		{
			System.err.println("File not opened");
			System.exit(1);
		}
 
		setSize(320,2000);
		con.setLayout(new GridLayout(35, 1, 0, 0));
		friendsList.setFont(bigFont);
 
		JPanel a = new JPanel(new GridLayout(1, 5, 1, 1));
		JPanel b = new JPanel(new GridLayout(1, 1, 1, 1));
		
		b.add(friendsList);
		a.add(nLabel);
		a.add(name);
		a.add(pLabel);
		a.add(number);
		a.add(enterDataButton);
		con.add(b);
		con.add(a);
 
		enterDataButton.addActionListener(this);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
 
public void actionPerformed(ActionEvent e1)
{
	try
	{
		ostream.writeUTF(name.getText());
		ostream.writeUTF(number.getText());
 
		name.setText("");
		number.setText("");
	}
 
	catch(NumberFormatException e2)
	{
		System.err.println("Invalid data inputted, only numeric data");
	}
 
	catch(IOException e3)
	{
		System.err.println("Error writing file");
		System.exit(1);
	}
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question