dkim18
asked on
Adding JPanel into JFrame
Hi!
I need to put some instructional label on top and three buttons at the bottom. I was thinking creating Jpanels for them and inserting into Jframe, but it doesn’t seems to work and the format is messed up. Can some one show me better ways of doing it?
Thanks,
++++++++++
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyJTextField {
public static void main(String[] args) {
JLabel l;
JButton b;
final JFrame f = new JFrame("MyJTextField.java Example");
Container cp = f.getContentPane();
// Make some 35 column wide text fields
JTextField nameField = new JTextField(35);
JTextField streetField = new JTextField(35);
JTextField cityField = new JTextField(35);
JTextField countyField = new JTextField(35);
JTextField zipField = new JTextField(35);
JTextField phoneField = new JTextField(35);
// Now place them in the panl
cp.setLayout(new GridBagLayout());
cp.setBackground(UIManager .getColor( "control") );
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIV E;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(2, 2, 2, 2);
c.anchor = GridBagConstraints.EAST;
// labels for the text fields
cp.add(l = new JLabel("Name:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("House/Street:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("City:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("State/County:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("Zip/Post code:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("Telephone:", SwingConstants.RIGHT), c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
// c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZON TAL;
// c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
cp.add(nameField, c);
c.gridx = 1;
c.gridy = GridBagConstraints.RELATIV E;
cp.add(streetField, c);
cp.add(cityField, c);
cp.add(countyField, c);
cp.add(zipField, c);
cp.add(phoneField, c);
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
f.setDefaultCloseOperation (JFrame.EX IT_ON_CLOS E);
Runnable showFrame = new Runnable() {
public void run() {
f.pack();
f.setVisible(true);
}
};
SwingUtilities.invokeLater (showFrame );
}
}
++++++++++++
I need to put some instructional label on top and three buttons at the bottom. I was thinking creating Jpanels for them and inserting into Jframe, but it doesn’t seems to work and the format is messed up. Can some one show me better ways of doing it?
Thanks,
++++++++++
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyJTextField {
public static void main(String[] args) {
JLabel l;
JButton b;
final JFrame f = new JFrame("MyJTextField.java Example");
Container cp = f.getContentPane();
// Make some 35 column wide text fields
JTextField nameField = new JTextField(35);
JTextField streetField = new JTextField(35);
JTextField cityField = new JTextField(35);
JTextField countyField = new JTextField(35);
JTextField zipField = new JTextField(35);
JTextField phoneField = new JTextField(35);
// Now place them in the panl
cp.setLayout(new GridBagLayout());
cp.setBackground(UIManager
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIV
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(2, 2, 2, 2);
c.anchor = GridBagConstraints.EAST;
// labels for the text fields
cp.add(l = new JLabel("Name:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("House/Street:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("City:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("State/County:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("Zip/Post code:", SwingConstants.RIGHT), c);
cp.add(l = new JLabel("Telephone:", SwingConstants.RIGHT), c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
// c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZON
// c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
cp.add(nameField, c);
c.gridx = 1;
c.gridy = GridBagConstraints.RELATIV
cp.add(streetField, c);
cp.add(cityField, c);
cp.add(countyField, c);
cp.add(zipField, c);
cp.add(phoneField, c);
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
f.setDefaultCloseOperation
Runnable showFrame = new Runnable() {
public void run() {
f.pack();
f.setVisible(true);
}
};
SwingUtilities.invokeLater
}
}
++++++++++++
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.