JTextField Fixed Height - java

How do I get the JTextField to have a fixed height when the Frame is maximized? I want it to look sort of similar to the Skype application on Ubuntu.
private JTextField username;
private JPasswordField password;
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){
setSize(200,200);
setLayout(new GridLayout(4,4));
setBackground(new Color(85,153,187));
setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
username = new JTextField();
password = new JPasswordField();
usernamelabel= new JLabel("Username");
passwordlabel= new JLabel("Password");
username.setBounds(5, 5, 100, 100);
username.setPreferredSize(new Dimension(80,20));
password.setPreferredSize(new Dimension(80,20));
add(usernamelabel);
add(username);
add(passwordlabel);
add(password);

Don't use a layout other than GridLayout or put the text field in another panel that has a FlowLayout that sits inside of your GridLayout.

I think this solves your problem.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
JFrame failFrame = new JFrame();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0, 4));
JPanel leftTextFieldPanel = new JPanel();
leftTextFieldPanel.setLayout(new FlowLayout());
JPanel rightTextFieldPanel = new JPanel();
rightTextFieldPanel.setLayout(new FlowLayout());
JTextField leftTextField = new JTextField();
leftTextField.setPreferredSize(new Dimension(150,20));
JTextField rightTextField = new JTextField();
rightTextField.setPreferredSize(new Dimension(150,20));
leftTextFieldPanel.add(leftTextField);
mainPanel.add(leftTextFieldPanel);
rightTextFieldPanel.add(rightTextField);
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
failFrame.add(mainPanel);
failFrame.setSize(600, 400);
failFrame.setLocationRelativeTo(null);
failFrame.setVisible(true);
}
}
I used nested Layouts. On the Top is your GridLayout and the textfields are in a FlowLayout which are added on the gridLayout panel. So the textfields are no longer stretched.

You can refer to here.
The setMaximumSize method can be found in JComponent and the JTextField gets to use this method from JComponent.
EDIT: However, it seems its not a good practice to hardcode the size of the JTextField particularly when you are running it on multiple platforms as you may face issues with the font-size.

Thanks for all the help guys, hope this helps out others in the future. I got the visual look that I was looking for.
private JTextField username;
private JPasswordField password;
private JPanel [] login = {new JPanel(), new JPanel()};
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){
setSize(200,200);
setBackground(new Color(85,153,187));
setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
username = new JTextField();
password = new JPasswordField();
usernamelabel= new JLabel("Username");
passwordlabel= new JLabel("Password");
login[0].add(usernamelabel);
login[0].add(username);
login[1].add(passwordlabel);
login[1].add(password);
for(JPanel p : login){
p.setBackground(new Color(85,153,187));
this.add(p);
}
username.setBounds(5, 5, 100, 100);
username.setPreferredSize(new Dimension(120,20));
password.setPreferredSize(new Dimension(120,20));
}

In a pinch, doing
tf.setMaximumSize(tf.getMinimumSize())
will keep it constant size and platform neutral.

Related

I can't set Properly the size of each element on my Java Swing GUI

package View;
import javax.swing.*;
import javax.swing.plaf.synth.ColorType;
import modulo.PatenteTypes;
import java.awt.*;
public class FrameLogin extends JFrame {
private static final long serialVersionUID = 1L;
Color Arancione = new Color(255, 150, 0);
public FrameLogin(String ArrayClienti[] ){
this.setTitle("BiKar Prenotazioni");
this.setSize(new Dimension(1000,600));
this.setBackground(Color.black);
this.setForeground(Color.black);
JPanel Pannello = new JPanel();
this.setLayout(new BorderLayout());
Pannello.setLayout(new BorderLayout());
Pannello.setBackground(Color.black);
Pannello.setForeground(Color.orange);
this.add(Pannello);
JComboBox TipoCliente = new JComboBox(ArrayClienti);
TipoCliente.setPreferredSize(new Dimension(200,250));
TipoCliente.setMaximumSize(TipoCliente.getPreferredSize());
TipoCliente.setSize(200, TipoCliente.getPreferredSize().height);
JTextField Nome = new JTextField();
JTextField Cognome = new JTextField();
JTextField ID = new JTextField();
JTextField Eta = new JTextField();
JComboBox<PatenteTypes> TipoPatente= new JComboBox<PatenteTypes>(PatenteTypes.values());
this.setLayout(new BorderLayout());
Pannello.setLayout(new BorderLayout());
this.setBackground(Color.black);
this.setForeground(Color.black);
this.add(Pannello);
this.add(Nome);
this.add(Cognome);
this.add(ID,BorderLayout.WEST);
this.add(Eta, BorderLayout.EAST);
this.add(TipoPatente,BorderLayout.CENTER);
this.add(TipoCliente,BorderLayout.NORTH);
TipoCliente.setBackground(Arancione);
TipoCliente.setForeground(Arancione);
Nome.setBackground(Arancione);
Nome.setForeground(Arancione);
Cognome.setBackground(Arancione);
Cognome.setForeground(Arancione);
ID.setBackground(Arancione);
ID.setForeground(Arancione);
Eta.setBackground(Arancione);
Eta.setForeground(Arancione);
TipoPatente.setBackground(Arancione);
TipoPatente.setForeground(Arancione);
this.setVisible(true);
Pannello.setVisible(true);
TipoCliente.setVisible(true);
ID.setVisible(true);
Eta.setVisible(true);
TipoPatente.setVisible(true);
Nome.setVisible(false);
Cognome.setVisible(false);
}
}
Result:
As you can see in the image the output are 2 very big ComboBox and no TextAreas at all.
I should have gotten 2 small ComboBox. one at the north, the other at the center, and 2 text ares at sides of the combobox at the center. thanks for your help.
I would like to be able to set their dimensione and their position ( I don't want this to be resizable).
also at the border you can see 2 orange lines. I think those where supposed to be the text areas, but all of the space was taken by the JComboBox

How to add spacing between JPanel and JFrame's contentPane?

This is the picture I am trying to replicate
This is what I have (didn't add icon images yet)
I can't seem to find a solution, been staring at it for quite some time.
I am trying to replicate the following picture, using GridLayout for the buttons and the figure out the rest on my own using Java Swing. Furthermore, I've added my buttons into a JPanel and now I'm trying to add spacing between the panel and the pane.
This is what I have, how can I go about it?
super(title);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
Container pane = this.getContentPane();
JButton b1 = new JButton();
b1.setBackground(Color.white);
JButton b2 = new JButton();
b2.setBackground(Color.white);
JButton b3 = new JButton();
b3.setBackground(Color.white);
JButton b4 = new JButton();
b4.setBackground(Color.white);
JButton b5 = new JButton();
b5.setBackground(Color.white);
JButton b6 = new JButton();
b6.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,10,10));
panel.setBackground(Color.black);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
pane.add(panel);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
The easiest way to do it would be to add an empty border to your JPanel (see this post on empty borders):
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// ...
panel.setBorder(new EmptyBorder(50, 50, 50, 50));
Another good approach (depending always on your application needs), if you have the JButton preferred size set, would be to have the main JPanel's grid layout set to have two columns and one row, with another JPanel inside each column. Adding to the interior JPanels a BoxLayout in Y_AXIS mode and aligning the buttons with setAlignmentX() would work great too (note this approach wouldn't center the JButtons vertically) (see How to use BoxLayout):
public class MyFrame extends JFrame {
private String title = "Title";
public MyFrame(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,2,10,10));
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
JButton b1 = new JButton();
b1.setBackground(Color.white);
//b1.setIcon(new ImageIcon(img1));
b1.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b1);
JButton b2 = new JButton();
b2.setBackground(Color.white);
//b2.setIcon(new ImageIcon(img2));
b2.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b2);
JButton b3 = new JButton();
b3.setBackground(Color.white);
//b3.setIcon(new ImageIcon(img3));
b3.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b3);
JButton b4 = new JButton();
b4.setBackground(Color.white);
//b4.setIcon(new ImageIcon(img4));
b4.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b4);
JButton b5 = new JButton();
b5.setBackground(Color.white);
//b5.setIcon(new ImageIcon(img5));
b5.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b5);
JButton b6 = new JButton();
b6.setBackground(Color.white);
//b6.setIcon(new ImageIcon(img6));
b6.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b6);
add(mainPanel); //Adding our mainPanel to the contentPane of the JFrame
this.setSize(500,500); //or pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
}
}
Here's a little demonstration I whipped up.
All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
You don't set the size of the JFrame and try and make the Swing components fit. You let the JFrame pack with all the Swing components.
You create a GridLayout JPanel inside of a FlowLayout JPanel. The FlowLayout JPanel uses an empty border of the appropriate size.
I used the image the OP provided to get the icons.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class EmptySpaceDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new EmptySpaceDemo());
}
private Image[] images;
public EmptySpaceDemo() {
this.images = createImages();
}
private Image[] createImages() {
BufferedImage image = readImage();
Image[] images = new Image[6];
images[0] = image.getSubimage(155, 113, 110, 90);
images[1] = image.getSubimage(276, 113, 110, 90);
images[2] = image.getSubimage(155, 217, 110, 90);
images[3] = image.getSubimage(276, 217, 110, 90);
images[4] = image.getSubimage(155, 321, 110, 90);
images[5] = image.getSubimage(276, 321, 110, 90);
return images;
}
#Override
public void run() {
JFrame frame = new JFrame("Empty Space Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.BLACK);
panel.setBorder(BorderFactory.createEmptyBorder(40, 100, 40, 100));
JPanel innerPanel = new JPanel(new GridLayout(0, 2, 10, 10));
innerPanel.setBackground(Color.BLACK);
for (int i = 0; i < images.length; i++) {
JButton button = new JButton(new ImageIcon(images[i]));
innerPanel.add(button);
}
panel.add(innerPanel);
return panel;
}
private BufferedImage readImage() {
try {
return ImageIO.read(getClass().getResourceAsStream("/icons.png"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

Other placement of fields everytime i run the code

I'm stuck with my application. Every time I run my code, my fields e.g. fromTextField once have width 180 other time my width private static final Integer widthField = 232.
Code:
package gui;
import javax.swing.*;
import java.awt.*;
public class MailGui extends JFrame {
private static final Integer widthField = 232;
private MailGui() {
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(5, 5));
JLabel fromLabel = new JLabel("From: ", SwingConstants.LEFT);
JLabel passwdLabel = new JLabel("Password: ", SwingConstants.LEFT);
JLabel toLabel = new JLabel("To: ", SwingConstants.LEFT);
JLabel subjectLabel = new JLabel("Subject: ", SwingConstants.LEFT);
JLabel textLabel = new JLabel("Content: ", SwingConstants.LEFT);
JTextField fromTextField = new JTextField();
JTextField toTextField = new JTextField();
JPasswordField passwdPasswordField = new JPasswordField();
JTextField subjectTextField = new JTextField();
JTextArea textArea = new JTextArea(8, 30);
JButton sendButton = new JButton("Send");
textArea.setLineWrap(true);
northPanel.add(fromLabel);
northPanel.add(fromTextField);
northPanel.add(passwdLabel);
northPanel.add(passwdPasswordField);
northPanel.add(toLabel);
northPanel.add(toTextField);
northPanel.add(subjectLabel);
northPanel.add(subjectTextField);
northPanel.add(textLabel);
northPanel.add(textArea);
this.add(northPanel, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(textArea);
this.add(scrollPane, BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.add(sendButton);
add(southPanel, BorderLayout.SOUTH);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
fromTextField.setBounds(textArea.getX() + 100, 0, widthField, 19);
passwdPasswordField.setBounds(textArea.getX() + 100, 19, widthField, 19);
toTextField.setBounds(textArea.getX() + 100, 38, widthField, 19);
subjectTextField.setBounds(textArea.getX() + 100, 57, widthField, 19);
}
public static void main(String[] args) {
new MailGui();
}
}
What I have:
Or
What I except:
Thanks for every help. Q.
Invoking the setBounds() method does nothing. The layout manager will override the size/location based on the rules of the layout manager. In the case of the GridLayout all components will be sized to the preferred size of the largest component or as the frame size is changes each cell will adjust to fill the space available in the frame.
When you create a JTextField the code should be something like:
JTextField textField = new JTextField(15);
This will allow the text field to determine its own preferred size to display 15 "W" characters based on the font of the text field.
Then the layout manager can do a better job at determining the size of each component
If you want the widths of the labels and the text fields to be different, then you need to use a different layout manager. Probably a GridBagLayout. Read the Swing tutorial on How to Use GridBagLayout for more information and examples.
Note the tutorial examples show you how to create the GUI on the Event Dispatch Thread (EDT).
Call pack() at the end of the GUI composition in order to layout it in a definite way once.
The dependency of the geometry to textArea is unfortunate. Put it in the layout too.
There are nice GUI editors with more complex layout managers.

JAVA JTextField doesnt show up

So Im trying to make a little program to calculate the area of a specific shape.
The user should be able to make a input via a textfield (Like the height and stuff of the shapes). The he should press a button and the price should get printed.
But it doesnt show up.
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rechner extends JFrame implements ActionListener{
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JButton button4;
private static JTextField numberField;
private JPanel jpanel;
public Rechner(String titel){
super(titel);
jpanel = new JPanel();
numberField = new JTextField(1500);
add(numberField, BorderLayout.CENTER);
button1 = new JButton("Rechteck");
button1.setBounds(10, 10, 150, 30);
button1.addActionListener(this);
add(button1);
button2 = new JButton("Dreieck");
button2.setBounds(170, 10, 150, 30);
button2.addActionListener(this);
add(button2);
button3 = new JButton("Trapez");
button3.setBounds(330, 10, 150, 30);
button3.addActionListener(this);
add(button3);
button4 = new JButton("Parallelogramm");
button4.setBounds(490, 10, 150, 30);
button4.addActionListener(this);
add(button4);
setResizable(false);
}
public static void main(String[] args) {
Rechner frame = new Rechner("Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(660, 400);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
System.out.println("fff");
}
String numberStr = numberField.getText();
}
}
The default layout manager of a JFrame (well, of its content pane in fact) is BorderLayout.
Without specified constraints , the component is added to BorderLayout.CENTER, so
add(component);
is the same as
add(component, BorderLayout.CENTER);
and each component added this way will replace the last component added to the center.
Also note that setBounds will have no effect if there is a layout manager, and that you create a JPanel that you never use.
Finally, you may want to have a look at this guide : A Visual Guide to Layout Managers
This line is mainly the problem:
add(numberField, BorderLayout.CENTER);
Is causing the TextField to fill the entire space. Then, the next time you add a component to the JFrame with BorderLayout.CENTER, the JTextField gets replaced. To fix this:
super(titel);
jpanel = new JPanel();
add(jpanel, BorderLayout.NORTH); //adding the jpanel
button1 = new JButton("Rechteck");
jpanel.add(button1);
button1.setBounds(10, 10, 150, 30);
//adding the other buttons to the JPanel...
//...
//...
button4.addActionListener(this);
button3.addActionListener(this);
button2.addActionListener(this);
button1.addActionListener(this);
numberField = new JTextField(1500);
add(numberField);//this will cause it to fill the remaining space
setResizable(false);
Explanation:
The buttons should go into the JPanel you created, and the JPanel should go into the JFrame's NORTH. That way they don't cover the JFrame

Nothing appears in the Java GUI

So I have a Java GUI exercise from college to work on, about making a registration form.
I've [mistakenly] coded everything from beginning to end without testing, and found out that nothing actually appears on my GUI. The program was just a plain JFrame without anything inside, like in the picture above.
I've tried to find out why this happened, however I haven't found any clue why. So your help is appreciated.
So far, I haven't found any duplicate of this question. I found that my issue is somewhat different from other users. So, I'd like to apologize as if there's one duplicate or so.
Below is the code: (you may want to take a keen look at this)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class RegistrationForm extends JFrame {
private JPanel panel;
private JPanel northPanel;
private JPanel eastPanel;
private JPanel westPanel;
private JPanel southPanel;
private JPanel centerPanel;
private JPanel formPanel;
private JPanel basicFormPanel;
private JPanel addressPanel;
private JPanel typePanel;
private JPanel cityPanel;
private JPanel agreementPanel;
private JPanel buttonPanel;
private JLabel formTitle;
private JLabel lblName;
private JLabel lblEmail;
private JLabel lblPhone;
private JLabel lblAddress;
private JLabel lblType;
private JLabel lblCity;
private JTextField name;
private JTextField email;
private JTextField phone;
private JScrollPane addressScroll;
private JTextArea address;
private ButtonGroup type;
private JRadioButton silver;
private JRadioButton gold;
private JRadioButton platinum;
private JComboBox<String> city;
private JCheckBox agreement;
private JButton submit;
private JButton reset;
public RegistrationForm() {
initElements();
initView();
initFormPanels();
}
public void initElements() {
panel = new JPanel();
northPanel = new JPanel();
eastPanel = new JPanel();
westPanel = new JPanel();
southPanel = new JPanel();
centerPanel = new JPanel();
formPanel = new JPanel();
basicFormPanel = new JPanel();
addressPanel = new JPanel();
typePanel = new JPanel();
cityPanel = new JPanel();
agreementPanel = new JPanel();
buttonPanel = new JPanel();
formTitle = new JLabel("Registration Form");
formTitle.setFont(new Font("Arial", Font.PLAIN, 32));
lblName = new JLabel("Name");
lblEmail = new JLabel("Email");
lblPhone = new JLabel("Phone");
lblAddress = new JLabel("Address");
lblType = new JLabel("Type");
lblCity = new JLabel("City");
name = new JTextField();
email = new JTextField();
phone = new JTextField();
address = new JTextArea();
addressScroll = new JScrollPane(address);
type = new ButtonGroup();
silver = new JRadioButton("Silver");
gold = new JRadioButton("Gold");
platinum = new JRadioButton("Platinum");
String[] cities = {"Jakarta", "Bandung", "Bogor"};
city = new JComboBox<String>(cities);
agreement = new JCheckBox("I agree with the terms and conditions");
submit = new JButton("Submit");
reset = new JButton("Reset");
}
public void initView() {
panel.setLayout(new BorderLayout());
panel.add(northPanel, BorderLayout.NORTH);
panel.add(eastPanel, BorderLayout.EAST);
panel.add(westPanel, BorderLayout.WEST);
panel.add(southPanel, BorderLayout.SOUTH);
panel.add(centerPanel, BorderLayout.CENTER);
northPanel.setBackground(Color.black);
northPanel.setPreferredSize(new Dimension(50, 50));
formTitle.setForeground(Color.white);
eastPanel.setBackground(Color.black);
westPanel.setBackground(Color.black);
southPanel.setBackground(Color.black);
centerPanel.setBackground(Color.gray);
formPanel.setBackground(Color.gray);
basicFormPanel.setBackground(Color.gray);
addressPanel.setBackground(Color.gray);
typePanel.setBackground(Color.gray);
cityPanel.setBackground(Color.gray);
agreementPanel.setBackground(Color.gray);
formPanel.add(basicFormPanel);
formPanel.add(addressPanel);
formPanel.add(typePanel);
formPanel.add(cityPanel);
formPanel.add(agreementPanel);
formPanel.add(buttonPanel);
centerPanel.add(formPanel);
buttonPanel.setBackground(Color.black);
buttonPanel.add(submit);
buttonPanel.add(reset);
southPanel.add(buttonPanel);
}
public void initFormPanels() {
// basic form panel
basicFormPanel.setLayout(new GridLayout(3, 3, 5, 5));
basicFormPanel.add(lblName);
basicFormPanel.add(name);
basicFormPanel.add(lblEmail);
basicFormPanel.add(email);
basicFormPanel.add(lblPhone);
basicFormPanel.add(phone);
// address panel
addressPanel.setLayout(new GridLayout(1, 2, 5, 5));
addressPanel.add(lblAddress);
addressPanel.add(addressScroll);
// type panel
typePanel.setLayout(new GridLayout(1, 2, 5, 5));
typePanel.add(lblType);
type.add(silver);
type.add(gold);
type.add(platinum);
buttonPanel.add(silver);
buttonPanel.add(gold);
buttonPanel.add(platinum);
typePanel.add(buttonPanel);
// city panel
cityPanel.setLayout(new GridLayout(1, 2, 5, 5));
cityPanel.add(lblCity);
cityPanel.add(city);
// agreement checkbox panel
agreementPanel.setLayout(new FlowLayout());
agreementPanel.add(agreement);
// button panel
buttonPanel.add(submit);
buttonPanel.add(reset);
// set preferred sizes
basicFormPanel.setPreferredSize(new Dimension(400, 100));
addressPanel.setPreferredSize(new Dimension(400, 90));
cityPanel.setPreferredSize(new Dimension(400, 30));
typePanel.setPreferredSize(new Dimension(400, 50));
agreementPanel.setPreferredSize(new Dimension(400, 30));
buttonPanel.setPreferredSize(new Dimension(400, 50));
}
public static void main(String[] args) {
RegistrationForm gui = new RegistrationForm();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(450, 450);
gui.setTitle("Registration");
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}
}
Thanks for helping out. I hope I'll get this done very soon.
Looks like you need to add your root component you made (panel) to this.
public RegistrationForm() {
initElements();
initView();
initFormPanels();
this.add(panel);
}
Change:
panel.add(centerPanel, BorderLayout.CENTER);
To this to see something:
panel.add(centerPanel, BorderLayout.CENTER);
setContentPane(panel);
add this.add(panel) in RegistrationForm constructor.It worked for me.

Categories

Resources