• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add text fields  RSS feed

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i want to add text fields in this card layout one belo the other with a little gap. But when i add textfield i am getting beside the other. Can please any one help me out with this issue, here is my code as follows. Please modify my code so i can get a appropriate output. Thanks and Regards.


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

public class exp4 extends JFrame
{
private CardLayout cl;

public exp4()
{
super("Experiment4");
JPanel cp = new JPanel();
cl = new CardLayout();
cp.setLayout(cl);
Container c = getContentPane();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp1.add(new JLabel("Name"));
jp2.add(new JLabel ("Card 2"));
jp3.add(new JLabel ("Card 3"));
jp1.add(new JTextField (10));
jp1.add(new JTextField(5));
cp.add(jp1,"1");
cp.add(jp2,"2");
cp.add(jp3,"3");
JPanel bp = new JPanel();
JButton first = new JButton ("First");
JButton next = new JButton ("Next");
JButton prev = new JButton ("Previous");
JButton last = new JButton ("Last");
bp.add(first);
bp.add(next);
bp.add(prev);
bp.add(last);
getContentPane().add(cp, BorderLayout.NORTH);
getContentPane().add(bp, BorderLayout.SOUTH);
setSize(400,400);
}
public static void main(String[] args) {
exp4 cl = new exp4();
cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl.setVisible(true);
}
}
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags round quoted code; it makes it easier to read.

You aren't using a layout on your panels; they default to FlowLayout which adds Components from left to right.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adeeb alexander:
i want to add text fields in this card layout one belo the other with a little gap.

I agree with the recommendations from Campbell Richie. In addition, you might want to look specifically into using the BoxLayout or the GridLayout to stack things. The Sun Layout tutorials are quite good, and I highly recommend them. Good luck.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pete stein:

The Sun Layout tutorials are quite good, and I highly recommend them. Good luck.


One more addition link to usage of layouts in java.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Balasubramanian Chandrasekaran:


One more addition link to usage of layouts in java.
Oh, it's that one. For a horrible moment I thought you had found the GridBagLayout Tutorial.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!