Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating JPanel

 
Kwesi Prat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wrote this program, and I want the first 5 textfilds to be verically aligned on the left, the ok and cancel buttons to be horizontally alligned in the center, and the last 5 text fields to be vetically aligned on the right. Could anyone help me with the code to do that.
Thanks in advace.
Kwsei.
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container.*;
public class TextDemo extends JFrame implements ActionListener {
private JButton button, button1;
private JTextField field1, field2, field3, field4, field5;
private JTextField field6, field7, field8, field9, field10;
// Main Method
public static void main (String args[]) {
TextDemo frame = new TextDemo();
frame.setVisible (true);
}
public TextDemo() {
// Program is closed when (x) button is pressed
setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);

// Set a size
setSize (200, 200);
setVisible(true);

// Left Panel
JPanel panel = new JPanel();
field1 = new JTextField (10);
field2 = new JTextField (10);
field3= new JTextField (10);
field4 = new JTextField (10);
field5 = new JTextField (10);
panel.add (field1);
panel.add (field2);
panel.add (field3);
panel.add (field4);
panel.add (field5);

//Center panel
JPanel panel2 = new JPanel();
JButton button = new JButton ("OK");
panel.add (button);
JButton button1 = new JButton("cancel");
panel.add (button1);

// Right Panel
JPanel panel3 = new JPanel();
field6 = new JTextField (10);
field7 = new JTextField (10);
field8 = new JTextField (10);
field9 = new JTextField (10);
field10 = new JTextField (10);
panel.add (field6);
panel.add (field7);
panel.add (field8);
panel.add (field9);
panel.add (field10);
getContentPane().add (panel);

}
public void actionPerformed (ActionEvent event) {

}
}
 
Joe Ess
Bartender
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I already did in this thread. The Layout part of the Swing Tutorial covers alignment.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic