Question

import javax.swing.*;
import java.awt.*;
public class SQlUI {
public static void main(String[] args){
SQlUI user=new SQlUI();
user.go();
}
public void go(){   
//Creating a Frame
JFrame frame=new JFrame();
//Creating three Panels
JPanel panel0=new JPanel();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();

//Creating three Buttons
JButton button0=new JButton("INSERT");
JButton button1=new JButton("UPDATE");
JButton button2=new JButton("DELETE");

//Adding panel0 to the frame which contains three butoon objects
frame.getContentPane().add(BorderLayout.SOUTH,panel0);
panel0.add(button0);
panel0.add(button1);
panel0.add(button2);

//Creating four textbox
JTextField textbox0 = new JTextField(120);
JTextField textbox1 = new JTextField(120);
JTextField textbox2 = new JTextField(120);
JTextField textbox3 = new JTextField(120);

//Adding panel1 to the frame which contains four textbox objects
frame.getContentPane().add(BorderLayout.EAST,panel1);
//Using BoxLayout managaer for panel1 objects
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(textbox0);
panel1.add(textbox1);
panel1.add(textbox2);
panel1.add(textbox3);

//Adding panel2 to the frame which contains four label objects
frame.getContentPane().add(BorderLayout.WEST,panel2);
//Using BoxLayout managaer for panel1 objects
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));

//Creating four labels 
JLabel label0=new JLabel("Name");
label0.setSize(50,50);
label0.setVisible(true);
JLabel label1=new JLabel("ID");
label1.setSize(50,50);
label1.setVisible(true);
JLabel label2=new JLabel("AGE");
label2.setSize(50,50);
label2.setVisible(true);
JLabel label3=new JLabel("ADDRESS");
label3.setSize(50,50);
label3.setVisible(true);
//Adding labels to panel2
panel2.add(label0);
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);

//Setting frame size and visiblity
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

Details about the GUI- I am trying to create a GUI which have four textbox where user can enter his details and there are three buttons available on GUI by which user can send the data or store the data. The GUI also contains labels which tells the user which textbox is for which type of data. Now the allignment of the objects are not coming the way i wanted it. All the labels are coming at one place and the size of box is very big. Tried to find a solution on stalkoverflow but got nothing.

i want something like this---- Name- textbox0 ID- textbox1 AGE- textbox2 ADDRESS- textbox3

Insert Update Delete

but getting something like---- textbox0

                     textbox1

Name ID AGE Address textbox2

                     textbox3

      Insert   Update   Delete  
Was it helpful?

Solution

This is not the best looking thing you can do, but it's simple and a good start for you.

public class SQlUI {

    public static void main(String[] args) {

        SQlUI user = new SQlUI();
        user.go();
    }

    public void go() {

        JFrame frame = new JFrame();

        JPanel buttonsPanel = new JPanel();
        JButton insert = new JButton("INSERT");
        JButton update = new JButton("UPDATE");
        JButton delete = new JButton("DELETE");
        buttonsPanel.add(insert);
        buttonsPanel.add(update);
        buttonsPanel.add(delete);
        frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

        JPanel namePanel = new JPanel();
        JLabel nameLabel = new JLabel("Name");
        JTextField nameField = new JTextField(120);
        namePanel.add(nameLabel);
        namePanel.add(nameField);

        JPanel idPanel = new JPanel();
        JLabel idLabel = new JLabel("ID");
        JTextField idField = new JTextField(120);
        idPanel.add(idLabel);
        idPanel.add(idField);

        centerPanel.add(namePanel);
        centerPanel.add(idPanel);
        frame.getContentPane().add(centerPanel, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
  • Notice that I call frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH); with the panel as the first argument and the position second, you did it the other way around.
  • Not sure why you need text fields of length 120.
  • Call frame.pack() instead of setting the size yourself.
  • See what indentation does to the code.
  • You can complete the rest of the panels by yourself.

OTHER TIPS

I suggest reading up on different layouts here. It'll give you more of an idea how you can lay your items out. You can then experiment to find out which one you actually want.

That said I find it far easier to design a GUI graphically, and IntelliJ has a pretty good GUI builder that I've often used. You'll then be able to play around with layout managers and JPanels to get the effect you actually want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top