当用户没有选择两个 PDF 时,我想禁用我的合并按钮。
我该怎么做呢?我试图做一个 while 循环来检查 file1
和 file2
是否为 null 但循环没有终止。
package pdfMerge;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFMergerUtility;
import java.awt.event.*;
public class pdfMerger extends JFrame {
private static final String ActionEvent = null;
private JButton choose1, choose2, mergeButton;
private JFileChooser fileChooser1, fileChooser2;
private JPanel contentsPane;
private int returnValue1, returnValue2;
private File file1, file2;
private String fileName1, fileName2;
private boolean valid;
public pdfMerger(){
super("PDF Merger");
setLayout(new BorderLayout());
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponents();
addAction();
setVisible(true);
}
public void addComponents(){
contentsPane = new JPanel(new GridLayout(0, 3));
add(contentsPane, BorderLayout.SOUTH);
choose1 = new JButton("Choose 1st pdf");
choose2 = new JButton("Choose 2nd pdf");
mergeButton = new JButton("Merge");
fileChooser1 = new JFileChooser();
fileChooser2 = new JFileChooser();
contentsPane.add(choose1);
contentsPane.add(choose2);
contentsPane.add(mergeButton);
}
public void addAction(){
choose1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
if (event.getSource() == choose1){
returnValue1 = fileChooser1.showOpenDialog(null);
if (returnValue1 == JFileChooser.APPROVE_OPTION){
file1 = fileChooser1.getSelectedFile();
fileName1 = file1.toString();
fileName1 = fileName1.replace("\\", "\\\\");
System.out.println(fileName1);
}
}
}
}
);
choose2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
if (event.getSource() == choose2){
returnValue2 = fileChooser2.showOpenDialog(null);
if (returnValue2 == JFileChooser.APPROVE_OPTION){
file2 = fileChooser2.getSelectedFile();
fileName2 = file2.toString();
fileName2 = fileName2.replace("\\", "\\\\");
System.out.println(fileName2);
}
}
}
}
);
mergeButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
PDFMergerUtility ut = new PDFMergerUtility();
ut.addSource(fileName1);
ut.addSource(fileName2);
ut.setDestinationFileName("C:\\Users\\Shaheedul\\Desktop\\MergedPDF.pdf");
try {
ut.mergeDocuments();
} catch (Exception error){
System.out.println("Something went wrong!");
}
}
}
);
}
}
最佳答案
简单:首先将 mergeButton 或其 Action 设置为禁用:
mergeButton.setEnabled(false);
然后在您的文件选择按钮的 Action 监听器中,如果已选择 2 个文件,则将 mergeButton 设置为启用。
例如注意用 //!!
,
// !! class names should begin with upper case letter
public class PdfMerger extends JFrame {
// ...
private JButton choose1, choose2, mergeButton;
// ...
public PdfMerger() {
super("PDF Merger");
setLayout(new BorderLayout());
setSize(500, 500); // advise against this
setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponents();
addAction();
setVisible(true);
// !!
mergeButton.setEnabled(false);
}
// ...
public void addAction() {
choose1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == choose1) {
returnValue1 = fileChooser1.showOpenDialog(null);
if (returnValue1 == JFileChooser.APPROVE_OPTION) {
file1 = fileChooser1.getSelectedFile();
fileName1 = file1.toString();
fileName1 = fileName1.replace("\\", "\\\\");
System.out.println(fileName1);
// !! added
mergeButton.setEnabled(bothFilesChosen());
}
}
}
});
choose2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == choose2) {
returnValue2 = fileChooser2.showOpenDialog(null);
if (returnValue2 == JFileChooser.APPROVE_OPTION) {
file2 = fileChooser2.getSelectedFile();
fileName2 = file2.toString();
fileName2 = fileName2.replace("\\", "\\\\");
System.out.println(fileName2);
// !! added
mergeButton.setEnabled(bothFilesChosen());
}
}
}
});
// ....
}
// !!
private boolean bothFilesChosen() {
return (file1 != null && file1.exists() && file2 != null && file2.exists());
}
}
关于用户未选择文件时 JAVA 禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253040/