📄 dialogtest.java
字号:
fd_savefileButton.right = new FormAttachment(0, 140);
fd_savefileButton.bottom = new FormAttachment(openfileButton, 22, SWT.TOP);
fd_savefileButton.top = new FormAttachment(openfileButton, 0, SWT.TOP);
savefileButton.setLayoutData(fd_savefileButton);
savefileButton.setText("SaveFile");
final Button fontButton = new Button(systemdialogGroup, SWT.NONE);
fontButton.addMouseListener(new MouseAdapter() {
public void mouseUp(final MouseEvent e) {
FontDialog fd = new FontDialog(allDialogsTestShell, SWT.NONE);
fd.setText("Select Font");
fd.setRGB(new RGB(0, 0, 255));
FontData defaultFont = new FontData("Courier", 10, SWT.BOLD);
fd.setFontData(defaultFont);
FontData newFont = fd.open();
if (newFont == null)
return;
showLabel.setFont(new Font(showLabel.getDisplay(), newFont));
showLabel.setForeground(new Color(showLabel.getDisplay(), fd.getRGB()));
}
});
final FormData fd_fontButton = new FormData();
fd_fontButton.right = new FormAttachment(0, 200);
fd_fontButton.top = new FormAttachment(savefileButton, -22, SWT.BOTTOM);
fd_fontButton.bottom = new FormAttachment(savefileButton, 0, SWT.BOTTOM);
fd_fontButton.left = new FormAttachment(0, 155);
fontButton.setLayoutData(fd_fontButton);
fontButton.setText("Font");
final Button choosefolderButton = new Button(systemdialogGroup, SWT.NONE);
choosefolderButton.addMouseListener(new MouseAdapter() {
public void mouseUp(final MouseEvent e) {
DirectoryDialog dlg = new DirectoryDialog(allDialogsTestShell);
// Set the initial filter path according
// to anything they've selected or typed in
dlg.setFilterPath("c:/");
// Change the title bar text
dlg.setText("SWT's DirectoryDialog");
// Customizable message displayed in the dialog
dlg.setMessage("Select a directory");
// Calling open() will open and run the dialog.
// It will return the selected directory, or
// null if user cancels
String dir = dlg.open();
if (dir != null) {
// Set the text box to the new selection
showLabel.setText(dir);
}
}
});
final FormData fd_choosefolderButton = new FormData();
fd_choosefolderButton.right = new FormAttachment(0, 90);
fd_choosefolderButton.top = new FormAttachment(0, 43);
fd_choosefolderButton.bottom = new FormAttachment(0, 65);
fd_choosefolderButton.left = new FormAttachment(openfileButton, 0, SWT.LEFT);
choosefolderButton.setLayoutData(fd_choosefolderButton);
choosefolderButton.setText("ChooseFolder");
final Button printButton = new Button(systemdialogGroup, SWT.NONE);
printButton.addMouseListener(new MouseAdapter() {
public void mouseUp(final MouseEvent e) {
PrintDialog printDialog = new PrintDialog(allDialogsTestShell, SWT.NONE);
printDialog.setText("Print");
PrinterData printerData = printDialog.open();
if (!(printerData == null)) {
Printer p = new Printer(printerData);
p.startJob("PrintJob");
p.startPage();
Rectangle trim = p.computeTrim(0, 0, 0, 0);
Point dpi = p.getDPI();
int leftMargin = dpi.x + trim.x;
int topMargin = dpi.y / 2 + trim.y;
GC gc = new GC(p);
Font font = gc.getFont();
String printText = showLabel.getText();
//Point extent = gc.stringExtent(printText);
gc.drawString(printText, leftMargin, topMargin
+ font.getFontData()[0].getHeight());
p.endPage();
gc.dispose();
p.endJob();
p.dispose();
}
}
});
final FormData fd_printButton = new FormData();
fd_printButton.top = new FormAttachment(choosefolderButton, 0, SWT.TOP);
fd_printButton.right = new FormAttachment(choosefolderButton, 50, SWT.RIGHT);
fd_printButton.left = new FormAttachment(choosefolderButton, 5, SWT.RIGHT);
printButton.setLayoutData(fd_printButton);
printButton.setText("Print");
final Button colorButton = new Button(systemdialogGroup, SWT.NONE);
colorButton.addMouseListener(new MouseAdapter() {
public void mouseUp(final MouseEvent e) {
ColorDialog cd = new ColorDialog(allDialogsTestShell);
cd.setText("ColorDialog Demo");
cd.setRGB(new RGB(255, 255, 255));
RGB newColor = cd.open();
if (newColor == null) {
return;
}
showLabel.setBackground(new Color(showLabel.getDisplay(), newColor));
}
});
final FormData fd_colorButton = new FormData();
fd_colorButton.left = new FormAttachment(printButton, 5, SWT.RIGHT);
fd_colorButton.right = new FormAttachment(0, 200);
fd_colorButton.top = new FormAttachment(printButton, 0, SWT.TOP);
colorButton.setLayoutData(fd_colorButton);
colorButton.setText("Color");
final Button copyfileButton = new Button(allDialogsTestShell, SWT.NONE);
copyfileButton.addMouseListener(new MouseAdapter() {
public void mouseUp(final MouseEvent e) {
try{
FileDialog fd = new FileDialog(allDialogsTestShell, SWT.OPEN);
fd.setText("Open");
fd.setFilterPath("C:/");
String[] filterExt = { "*.txt", "*.doc", "*.rtf", "*.*" };
fd.setFilterExtensions(filterExt);
String selectedPath = fd.open();
if(selectedPath!=null){
showLabel.setText(selectedPath);
}
else{
showLabel.setText("return null");
return;
}
FileInputStream in = new FileInputStream(selectedPath);
FileDialog fd1 = new FileDialog(allDialogsTestShell, SWT.SAVE);
fd1.setText("Save");
fd1.setFilterPath("C:/");
String[] filterExt1 = { "*.txt", "*.doc", "*.rtf", "*.*" };
fd1.setFilterExtensions(filterExt1);
String savePath = fd1.open();
if(savePath!=null){
showLabel.setText(savePath);
}
else{
showLabel.setText("return null");
return;
}
FileOutputStream out=new FileOutputStream(savePath);
byte b[]=new byte[in.available()];
in.read(b);
out.write(b);
in.close();
out.close();
}
catch (FileNotFoundException Ee){
showLabel.setText("Read File Error!");
}
catch(IOException Ee){
showLabel.setText("Read File Error!");
}
}
});
final FormData fd_copyfileButton = new FormData();
fd_copyfileButton.top = new FormAttachment(0, 13);
fd_copyfileButton.bottom = new FormAttachment(0, 35);
copyfileButton.setLayoutData(fd_copyfileButton);
copyfileButton.setText("CopyFile");
Button listfilesorfoldersButton;
listfilesorfoldersButton = new Button(allDialogsTestShell, SWT.NONE);
fd_copyfileButton.left = new FormAttachment(listfilesorfoldersButton, -68, SWT.LEFT);
fd_copyfileButton.right = new FormAttachment(listfilesorfoldersButton, -5, SWT.LEFT);
listfilesorfoldersButton.addMouseListener(new MouseAdapter() {
public void mouseUp(final MouseEvent e) {
try {
DirectoryDialog dlg = new DirectoryDialog(allDialogsTestShell);
dlg.setFilterPath(".");
dlg.setText("DirectoryDialog");
dlg.setMessage("Select a directory");
String dir = dlg.open();
if (dir != null) {
showLabel.setText(dir);
}
else
{
return;
}
File path = new File(dir);
String[] list;
list = path.list();
for(int i = 0; i < list.length; i++)
{
listfileText.append(list[i]+" - ");
}
} catch(Exception Ee) {
Ee.printStackTrace();
}
}
});
final FormData fd_listfilesorfoldersButton = new FormData();
fd_listfilesorfoldersButton.top = new FormAttachment(copyfileButton, -22, SWT.BOTTOM);
fd_listfilesorfoldersButton.bottom = new FormAttachment(copyfileButton, 0, SWT.BOTTOM);
fd_listfilesorfoldersButton.left = new FormAttachment(0, 90);
fd_listfilesorfoldersButton.right = new FormAttachment(0, 210);
listfilesorfoldersButton.setLayoutData(fd_listfilesorfoldersButton);
listfilesorfoldersButton.setText("ListFilesOrFolders");
listfileText = new Text(allDialogsTestShell, SWT.BORDER);
final FormData fd_listfileText = new FormData();
fd_listfileText.top = new FormAttachment(0, 325);
fd_listfileText.right = new FormAttachment(showLabel, 0, SWT.RIGHT);
fd_listfileText.bottom = new FormAttachment(showLabel, -5, SWT.TOP);
fd_listfileText.left = new FormAttachment(systemdialogGroup, 0, SWT.LEFT);
listfileText.setLayoutData(fd_listfileText);
allDialogsTestShell.layout();
while (!allDialogsTestShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -