JavaGUI,组织一个对话框从用户那里获取数据

JavaGUI,组织一个对话框从用户那里获取数据,java,swing,user-interface,Java,Swing,User Interface,我正在为我的研究项目设计一个图形用户界面。我想创建一个对话框,从用户那里获取信息。以下是截图: 下面是上面截图的代码: JTextField projnameField=新的JTextField(10); JTextField nField=新的JTextField(5); JTextField mField=新的JTextField(5); JTextField alphaField=新的JTextField(5); JTextField kField=新的JTextField(5); JF

我正在为我的研究项目设计一个图形用户界面。我想创建一个对话框,从用户那里获取信息。以下是截图:

下面是上面截图的代码:

JTextField projnameField=新的JTextField(10);
JTextField nField=新的JTextField(5);
JTextField mField=新的JTextField(5);
JTextField alphaField=新的JTextField(5);
JTextField kField=新的JTextField(5);
JFileChooser inputfile=新的JFileChooser();
inputfile.setFileSelectionMode(JFileChooser.FILES\u和\u目录);
File File=inputfile.getSelectedFile();
字符串fullpath=file.getAbsolutePath();
JPanel myPanel=新的JPanel();
添加(新JLabel(“项目名称:”);
myPanel.add(projnameField);
添加(新JLabel(“实例数:”);
myPanel.add(nField);
添加(新的JLabel(“属性数:”);
myPanel.add(mField);
添加(新的JLabel(“Alpha:”);
myPanel.add(alphaField);
添加(新的JLabel(“模式数:”);
myPanel.add(kField);
添加(新的JLabel(“请选择您的数据集:”);
添加(输入文件);
设置布局(新的框布局(myPanel,BoxLayout.Y_轴));
int result=JOptionPane.showConfirmDialog(
null,myPanel,“CPM程序”,JOptionPane.OK\u CANCEL\u选项);
double alpha=double.parseDouble(alphaField.getText());
int numpat=Integer.parseInt(kField.getText());
int num_inst=Integer.parseInt(nField.getText());
int num_attr=Integer.parseInt(mField.getText());
字符串projname=(projnameField.getText());
关于上图,我有两个问题:

  • 请注意,标签居中。我怎样才能像这样把它们放在左边:

    Project name:        --textbox--
    Number of instances: --textbox--
    
  • 在标签“请选择您的数据集”中,我想浏览文件,选择它并将完整路径复制到“请选择您的数据集”标签前面的空白框中,但我不知道该怎么做

  • 1-第一个问题是,所有标签(如项目名称或实例数)都居中(看起来是!)。我怎样才能把它们放在左边

    JLabel有一个构造函数,该构造函数将int作为参数之一,您可以使用它定位保存在JLabel中的文本

    2-第二个问题是文本字段不在标签的前面,而在标签的下面。我希望每个文本字段都位于标签前面,例如:

    布局是这里的关键。考虑使用GRIDBAGHORD(可能会有点难以使用)或MigLayout(更容易使用,但你必须先下载它),以便使用GUI的更表结构。 例如,请查看我在中的代码,以获取使用GridBagLayout的表格结构示例。

    您可以在第一节中使用。例如

    import java.awt.*;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.*;
    
    class TwoColumnLayout {
    
        /**
         * Provides a JPanel with two columns (labels & fields) laid out using
         * GroupLayout. The arrays must be of equal size.
         *
         * Typical fields would be single line textual/input components such as
         * JTextField, JPasswordField, JFormattedTextField, JSpinner, JComboBox,
         * JCheckBox.. & the multi-line components wrapped in a JScrollPane -
         * JTextArea or (at a stretch) JList or JTable.
         *
         * @param labels The first column contains labels.
         * @param fields The last column contains fields.
         * @param addMnemonics Add mnemonic by next available letter in label text.
         * @return JComponent A JPanel with two columns of the components provided.
         */
        public static JComponent getTwoColumnLayout(
                JLabel[] labels,
                JComponent[] fields,
                boolean addMnemonics) {
            if (labels.length != fields.length) {
                String s = labels.length + " labels supplied for "
                        + fields.length + " fields!";
                throw new IllegalArgumentException(s);
            }
            JComponent panel = new JPanel();
            GroupLayout layout = new GroupLayout(panel);
            panel.setLayout(layout);
            // Turn on automatically adding gaps between components
            layout.setAutoCreateGaps(true);
            // Create a sequential group for the horizontal axis.
            GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
            GroupLayout.Group yLabelGroup = layout.createParallelGroup(GroupLayout.Alignment.TRAILING);
            hGroup.addGroup(yLabelGroup);
            GroupLayout.Group yFieldGroup = layout.createParallelGroup();
            hGroup.addGroup(yFieldGroup);
            layout.setHorizontalGroup(hGroup);
            // Create a sequential group for the vertical axis.
            GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
            layout.setVerticalGroup(vGroup);
    
            int p = GroupLayout.PREFERRED_SIZE;
            // add the components to the groups
            for (JLabel label : labels) {
                yLabelGroup.addComponent(label);
            }
            for (Component field : fields) {
                yFieldGroup.addComponent(field, p, p, p);
            }
            for (int ii = 0; ii < labels.length; ii++) {
                vGroup.addGroup(layout.createParallelGroup().
                        addComponent(labels[ii]).
                        addComponent(fields[ii], p, p, p));
            }
    
            if (addMnemonics) {
                addMnemonics(labels, fields);
            }
    
            return panel;
        }
    
        private final static void addMnemonics(
                JLabel[] labels,
                JComponent[] fields) {
            Map<Character, Object> m = new HashMap<Character, Object>();
            for (int ii = 0; ii < labels.length; ii++) {
                labels[ii].setLabelFor(fields[ii]);
                String lwr = labels[ii].getText().toLowerCase();
                for (int jj = 0; jj < lwr.length(); jj++) {
                    char ch = lwr.charAt(jj);
                    if (m.get(ch) == null && Character.isLetterOrDigit(ch)) {
                        m.put(ch, ch);
                        labels[ii].setDisplayedMnemonic(ch);
                        break;
                    }
                }
            }
        }
    
        /**
         * Provides a JPanel with two columns (labels & fields) laid out using
         * GroupLayout. The arrays must be of equal size.
         *
         * @param labelStrings Strings that will be used for labels.
         * @param fields The corresponding fields.
         * @return JComponent A JPanel with two columns of the components provided.
         */
        public static JComponent getTwoColumnLayout(
                String[] labelStrings,
                JComponent[] fields) {
            JLabel[] labels = new JLabel[labelStrings.length];
            for (int ii = 0; ii < labels.length; ii++) {
                labels[ii] = new JLabel(labelStrings[ii]);
            }
            return getTwoColumnLayout(labels, fields);
        }
    
        /**
         * Provides a JPanel with two columns (labels & fields) laid out using
         * GroupLayout. The arrays must be of equal size.
         *
         * @param labels The first column contains labels.
         * @param fields The last column contains fields.
         * @return JComponent A JPanel with two columns of the components provided.
         */
        public static JComponent getTwoColumnLayout(
                JLabel[] labels,
                JComponent[] fields) {
            return getTwoColumnLayout(labels, fields, true);
        }
    
        public static String getProperty(String name) {
            return name + ": \t"
                    + System.getProperty(name)
                    + System.getProperty("line.separator");
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    JTextField projnameField = new JTextField(10);
                    JTextField nField = new JTextField(5);
                    JTextField mField = new JTextField(5);
                    JTextField alphaField = new JTextField(5);
                    JTextField kField = new JTextField(5);
    
                    JTextField[] components = {
                        projnameField,
                        nField,
                        mField,
                        alphaField,
                        kField
                    };
    
                    String[] labels = {
                        "Project Name:",
                        "Number of instances:",
                        "Number of attributes:",
                        "Alpha:",
                        "Number of patterns:"
                    };
    
                    JOptionPane.showMessageDialog(null, 
                            getTwoColumnLayout(labels,components));
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
            SwingUtilities.invokeLater(r);
        }
    }
    

    import java.awt.*;
    导入java.util.HashMap;
    导入java.util.Map;
    导入javax.swing.*;
    第二类列布局{
    /**
    *提供一个JPanel,其中包含两列(标签和字段),使用
    *GroupLayout。数组的大小必须相等。
    *
    *典型的字段是单行文本/输入组件,例如
    *JTextField、JPasswordField、JFormattedTextField、JSpinner、JComboBox、,
    *JCheckBox..&包装在JScrollPane中的多行组件-
    *JTextArea或(在一定范围内)JList或JTable。
    *
    *@param labels第一列包含标签。
    *@param fields最后一列包含字段。
    *@param Add助记符通过标签文本中的下一个可用字母添加助记符。
    *@return JComponent带有两列组件的JPanel。
    */
    公共静态JComponent布局(
    JLabel[]标签,
    JComponent[]字段,
    布尔加法(助记符){
    if(labels.length!=fields.length){
    字符串s=labels.length+“为提供的标签”
    +fields.length+“fields!”;
    抛出新的IllegalArgumentException;
    }
    JComponent panel=newjpanel();
    GroupLayout布局=新的GroupLayout(面板);
    面板设置布局(布局);
    //启用“自动在零部件之间添加间隙”
    layout.setAutoCreateGaps(真);
    //为水平轴创建顺序组。
    GroupLayout.SequentialGroup hGroup=layout.CreateSquentialGroup();
    GroupLayout.Group=layout.createParallelGroup(GroupLayout.Alignment.TRAILING);
    hgroups.addGroup(yLabelGroup);
    GroupLayout.Group yFieldGroup=layout.createParallelGroup();
    hGroup.addGroup(yFieldGroup);
    布局。setHorizontalGroup(hGroup);
    //为垂直轴创建顺序组。
    GroupLayout.SequentialGroup vGroup=layout.CreateSquentialGroup();
    布局。setVerticalGroup(vGroup);
    int p=组布局。首选大小;
    //将组件添加到组中
    用于(JLabel标签:标签){
    yLabelGroup.addComponent(标签);
    }
    用于(组件字段:字段){
    yFieldGroup.addComponent(字段,p,p,p);
    }
    对于(int ii=0;ii