我创建了一个能够加扰数据文件的简单Java应用程序。它适用于我的测试人员类,但为了将这个工具提供给一些文档开发人员,它必须有一个gui。 所以... 我正在使用带有WindowBuilder的Eclipse Kepler。作为一个新手我真的在努力与这个gui。我已经多次尝试创建gui,就像所附图片一样,但我无法“获取”所有选定的信息。我尝试过内部课程,但不知道该怎么做。 所以我真的希望有人可以提供帮助。
---经过几次评论后,我的问题不明确,我改变了问题。我希望这能说清楚我想要实现的目标。 ---
我创建了以下类:
package com.scrambledata.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.custom.CCombo;
public class gui {
private Text txtFile2Read, txtScrambleFile;
protected Shell shlScrambledata;
public String docType, docModel, fakeAddresslist, file2scramble;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
gui window = new gui();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlScrambledata.open();
shlScrambledata.layout();
while (!shlScrambledata.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
} // end if (!display.readAndDispatch()) {
//display.dispose();
} // end while (!shlScrambledata.isDisposed()) {
} // end public void open() {
/**
* Create contents of the window.
*/
private void createContents() {
shlScrambledata = new Shell();
shlScrambledata.setSize(450, 405);
shlScrambledata.setText("ScrambleData");
// Create the document type group
Group docTypeGroup = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
docTypeGroup.setLocation(10, 10);
docTypeGroup.setSize(225, 49);
docTypeGroup.setText("Choose a Document Type");
final CCombo cmbDocType = new CCombo(docTypeGroup, SWT.BORDER);
cmbDocType.setItems(new String[] {"ACC710", "COMKB", "DEB442", "DEB443", "DEB444", "DEB814", "FAC617", "INC411", "INC715", "KLT334 ", "KLT355", "KLT358", "KLT361", "OVK710", "PKOA"});
cmbDocType.setBounds(10, 18, 198, 21);
cmbDocType.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println(cmbDocType.getText());
}
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println(cmbDocType.getText());
}
});
// create the documentmodel group
Group docModelGroup = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
docModelGroup.setLocation(10, 71);
docModelGroup.setSize(225, 49);
docModelGroup.setText("Choose a document model.");
final CCombo cmbDocModel = new CCombo(docModelGroup, SWT.BORDER);
cmbDocModel.setItems(new String[] {"XML", "FlatFile", "COMklantbeeld"});
cmbDocModel.setBounds(10, 18, 198, 21);
cmbDocModel.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println(cmbDocModel.getText());
}
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println(cmbDocModel.getText());
}
});
// create the file chooser for the fake addresslist
Group grpSelectTheFake = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
grpSelectTheFake.setText("Select the fake address list.");
grpSelectTheFake.setBounds(10, 131, 333, 66);
txtFile2Read = new Text(grpSelectTheFake, SWT.BORDER);
txtFile2Read.setBounds(10, 26, 211, 21);
Button btnFileChooser = new Button(grpSelectTheFake, SWT.PUSH);
btnFileChooser.setBounds(240, 24, 75, 25);
btnFileChooser.setText("Browse");
btnFileChooser.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shlScrambledata, SWT.NULL);
String path = dialog.open();
if (path != null) {
File file = new File(path);
if (file.isFile())
displayFakeAddressListFiles(new String[] { file.toString()});
else
displayFakeAddressListFiles(file.list());
}
}
});
// create the filechooser for the file to be scrambled
Group grpSelectScrambleFile = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
grpSelectScrambleFile.setText("Select the file that you want to scramble.");
grpSelectScrambleFile.setBounds(10, 213, 333, 66);
txtScrambleFile = new Text(grpSelectScrambleFile, SWT.BORDER);
txtScrambleFile.setBounds(10, 26, 211, 21);
Button btnSelectScrambleFile = new Button(grpSelectScrambleFile, SWT.NONE);
btnSelectScrambleFile.setBounds(240, 24, 75, 25);
btnSelectScrambleFile.setText("Browse");
btnSelectScrambleFile.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shlScrambledata, SWT.NULL);
String path = dialog.open();
if (path != null) {
File file = new File(path);
if (file.isFile())
displayScrambleFiles(new String[] { file.toString()});
else
displayScrambleFiles(file.list());
}
}
});
Button btnScramble = new Button(shlScrambledata, SWT.NONE);
btnScramble.setBounds(23, 309, 75, 25);
btnScramble.setText("Scramble!");
btnScramble.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
System.out.println("DocType: " );
System.out.println("DocModel: " );
System.out.println("AddressFile: " );
System.out.println("File2Scramble: " );
}
});
} // end createContents
public void displayFakeAddressListFiles(String[] files) {
for (int i = 0; files != null && i < files.length; i++) {
txtFile2Read.setText(files[i]);
txtFile2Read.setEditable(true);
System.out.println(txtFile2Read.getText());
}
} // end method public void displayFakeAddressListFiles(String[] files) {
public void displayScrambleFiles(String[] files) {
for (int i = 0; files != null && i < files.length; i++) {
txtScrambleFile.setText(files[i]);
txtScrambleFile.setEditable(true);
System.out.println(txtFile2Read.getText());
}
} // end method public void displayScrambleFiles(String[] files) {
} // end public class gui {
这将创建以下屏幕:
当我在组合框中选择一个值时,我会在Eclipse控制台中看到所选的值。如果我选择假地址,我会在我的gui中看到所选的值。我也在控制台中获取所选值。 当我选择想要加扰的文件时,我确实在我的gui中看到了正确的文件,但是我的控制台显示了来自前一个filechooser的信息(假地址)。
问题1:如何在我的控制台中获取正确的信息(所以fakeaddresslist选择器和file2scramble选择器)。我希望我必须使用内部类,但不能让它工作。
如果我按下Scramble!按钮我想开始实际的争夺类。此类需要文档类型,模型,file2scramble和fakeaddresslist。所以我需要知道用户在上面的字段中选择的信息。对于这个测试,我想在控制台中打印选定/给定的信息,只是为了证明我拥有它。
Qeustion 2:我如何获取并“存储”组合框和文件追踪者的信息?
我希望我的问题现在清楚,并且有人能够让我更进一步。
亲切的问候, MIJNO
答案 0 :(得分:1)
好的,您需要做的就是通过调用Widget
来获取getText()
中的文字。它不是必要的(但如果您愿意,仍然可以)将它们存储在String
中。
我冒昧地将你的代码搞得一团糟。它现在使用Layout
而不是绝对定位,这使它更加健壮。如果您不了解布局,请阅读this。
好的,这是:
import java.io.*;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Gui
{
protected Shell shlScrambledata;
private CCombo cmbDocType;
private CCombo cmbDocModel;
private Text txtFile2Read;
private Text txtScrambleFile;
public static void main(String[] args)
{
try
{
Gui window = new Gui();
window.open();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void open()
{
Display display = Display.getDefault();
createContents();
shlScrambledata.open();
shlScrambledata.pack();
while (!shlScrambledata.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}
private void createContents()
{
shlScrambledata = new Shell();
shlScrambledata.setText("ScrambleData");
shlScrambledata.setLayout(new GridLayout(2, false));
// Create the document type group
Group docTypeGroup = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
docTypeGroup.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
docTypeGroup.setLayout(new GridLayout(1, false));
docTypeGroup.setText("Choose a Document Type");
cmbDocType = new CCombo(docTypeGroup, SWT.BORDER);
cmbDocType.setItems(new String[] { "ACC710", "COMKB", "DEB442", "DEB443", "DEB444", "DEB814", "FAC617", "INC411", "INC715", "KLT334 ", "KLT355", "KLT358", "KLT361", "OVK710", "PKOA" });
cmbDocType.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// Create the document model group
Group docModelGroup = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
docModelGroup.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
docModelGroup.setLayout(new GridLayout(1, false));
docModelGroup.setText("Choose a document model.");
cmbDocModel = new CCombo(docModelGroup, SWT.BORDER);
cmbDocModel.setItems(new String[] { "XML", "FlatFile", "COMklantbeeld" });
cmbDocModel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// create the file chooser for the fake addresslist
Group grpSelectTheFake = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
grpSelectTheFake.setText("Select the fake address list.");
grpSelectTheFake.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
grpSelectTheFake.setLayout(new GridLayout(2, false));
txtFile2Read = new Text(grpSelectTheFake, SWT.BORDER);
txtFile2Read.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnFileChooser = new Button(grpSelectTheFake, SWT.PUSH);
btnFileChooser.setText("Browse");
btnFileChooser.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
FileDialog dialog = new FileDialog(shlScrambledata, SWT.NULL);
String path = dialog.open();
if (path != null)
{
File file = new File(path);
txtFile2Read.setText(file.getAbsolutePath());
}
}
});
// create the filechooser for the file to be scrambled
Group grpSelectScrambleFile = new Group(shlScrambledata, SWT.SHADOW_ETCHED_IN);
grpSelectScrambleFile.setText("Select the file that you want to scramble.");
grpSelectScrambleFile.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
grpSelectScrambleFile.setLayout(new GridLayout(2, false));
txtScrambleFile = new Text(grpSelectScrambleFile, SWT.BORDER);
txtScrambleFile.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnSelectScrambleFile = new Button(grpSelectScrambleFile, SWT.NONE);
btnSelectScrambleFile.setText("Browse");
btnSelectScrambleFile.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
FileDialog dialog = new FileDialog(shlScrambledata, SWT.NULL);
String path = dialog.open();
if (path != null)
{
File file = new File(path);
txtScrambleFile.setText(file.getAbsolutePath());
}
}
});
// Create the scramble button
Button btnScramble = new Button(shlScrambledata, SWT.NONE);
shlScrambledata.setDefaultButton(btnScramble);
btnScramble.setText("Scramble!");
btnScramble.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
System.out.println("DocType: " + cmbDocType.getText());
System.out.println("DocModel: " + cmbDocModel.getText());
System.out.println("AddressFile: " + txtFile2Read.getText());
System.out.println("File2Scramble: " + txtScrambleFile.getText());
}
});
}
}
看起来像这样:
并在您单击按钮时打印所选项目。