the frame is okay but the text2, textfield has occupy the whole frame. what's wrong with my code?
image: http://img68.imageshack.us/my.php?image=73815503ku1.png
here's the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JFrameTest extends JFrame implements ActionListener {
	JButton button;	
	JLabel label1, label2, label3;
	JTextField text1, text2;
	Container con = getContentPane();
	
	public JFrameTest() {
		button = new JButton("Submit");
		button.addActionListener(this);
		button.setBounds(400, 75, 75, 20);
		button.setVisible(true);
		
		label1 = new JLabel("Name: ");
		label1.setBounds(10, 10, 75, 20);
		label1.setVisible(true);
		
		label2 = new JLabel("Address: ");
		label2.setBounds(10, 30, 75, 20);
		label2.setVisible(true);
		
		label3 = new JLabel("Output");
		label3.setBounds(10, 100, 200, 20);
		label3.setVisible(true);
		
		text1 = new JTextField();
		text1.setBounds(75, 10, 400, 20);
		text1.setVisible(true);
				
		text2 = new JTextField();
		text2.setBounds(75, 30, 400, 20);
		text2.setVisible(true);
		
		con.add(button);
		con.add(label1);
		con.add(label2);
		con.add(label3);
		con.add(text1);
		con.add(text2);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if (source == button) {
			label3.setText("The name is "+text1.getText()+" and the address is "+text2.getText());
		}
	}
	
	public static void main (String args[]) {
		JFrameTest frame = new JFrameTest();
		frame.setTitle("JFrame Test");
		frame.setBounds(50, 50, 500, 200);
		frame.setVisible(true);
	}
}

Recommended Answers

All 6 Replies

You have not set the layout for your JFrame, and so it is using the FlowLayout.
And from your code it looks like you want to use the NULL Layout (something like what VB offers).
Refer to this tutorial from Sun to learn about Layout Managers in Java.

Also I suggest you do not use the NULL layout cause then Java cannot rearrange/resize your components depending on the User's screen size / resolution.

You have not set the layout for your JFrame, and so it is using the FlowLayout.

Uhmmmm, I believe the contentPanes of the top-level windows use BorderLayout, but that's beside the fact.

See http://java.sun.com/docs/books/tutorial/uiswing/layout/

It is almost always a big mistake to attempt to use the "null" layout (i.e. no layout). And for something like this, it is definately a mistake. Learn how to use some of the varying layout managers.

I wouldn't suggest you try it right away, but I use GridBagLayout extensively. It is the most flexible and controllable of all the layout managers, but it is also the most complex and the easiest to get wrong.

Uhmmmm, I believe the contentPanes of the top-level windows use BorderLayout, but that's beside the fact.

Grrrr :@ ........ yep that's correct :) !!!

Glad at least we agree on the tutorial he should use and about the null layout.

thanks for the replies.. i'm just starting w/ frame. idk about layout. so that's the prob with my code.. thanks! i will mark this as solved.

Grrrr :@ ........ yep that's correct :) !!!

Glad at least we agree on the tutorial he should use and about the null layout.

Oops. Didn't read your post carefully and missed the reference to the tutorial. Otherwise I'd have simply said "listen to him". ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.