3

例如,我想将数据插入到页面的两个类别中。

USER         |             PASSWORDS

user1        |             ******

user2        |             *******

user3        |             *********

我已插入管道符号来表示垂直分隔符。垂直分隔符应该是连续的。请帮忙。

DesignGridLayout layout= new DesignGridLayout(Panel);

JSeparator sep = new JSeparator(JSeparator.VERTICAL);

layout.row().grid(userlabel).add(passwordlabel);
4

1 回答 1

0

你可能会这样做......

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;


public class PanelAdd extends JFrame {

    JPanel panelLabels, panelPasswords  ;

    JLabel [] userLabels ;
    JPasswordField [] passwordFields;

    public PanelAdd() {

        panelLabels = new JPanel();
        panelPasswords = new JPanel();  

        GridLayout panelsLayout = new GridLayout(0, 1, 0, 5);
        GridLayout mainLayout = new GridLayout(1, 2);

        panelLabels.setLayout(panelsLayout);
        panelPasswords.setLayout(panelsLayout);

        setLayout(mainLayout);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        setSize(350, 150);
    }

    public static void main(String [] args) {

        PanelAdd add = new PanelAdd();
        add.addControls();
        add.setVisible(true);

    }

    private void addControls() {

        userLabels = new JLabel[3];
        passwordFields = new JPasswordField[3];

        panelLabels.add(new JLabel("Users"));
        panelPasswords.add(new JLabel("Passwords"));

        for ( int i = 0 ; i < 3 ; i++) {
            userLabels[i] = new JLabel("User "+i);
            passwordFields[i] = new JPasswordField();

            panelLabels.add(userLabels[i]);
            panelPasswords.add(passwordFields[i]);
        }

        add(panelLabels);
        JSeparator sep = new JSeparator(JSeparator.VERTICAL);
        add(sep);
        add(panelPasswords);

    }

}

这些GridLayout对象使您能够指定放置组件的矩形网格。网格中的每个单元格与其他单元格的高度相同,并且每个宽度与其他单元格的宽度相同。组件被垂直和水平拉伸以填充单元格。

于 2014-12-21T11:12:28.167 回答