简体   繁体   中英

Choose Layout for JDialog Java

I am having a hard time picking the right Layout for my JDialog. I want to have a component in each line, with a label before it.

I tried to set the panel's layout to nulll and then use setBounds, but it's just too confusing(especially for my very first gui code).

I tried the GridLayout too (with the specified dimensions above) but still don't get the intended layout (I get one component in each line, without its label before it).

Here's a sample code of my JDialog and it's parent frame :

public class Test {

private JDialog dialogTest;

private JPanel dialogTestPane;
private JPanel buttonDialogPane;
private JPanel okCancelPane;

private JTextField aTextField;

private JLabel textLabel;
private JLabel buttonLabel;

private JButton buttonTest;
private JButton buttonDialog;
private JButton buttonOk;
private JButton buttonCancel;

private JFrame testFrame;

public Test(){
    createMainFrame();

}

private void createDialog(){
        //Set jdialog
     dialogTest = new JDialog();
     dialogTest.setLayout(new BorderLayout());
     dialogTestPane = new JPanel();
     //dialogTestPane.setLayout(null);
     dialogTestPane.setLayout(new GridLayout(5,5,5,5));

     textLabel = new JLabel("Text:");
     //textLabel.setBounds(10,20,46,16);
     dialogTestPane.add(textLabel);

     aTextField = new JTextField();
    // aTextField.setBounds(70,20,80,45);
     dialogTestPane.add(aTextField);

     buttonLabel = new JLabel("Button:");
    // buttonLabel.setBounds(10,60,46,16);
     dialogTestPane.add(buttonLabel);

     buttonTest = new JButton("Test");
     //buttonTest.setLocation(70,60);
     dialogTestPane.add(buttonTest);

     dialogTest.add(dialogTestPane, BorderLayout.CENTER);

     //Ok,cancel buttons
     okCancelPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
     buttonOk = new JButton("Ok");
     buttonCancel = new JButton("Cancel");
     okCancelPane.add(buttonOk);
     okCancelPane.add(buttonCancel);

     dialogTest.add(okCancelPane, BorderLayout.SOUTH);
     dialogTest.setVisible(true);

}
private void createMainFrame(){
       //Set frame of buttonTest -> No layout issues here
     testFrame = new JFrame();
     buttonDialogPane = new JPanel(new FlowLayout());
     buttonDialog = new JButton("Get Dialog");
     buttonDialogPane.add(buttonDialog);
     testFrame.add(buttonDialogPane);

     //Showing Dialog

     buttonDialog.addActionListener((ActionEvent ae) -> {
         createDialog();
     });
     testFrame.setVisible(true);
}


}

The Main class :

public class TestMain {

 public static void main(String[] args){
       Test test = new Test();
    }

}

Any help would be appreciated! Thank you !

There is many ways to do what you want, so I will present how I like to do it. I'm using GroupLayout, you can read more about it here: How to Use GroupLayout

Implementation

So first you create your components

dialogTestPane = new JPanel();

textLabel = new JLabel("Text:");
aTextField = new JTextField();
buttonLabel = new JLabel("Button:");
buttonTest = new JButton("Test");

then you have to create GroupLayout and set it for your JPanel()

GroupLayout groupLayout = new GroupLayout(dialogTestPane);
groupLayout.setAutoCreateGaps(true);
dialogTestPane.setLayout(groupLayout);

Now you set horizontal group, in your case it means that you create 2 groups, one for Labels and other one for Components.

groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
        .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(textLabel)
                .addComponent(buttonLabel))
        .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(aTextField)
                .addComponent(buttonTest)));

Next step is to set Vertical group, it means that you create group for every Label and Component.

groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
        .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(textLabel)
                .addComponent(aTextField))
        .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(buttonLabel)
                .addComponent(buttonTest)));

And finally you have to add your JPanel to superior one

dialogTest.add(dialogTestPane);

And that's it! For bigger number of components i recommend to keep your labels and component in arrays and set group layout in loop to avoid hundreds of lines just for setting layout.

Well, it can be done in other ways, with for example MiGLayout or mixing other layout managers. Useful links:

  1. Using Layout Managers
  2. MiGLayout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM