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