如何使用Java Swing布局管理器来制作这个GUI? [英] How to use Java Swing layout manager to make this GUI?

查看:115
本文介绍了如何使用Java Swing布局管理器来制作这个GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图弄清楚应该在JFrame上使用哪些布局来完成此布局。我正在尝试编写GUI而不是使用可视化GUI制作工具。到目前为止,我只能看到这样:



这是源代码对于上面的GUI:



您需要将它们分解为各自的组件并专注于布局和功能要求



第01部分





所以,这是非常基本的

 公共类SourcePane扩展JPanel {
private JTextField datasourceName ;
私人JTextField desciption;

public SourcePane(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;

add(new JLabel(Data Source Name:),gbc);
gbc.gridy ++;
add(new JLabel(Description:),gbc);

gbc.gridx ++;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;

add((datasourceName = new JTextField(10)),gbc);
gbc.gridy ++;
add((desciption = new JTextField(10)),gbc);
}

public String getDataSourceName(){
return datasourceName.getText();
}

public String getDescription(){
return desciption.getText();
}

public void setDataSourceName(String name){
datasourceName.setText(name);
}

public void setDescription(String description){
desciption.setText(description);
}

}

我还添加了一些访问者,我将不会添加到其余的代码中,但提供了如何在组件之间获取/设置信息的想法



Part 02





这有点困难,因为有一个额外的建议数据库旁边的标签:标签。它可能可以在一个布局中完成,但是使用额外的容器并进一步复合布局会更容易

 公共类DatabasePane扩展JPanel {

私有JButton select,create,repair,compact;
私有JLabel数据库;

public DatabasePane(){
setLayout(new GridBagLayout());
setBorder(new CompoundBorder(new TitledBorder(Database),new EmptyBorder(12,0,0,0)));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0,0,0,4);

JPanel panel = new JPanel(new GridBagLayout());
panel.add(new JLabel(Database:),gbc);
gbc.gridx ++;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0,0,0,0);
panel.add((database = new JLabel()),gbc);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4,4,4,4);
add(panel,gbc);

gbc.gridwidth = 1;
gbc.weightx = 0.25;
gbc.gridy ++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add((select = new JButton(Select)),gbc);
gbc.gridx ++;
add((create = new JButton(Create)),gbc);
gbc.gridx ++;
add((repair = new JButton(Repair)),gbc);
gbc.gridx ++;
add((compact = new JButton(Compact)),gbc);
}

}



第03部分





再次,这似乎有点复杂,因为数据库:按钮似乎有一个额外的标签。您可以简单地使用按钮 text 属性,但我选择进一步演示复合布局的想法

  public class SystemDatabasePane扩展JPanel {

private JRadioButton none,database;
private JLabel databaseLabel;
private JButton systemDatabase;

public SystemDatabasePane(){
setLayout(new GridBagLayout());
setBorder(new CompoundBorder(new TitledBorder(System Database),new EmptyBorder(8,0,0,0)));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0,0,0,4);
gbc.anchor = GridBagConstraints.WEST;

JPanel panel = new JPanel(new GridBagLayout());
panel.add((none = new JRadioButton(None)),gbc);
gbc.gridy ++;
panel.add((none = new JRadioButton(Database:)),gbc);

gbc.gridx ++;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add((databaseLabel = new JLabel()),gbc);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4,4,4,4);
add(panel,gbc);

gbc.gridy ++;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
add((systemDatabase = new JButton(System Database ...)),gbc);
systemDatabase.setEnabled(false);
}

}



第04部分





最后,动作面板。这实际上相对简单,但是利用 GridBagLayout 及其约束的属性来在单个布局中完成整个事情

 公共类ActionPane扩展JPanel {

私有JButton好,取消,帮助,高级,选项;

public ActionPane(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(4,4,4,4);

add((okay = new JButton(Ok)),gbc);
gbc.gridy ++;
add((cancel = new JButton(Cancel)),gbc);
gbc.gridy ++;
add((help = new JButton(Help)),gbc);
gbc.gridy ++;
add((advanced = new JButton(Advanced)),gbc);
gbc.gridy ++;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTH;
add((options = new JButton(Options>>)),gbc);
}

}



全部放在一起





这样就可以将所有单独的元素组合成一个布局

 公共类DatabasePropertiesPane扩展JPanel {

private SourcePane sourcePane;
private DatabasePane databasePane;
private SystemDatabasePane systemDatabasePane;
私人ActionPane actionPane;

public DatabasePropertiesPane(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 0.33;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(4,4,4,4);

add((sourcePane = new SourcePane()),gbc);
gbc.gridy ++;
add((databasePane = new DatabasePane()),gbc);
gbc.gridy ++;
add((systemDatabasePane = new SystemDatabasePane()),gbc);

gbc.gridy = 0;
gbc.gridx ++;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1;
gbc.weightx = 0;
add((actionPane = new ActionPane()),gbc);
}

}



Runnable示例



  import java.awt.Color; 
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class TestLayout {

public static void main(String [] args){
new TestLayout();
}

public TestLayout(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}

JFrame frame = new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DatabasePropertiesPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

公共类DatabasePropertiesPane扩展JPanel {

private SourcePane sourcePane;
private DatabasePane databasePane;
private SystemDatabasePane systemDatabasePane;
私人ActionPane actionPane;

public DatabasePropertiesPane(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 0.33;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(4,4,4,4);

add((sourcePane = new SourcePane()),gbc);
gbc.gridy ++;
add((databasePane = new DatabasePane()),gbc);
gbc.gridy ++;
add((systemDatabasePane = new SystemDatabasePane()),gbc);

gbc.gridy = 0;
gbc.gridx ++;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1;
gbc.weightx = 0;
add((actionPane = new ActionPane()),gbc);
}

}

公共类SourcePane扩展JPanel {
private JTextField datasourceName;
私人JTextField desciption;

public SourcePane(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;

add(new JLabel(Data Source Name:),gbc);
gbc.gridy ++;
add(new JLabel(Description:),gbc);

gbc.gridx ++;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;

add((datasourceName = new JTextField(10)),gbc);
gbc.gridy ++;
add((desciption = new JTextField(10)),gbc);
}

public String getDataSourceName(){
return datasourceName.getText();
}

public String getDescription(){
return desciption.getText();
}

public void setDataSourceName(String name){
datasourceName.setText(name);
}

public void setDescription(String description){
desciption.setText(description);
}

}

公共类DatabasePane扩展JPanel {

私有JButton select,create,repair,compact;
私有JLabel数据库;

public DatabasePane(){
setLayout(new GridBagLayout());
setBorder(new CompoundBorder(new TitledBorder(Database),new EmptyBorder(12,0,0,0)));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0,0,0,4);

JPanel panel = new JPanel(new GridBagLayout());
panel.add(new JLabel(Database:),gbc);
gbc.gridx ++;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0,0,0,0);
panel.add((database = new JLabel()),gbc);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4,4,4,4);
add(panel,gbc);

gbc.gridwidth = 1;
gbc.weightx = 0.25;
gbc.gridy ++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add((select = new JButton(Select)),gbc);
gbc.gridx ++;
add((create = new JButton(Create)),gbc);
gbc.gridx ++;
add((repair = new JButton(Repair)),gbc);
gbc.gridx ++;
add((compact = new JButton(Compact)),gbc);
}

}

公共类SystemDatabasePane扩展JPanel {

private JRadioButton none,database;
private JLabel databaseLabel;
private JButton systemDatabase;

public SystemDatabasePane(){
setLayout(new GridBagLayout());
setBorder(new CompoundBorder(new TitledBorder(System Database),new EmptyBorder(8,0,0,0)));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0,0,0,4);
gbc.anchor = GridBagConstraints.WEST;

JPanel panel = new JPanel(new GridBagLayout());
panel.add((none = new JRadioButton(None)),gbc);
gbc.gridy ++;
panel.add((none = new JRadioButton(Database:)),gbc);

gbc.gridx ++;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add((databaseLabel = new JLabel()),gbc);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4,4,4,4);
add(panel,gbc);

gbc.gridy ++;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
add((systemDatabase = new JButton(System Database ...)),gbc);
systemDatabase.setEnabled(false);
}

}

公共类ActionPane扩展JPanel {

私有JButton没关系,取消,帮助,高级,选项;

public ActionPane(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(4,4,4,4);

add((okay = new JButton(Ok)),gbc);
gbc.gridy ++;
add((cancel = new JButton(Cancel)),gbc);
gbc.gridy ++;
add((help = new JButton(Help)),gbc);
gbc.gridy ++;
add((advanced = new JButton(Advanced)),gbc);
gbc.gridy ++;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTH;
add((options = new JButton(Options>>)),gbc);
}

}
}

有一个查看在容器中布置组件如何使用GridBagLayout 获取更多详细信息


I am trying to figure out what layouts should be used on the JFrame to get this layout accomplished. I am trying to code the GUI rather than use visual GUI-making tools. So far I was only able to get it to look like this:

This is the source code for the GUI above: http://pastebin.com/s06pareG

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

            JPanel upPanel = new JPanel();
            upPanel.setLayout(new GridLayout(2,3));

            JLabel data = new JLabel("Data Source Name:");
            upPanel.add(data);
            JTextField dataText = new JTextField();
            upPanel.add(dataText);
            JLabel desc = new JLabel("Description:");
            upPanel.add(desc);
            JTextField descText = new JTextField();
            upPanel.add(descText);

            JPanel midPanel = new JPanel();
            midPanel.setBorder(new TitledBorder(null, "Database", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            JLabel dbTitle = new JLabel("Database");
            JButton select = new JButton("Select...");
            JButton create = new JButton("Create...");
            JButton repair = new JButton("Repair...");
            JButton compact = new JButton("Compact...");

            JPanel eastPanel = new JPanel();
            eastPanel.setLayout(new GridLayout(4,1));

            JButton ok = new JButton("OK");
            JButton cancel = new JButton("Cancel");
            JButton help = new JButton("Help");
            JButton advanced = new JButton("Advanced...");
            eastPanel.add(ok); eastPanel.add(cancel); eastPanel.add(help); eastPanel.add(advanced);

            frame.getContentPane().add(upPanel, BorderLayout.NORTH);
            frame.getContentPane().add(midPanel, BorderLayout.WEST);
            midPanel.setLayout(new BorderLayout(0, 0));
            midPanel.add(dbTitle);
            midPanel.add(select);
            midPanel.add(create);
            midPanel.add(repair);
            midPanel.add(compact);
            frame.getContentPane().add(eastPanel, BorderLayout.EAST);

    }

I was thinking about making the JFrame absolute layout and then creating 4 JPanels with GridLayout. Also, I'm having trouble making the "Database:" label sit on its own row and placing the JButtons below it. What types of layouts and customizing functions should I look into to accomplish this look?

解决方案

Break down you layout into it's basic areas of responsibility, focusing each area individually and managing it's own layout requirements

So, as I see, you have four basic areas of functionality...

You need to break these apart into their own individual components and focus on there layout and functional requirements

Part 01

So, this is pretty basic

public class SourcePane extends JPanel {
    private JTextField datasourceName;
    private JTextField desciption;

    public SourcePane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;

        add(new JLabel("Data Source Name: "), gbc);
        gbc.gridy++;
        add(new JLabel("Description: "), gbc);

        gbc.gridx++;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        add((datasourceName = new JTextField(10)), gbc);
        gbc.gridy++;
        add((desciption = new JTextField(10)), gbc);
    }

    public String getDataSourceName() {
        return datasourceName.getText();
    }

    public String getDescription() {
        return desciption.getText();
    }

    public void setDataSourceName(String name) {
        datasourceName.setText(name);
    }

    public void setDescription(String description) {
        desciption.setText(description);
    }

}

I've also added some accessors, which I won't be adding to the rest of the code, but provides the idea of how you might get/set information between the components

Part 02

This is suitably more difficult, as there is a suggest of an additional label next to the Database: label. It "might" be possible to get this done in a single layout, but it would be easier to use an additional container and further compound the layouts

public class DatabasePane extends JPanel {

    private JButton select, create, repair, compact;
    private JLabel database;

    public DatabasePane() {
        setLayout(new GridBagLayout());
        setBorder(new CompoundBorder(new TitledBorder("Database"), new EmptyBorder(12, 0, 0, 0)));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 0, 0, 4);

        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(new JLabel("Database: "), gbc);
        gbc.gridx++;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 0, 0, 0);
        panel.add((database = new JLabel()), gbc);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(4, 4, 4, 4);
        add(panel, gbc);

        gbc.gridwidth = 1;
        gbc.weightx = 0.25;
        gbc.gridy++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add((select = new JButton("Select")), gbc);
        gbc.gridx++;
        add((create = new JButton("Create")), gbc);
        gbc.gridx++;
        add((repair = new JButton("Repair")), gbc);
        gbc.gridx++;
        add((compact = new JButton("Compact")), gbc);
    }

}

Part 03

Again, this is a little more complex then it seems, as the Database: button seems to have an additional label. You could simply make use of the buttons text property, but I've chosen to further demonstrate the idea of compound layouts

public class SystemDatabasePane extends JPanel {

    private JRadioButton none, database;
    private JLabel databaseLabel;
    private JButton systemDatabase;

    public SystemDatabasePane() {
        setLayout(new GridBagLayout());
        setBorder(new CompoundBorder(new TitledBorder("System Database"), new EmptyBorder(8, 0, 0, 0)));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(0, 0, 0, 4);
        gbc.anchor = GridBagConstraints.WEST;

        JPanel panel = new JPanel(new GridBagLayout());
        panel.add((none = new JRadioButton("None")), gbc);
        gbc.gridy++;
        panel.add((none = new JRadioButton("Database: ")), gbc);

        gbc.gridx++;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add((databaseLabel = new JLabel("")), gbc);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(4, 4, 4, 4);
        add(panel, gbc);

        gbc.gridy++;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        add((systemDatabase = new JButton("System Database...")), gbc);
        systemDatabase.setEnabled(false);
    }

}

Part 04

And finally, the "actions" panel. This is actually relatively simply, but makes use of the GridBagLayout and the properties of it's constraints to do the whole thing in a single layout

public class ActionPane extends JPanel {

    private JButton okay, cancel, help, advanced, options;

    public ActionPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        gbc.insets = new Insets(4, 4, 4, 4);

        add((okay = new JButton("Ok")), gbc);
        gbc.gridy++;
        add((cancel = new JButton("Cancel")), gbc);
        gbc.gridy++;
        add((help = new JButton("Help")), gbc);
        gbc.gridy++;
        add((advanced = new JButton("Advanced")), gbc);
        gbc.gridy++;
        gbc.weighty = 1;
        gbc.anchor = GridBagConstraints.SOUTH;
        add((options = new JButton("Options >>")), gbc);
    }

}

Putting it all together

This then simply puts all the separate elements together into a single layout

public class DatabasePropertiesPane extends JPanel {

    private SourcePane sourcePane;
    private DatabasePane databasePane;
    private SystemDatabasePane systemDatabasePane;
    private ActionPane actionPane;

    public DatabasePropertiesPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 0.33;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(4, 4, 4, 4);

        add((sourcePane = new SourcePane()), gbc);
        gbc.gridy++;
        add((databasePane = new DatabasePane()), gbc);
        gbc.gridy++;
        add((systemDatabasePane = new SystemDatabasePane()), gbc);

        gbc.gridy = 0;
        gbc.gridx++;
        gbc.gridheight = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.VERTICAL;
        gbc.weighty = 1;
        gbc.weightx = 0;
        add((actionPane = new ActionPane()), gbc);
    }

}

Runnable example

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class TestLayout {

    public static void main(String[] args) {
        new TestLayout();
    }

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DatabasePropertiesPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class DatabasePropertiesPane extends JPanel {

        private SourcePane sourcePane;
        private DatabasePane databasePane;
        private SystemDatabasePane systemDatabasePane;
        private ActionPane actionPane;

        public DatabasePropertiesPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 0.33;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(4, 4, 4, 4);

            add((sourcePane = new SourcePane()), gbc);
            gbc.gridy++;
            add((databasePane = new DatabasePane()), gbc);
            gbc.gridy++;
            add((systemDatabasePane = new SystemDatabasePane()), gbc);

            gbc.gridy = 0;
            gbc.gridx++;
            gbc.gridheight = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.VERTICAL;
            gbc.weighty = 1;
            gbc.weightx = 0;
            add((actionPane = new ActionPane()), gbc);
        }

    }

    public class SourcePane extends JPanel {
        private JTextField datasourceName;
        private JTextField desciption;

        public SourcePane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;

            add(new JLabel("Data Source Name: "), gbc);
            gbc.gridy++;
            add(new JLabel("Description: "), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add((datasourceName = new JTextField(10)), gbc);
            gbc.gridy++;
            add((desciption = new JTextField(10)), gbc);
        }

        public String getDataSourceName() {
            return datasourceName.getText();
        }

        public String getDescription() {
            return desciption.getText();
        }

        public void setDataSourceName(String name) {
            datasourceName.setText(name);
        }

        public void setDescription(String description) {
            desciption.setText(description);
        }

    }

    public class DatabasePane extends JPanel {

        private JButton select, create, repair, compact;
        private JLabel database;

        public DatabasePane() {
            setLayout(new GridBagLayout());
            setBorder(new CompoundBorder(new TitledBorder("Database"), new EmptyBorder(12, 0, 0, 0)));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(0, 0, 0, 4);

            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(new JLabel("Database: "), gbc);
            gbc.gridx++;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 0, 0);
            panel.add((database = new JLabel()), gbc);

            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4, 4, 4, 4);
            add(panel, gbc);

            gbc.gridwidth = 1;
            gbc.weightx = 0.25;
            gbc.gridy++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add((select = new JButton("Select")), gbc);
            gbc.gridx++;
            add((create = new JButton("Create")), gbc);
            gbc.gridx++;
            add((repair = new JButton("Repair")), gbc);
            gbc.gridx++;
            add((compact = new JButton("Compact")), gbc);
        }

    }

    public class SystemDatabasePane extends JPanel {

        private JRadioButton none, database;
        private JLabel databaseLabel;
        private JButton systemDatabase;

        public SystemDatabasePane() {
            setLayout(new GridBagLayout());
            setBorder(new CompoundBorder(new TitledBorder("System Database"), new EmptyBorder(8, 0, 0, 0)));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(0, 0, 0, 4);
            gbc.anchor = GridBagConstraints.WEST;

            JPanel panel = new JPanel(new GridBagLayout());
            panel.add((none = new JRadioButton("None")), gbc);
            gbc.gridy++;
            panel.add((none = new JRadioButton("Database: ")), gbc);

            gbc.gridx++;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            panel.add((databaseLabel = new JLabel("")), gbc);

            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4, 4, 4, 4);
            add(panel, gbc);

            gbc.gridy++;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.CENTER;
            add((systemDatabase = new JButton("System Database...")), gbc);
            systemDatabase.setEnabled(false);
        }

    }

    public class ActionPane extends JPanel {

        private JButton okay, cancel, help, advanced, options;

        public ActionPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            gbc.insets = new Insets(4, 4, 4, 4);

            add((okay = new JButton("Ok")), gbc);
            gbc.gridy++;
            add((cancel = new JButton("Cancel")), gbc);
            gbc.gridy++;
            add((help = new JButton("Help")), gbc);
            gbc.gridy++;
            add((advanced = new JButton("Advanced")), gbc);
            gbc.gridy++;
            gbc.weighty = 1;
            gbc.anchor = GridBagConstraints.SOUTH;
            add((options = new JButton("Options >>")), gbc);
        }

    }
}

Have a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details

这篇关于如何使用Java Swing布局管理器来制作这个GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆