Java GUI,组织一个对话框以从用户获取数据 [英] Java GUI, organizing a dialog box to get data from the user

查看:128
本文介绍了Java GUI,组织一个对话框以从用户获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am designing a GUI for my research project. I want to create a dialog box that gets information from the user. Here is the screenshot:

以下是截图的代码:

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);

JFileChooser inputfile = new JFileChooser();
inputfile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

File file = inputfile.getSelectedFile();
String fullpath = file.getAbsolutePath();

JPanel myPanel = new JPanel();

myPanel.add(new JLabel("Project Name:"));
myPanel.add(projnameField);

myPanel.add(new JLabel("Number of instances:"));
myPanel.add(nField);

myPanel.add(new JLabel("Number of attributes:"));
myPanel.add(mField);

myPanel.add(new JLabel("Alpha:"));
myPanel.add(alphaField);

myPanel.add(new JLabel("Number of patterns:"));
myPanel.add(kField);

myPanel.add(new JLabel("Please select your datset:"));
myPanel.add(inputfile);

myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

int result = JOptionPane.showConfirmDialog(
    null, myPanel, "CPM Program", JOptionPane.OK_CANCEL_OPTION);

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());
String projname = (projnameField.getText());

参考上图,我有两个问题:

In reference to the above image I have two questions:


  1. 请注意标签居中。我怎样才能把这些放在左侧:

  1. Notice the labels are centered. How can I put those in the left side like this:

Project name:        --textbox--
Number of instances: --textbox--


  • 在标签中,请选择您的数据集,我想浏览文件,选择它并在请选择您的数据集标签前面的空白框中复制完整路径,但我不知道该怎么做。

  • In the label, "Please select you dataset", I want to browse the file, select it and copy the full path in a blank box in the front of "Please select you dataset" label, but I do not know how I should do it.


    推荐答案


    1-第一个问题是所有标签,例如项目名称或者实例数量居中(似乎!)。如何将它们放在左侧?

    1- The first problem is that all of the labels such as project name or number of instances are centered (it seems!). How can I put those in the left side?

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

    JLabel has a constructor that takes an int as one of the parameters, and you can use it to position your text held in the JLabel.


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

    2- The second problem is that text fields are not in the front of labels and are below of them. I want to have each text field in the front of the label such as:

    布局是关键所在。考虑使用GridBagLayout(最初可能有些难以使用)或MigLayout(更容易使用但必须先下载)以允许为GUI使用更多表格结构。

    Layouts are the key here. Consider using GridBagLayout (which can be somewhat difficult to use initially) or MigLayout (easier to use but you have to download it first) to allow use of a more tabular structure for your GUI.

    例如,请参阅此答案中的代码,以获取使用GridBagLayout的表格结构示例。

    For example, please have a look at my code in this answer for an example of tabular structure using GridBagLayout.

    这篇关于Java GUI,组织一个对话框以从用户获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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