gpt4 book ai didi

java - 从另一个按钮禁用按钮

转载 作者:行者123 更新时间:2023-12-02 02:56:57 26 4
gpt4 key购买 nike

我想在单击 btnSearchFile 时禁用按钮 btnCopyFolder

这是我的代码:

 package org.eclipse.wb.swt;

import java.nio.file.Path;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;

public class MainUI {


private Text txtSource;
private static Text txtDestination;
protected Shell shell;
private static Text text1;
static DateTime ddFrom;
static Button btnCopyFolder;
static String text = "" ;
static String text2 = "" ;
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 400);
shell.setText("Connection Manager");

final DateTime ddFrom = new DateTime (shell, SWT.DROP_DOWN | SWT.BORDER);
final DateTime ddTo = new DateTime (shell, SWT.DROP_DOWN | SWT.BORDER);
text = String.format("%02d/%02d/%04d",ddFrom.getDay(),ddFrom.getMonth() + 1,ddFrom.getYear());
text2 = String.format("%04d%02d%02d",ddTo.getYear(), ddTo.getMonth() + 1, ddTo.getDay());
// final DateTime time = new DateTime (shell,SWT.TIME | SWT.SHORT);

Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
lblNewLabel.setBounds(10, 0, 107, 25);
lblNewLabel.setText("Source File ");

Button btnSearchFile = new Button(shell, SWT.NONE);
btnSearchFile.setBounds(103, 30, 107, 25);
btnSearchFile.setText("Search File");
btnSearchFile.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog fd = new FileDialog(shell, SWT.MULTI);
String firstFile = fd.open();
btnCopyFolder.setVisible(false);
if (firstFile != null) {
String[] selectedFiles = fd.getFileNames();
File file = new File(firstFile);
for (int ii = 0; ii < selectedFiles.length; ii++ )
{
if (file.isFile())
{
displayFiles(new String[] { file.toString()});
}
else
displayFiles(file.list());
}
}

}
});

txtSource = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL );
txtSource.setBounds(10, 61, 414, 34);

Label lblNewLabel_1 = new Label(shell, SWT.NONE);
lblNewLabel_1.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
lblNewLabel_1.setBounds(10, 96, 107, 18);
lblNewLabel_1.setText("Destination File");

final Button btnSearchDirectory = new Button(shell, SWT.NONE);
btnSearchDirectory.setText("Search Directory");
btnSearchDirectory.setBounds(216, 31, 107, 25);
btnSearchDirectory.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dlg = new DirectoryDialog(shell);
dlg.setFilterPath(txtSource.getText());
dlg.setMessage("Select a source file to transfer");
String dir = dlg.open();
if (dir != null) {
txtSource.setText(dir);
}
}
});

Button btnSearchDestination = new Button(shell, SWT.NONE);
btnSearchDestination.setBounds(10, 120, 75, 25);
btnSearchDestination.setText("Search");
btnSearchDestination.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SAVE);
dialog.setFilterPath(txtDestination.getText());
String dir = dialog.open();
if(dir != null){
txtDestination.setText(dir);
}
}
});

txtDestination = new Text(shell, SWT.BORDER);
txtDestination.setBounds(10, 151, 414, 25);
txtDestination.setText("");

Label lblNewLabel_2 = new Label(shell, SWT.NONE);
lblNewLabel_2.setBounds(10, 182, 81, 15);
lblNewLabel_2.setText("Date Range:");

Button btnCopyFile = new Button(shell, SWT.NONE);
btnCopyFile.setBounds(86, 253, 75, 25);
btnCopyFile.setText("Copy File");

btnCopyFile.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
File srcFolder = new File(txtSource.getText());
File destFolder = new File(txtDestination.getText());

if(!srcFolder.exists()){
MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
msgBox.setText("Information");
msgBox.setMessage("Directory does not exist.");
msgBox.open();
txtSource.setText("");
txtDestination.setText("");

}else{

if(txtDestination.getText().isEmpty())
{
MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
msgBox.setText("Warning");
msgBox.setMessage("Invalid Path..");
msgBox.open();
txtSource.setText("");
txtDestination.setText("");
}
else
{
try {
copyFolder1(srcFolder,destFolder);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
text1.append("Finished Copying.....\n");
MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
msgBox.setText("Information");
msgBox.setMessage("Done Copying...");
msgBox.open();
}
text1.append("");
}
}
});
Button btnCancel = new Button(shell, SWT.NONE);
btnCancel.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0)
{
text = String.format("%02d/%02d/%04d",ddFrom.getDay(),ddFrom.getMonth() + 1,ddFrom.getYear());
text2 = String.format("%04d%02d%02d",ddTo.getYear(), ddTo.getMonth() + 1, ddTo.getDay());
String txt = ("Date Range is: " + " From "+ text + " : " + " To " + text2 + "\n");
text1.append(txt);
txtSource.setText("");
txtDestination.setText("");

}
});
btnCancel.setBounds(167, 253, 75, 25);
btnCancel.setText("Cancel");

Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
label.setBounds(0, 288, 434, 13);

Label lblFrom = new Label(shell, SWT.NONE);
lblFrom.setBounds(50, 203, 55, 15);
lblFrom.setText("From");

//DateTime ddFrom = new DateTime(shell, SWT.DROP_DOWN);
ddFrom.setBounds(50, 224, 119, 23);

Label label_1 = new Label(shell, SWT.NONE);
label_1.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL));
label_1.setBounds(199, 222, 22, 25);
label_1.setText(":");

//DateTime ddTo = new DateTime(shell, SWT.NONE);
ddTo.setBounds(233, 224, 119, 23);

Label lblTo = new Label(shell, SWT.NONE);
lblTo.setText("To");
lblTo.setBounds(233, 203, 55, 15);

text1 = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
text1.setText("Progress\n");
text1.setEnabled(true);
text1.setBounds(0, 302, 434, 59);

Button btnCopyFolder = new Button(shell, SWT.NONE);
btnCopyFolder.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
File srcFolder = new File(txtSource.getText());
File destFolder = new File(txtDestination.getText());
if(!srcFolder.exists()){
MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
msgBox.setText("Information");
msgBox.setMessage("Directory does not exist.");
msgBox.open();
txtSource.setText("");
txtDestination.setText("");
}else{
if(txtDestination.getText().isEmpty())
{
MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
msgBox.setText("WARNING");
msgBox.setMessage("Invalid Path...");
msgBox.open();
txtSource.setText("");
txtDestination.setText("");
}
else
{
try {
copyFolder(srcFolder,destFolder);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
text1.append("Finished Copying.....\n");
MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
msgBox.setText("Information");
msgBox.setMessage("Done Copying...");
msgBox.open();
}
}
}
});
btnCopyFolder.setText("Copy Folder");
btnCopyFolder.setBounds(248, 253, 75, 25);
}
public void displayFiles(String[] files) {
for (int i = 0; files != null && i < files.length; i++) {
txtSource.setText(files[i]);
txtSource.setEditable(true);
}
}

public static void copyFolder(File src, File dest)
throws IOException{
if(src.isDirectory()){
if (!dest.exists())
{
dest.mkdir();
text1.append("Directory created : " + dest + "\n");
}
final String files[] = src.list();
for (String file : files)
{
File srcFile = new File(src, file);
File destFile = new File(dest, file);

//Recursive function call
copyFolder(srcFile, destFile);
}
}
else{
text1.append("");
Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
text1.append(text + " Copying " + src.getAbsolutePath() + "\n");
//
}
}
public static void copyFolder1(File src, File dest)
throws IOException{
// String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new DateTime(ddFrom, 0));
if(src.isDirectory()){
if (!dest.exists())
{
dest.mkdir();
text1.append("Directory created : " + dest + "\n");
}
String files[] = src.list();
for (String file : files)
{
File srcFile = new File(src, file);
File destFile = new File(dest, file);

//Recursive function call
copyFolder1(srcFile, destFile);
}
}
else{
if(dest.isDirectory())
{
copyFile(src, new File (dest, src.getName()));
text1.append(text + " Copying " + src.getAbsolutePath() + "\n");
}
else
{
copyFile(src, dest);
}
// }
}
}
public static void copyFile(File src, File dest) throws IOException
{
InputStream oInStream = new FileInputStream(src);
OutputStream oOutStream = new FileOutputStream(dest);

// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream );
while ((nLength = oBuffInputStream.read(oBytes)) > 0)
{
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
}
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
MainUI window = new MainUI();
window.open();
} catch (Exception e) {
e.printStackTrace();
}


}


}

这是错误

java.lang.NullPointerException
at org.eclipse.wb.swt.MainUI$1.widgetSelected(MainUI.java:87)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at org.eclipse.wb.swt.MainUI.open(MainUI.java:56)
at org.eclipse.wb.swt.MainUI.main(MainUI.java:370)

这是错误所在的行

btnCopyFolder.setVisible(false); //line 100

最佳答案

您正在使用此代码隐藏字段btnCopyFolder

Button btnCopyFolder = new Button(shell, SWT.NONE);

更改为

btnCopyFolder = new Button(shell, SWT.NONE);

关于java - 从另一个按钮禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com