我一直在研究Java的JFileChooser。我使用Oracles FileChooserDemo作为基本模板。我对Oracle演示做了一些小的修改。我的问题是,当我运行程序时,JFrame确实很小。即使我使用setSize或setPreferredSize,JFrame也保持不变。该演示使用 pack 方法。我以为这限制了框架的大小,但是通过删除该行,可以在运行程序时显示窗口,但仅在顶部具有最小化,最大化和关闭按钮。这是我完整的代码:
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.awt.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.*;
public class Swing_Main_Menu extends JFrame implements ActionListener
{
public static final int WIDTH=900;
public static final int HEIGHT=600;
public static void main(String[] args)
{
Swing_Main_Menu gui = new Swing_Main_Menu();
gui.setVisible(true);
}
public Swing_Main_Menu()
{
super();
setSize(WIDTH, HEIGHT);
setLayout(new GridLayout(2,2));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton readPDF = new JButton("Read PDF");
readPDF.setFont(new Font("Arial",Font.PLAIN, 30));
readPDF.setBackground(Color.LIGHT_GRAY);
readPDF.addActionListener(new PDFExtraction());
JButton view = new JButton("View");
view.setFont(new Font("Arial",Font.PLAIN, 30));
view.setBackground(Color.LIGHT_GRAY);
view.addActionListener(this);
JButton dashboard = new JButton("Dashboard");
dashboard.setFont(new Font("Arial",Font.PLAIN, 30));
dashboard.setBackground(Color.LIGHT_GRAY);
dashboard.addActionListener(this);
JButton upload = new JButton("Batch Upload");
upload.setFont(new Font("Arial",Font.PLAIN, 30));
upload.setBackground(Color.LIGHT_GRAY);
upload.addActionListener(this);
add(readPDF);
add(view);
add(dashboard);
add(upload);
}
private class PDFExtraction implements ActionListener
{
private void readFields(PDDocument doc) throws Exception
{
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDAcroForm form = catalog.getAcroForm();
List<PDField> fields = form.getFields();
ArrayList <String> allFields= new ArrayList<>(1);
for(PDField field: fields)
{
String name = field.getFullyQualifiedName();
if (field instanceof PDTextField || field instanceof PDComboBox)
{
String value = field.getValueAsString();
allFields.add(value);
}
else if (field instanceof PDPushButton)
;
else
{
if (field instanceof PDRadioButton)
{
PDRadioButton radioButton = (PDRadioButton)form.getField(name);
String value=radioButton.getValue();
allFields.add(value);
}
else if (field instanceof PDCheckBox)
{
PDButton box = (PDButton)field;
String value = box.getValue();
allFields.add(value);
}
}
}
int count = 1;
for (String element : allFields)
{
System.out.println(count + ": " + element );
count++;
}
}
private void readFile() throws Exception {
FileChooserForRead fc = new FileChooserForRead();
fc.chooseFile();
}
public void actionPerformed (ActionEvent e)
{
try
{
readFile();
}
catch (Exception except)
{
System.exit(0);
}
}
}
public void actionPerformed (ActionEvent e)
{
String buttonString = e.getActionCommand();
if (buttonString.equals("View"))
{
//JFrame newMenu = new JFrame();
//newMenu.setSize(WIDTH, HEIGHT);
//newMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FileChooser pickFile = new FileChooser();
pickFile.chooseFile();
}
else if (buttonString.equals("Dashboard"))
{
System.exit(0);
}
else if(buttonString.equals("Batch Upload"))
{
System.exit(0);
}
else
{
System.exit(0);
}
}
public class FileChooser extends JPanel
implements ActionListener
{
static private final String newline = "\n";
JButton openButton;
JTextArea log;
JFileChooser fc;
public FileChooser() {
super(new BorderLayout());
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
fc = new JFileChooser();
//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
openButton = new JButton("Open a File...");
//openButton.setPreferredSize(new Dimension(100, 50));
//openButton.setFont(new Font("Arial",Font.PLAIN, 30));
openButton.addActionListener(this);
JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(openButton);
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooser.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
log.append("Opening: " + file.getName() + "." + newline);
try
{
Desktop.getDesktop().open(file);
}
catch (IOException ex)
{
System.exit(0);
}
}
else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FileChooser");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Add content to the window.
frame.add(new FileChooser());
//Display the window.
//frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.setSize(WIDTH,HEIGHT);
frame.pack();
frame.setVisible(true);
}
public void chooseFile () {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
public class FileChooserForRead extends JPanel implements ActionListener
{
static private final String newline = "\n";
JButton openButton;
JTextArea log;
JFileChooser fc;
public FileChooserForRead() {
super(new BorderLayout());
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
fc = new JFileChooser();
//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
openButton = new JButton("Open a File...");
//openButton.setPreferredSize(new Dimension(100, 50));
//openButton.setFont(new Font("Arial",Font.PLAIN, 30));
openButton.addActionListener(this);
JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(openButton);
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserForRead.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
log.append("Opening: " + file.getName() + "." + newline);
try
{
PDDocument doc = PDDocument.load(file);
PDFExtraction reader = new PDFExtraction();
reader.readFields(doc);
}
catch (Exception ex)
{
System.exit(0);
}
}
else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FileChooser");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Add content to the window.
frame.add(new FileChooserForRead());
//Display the window.
//frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.pack();
frame.setVisible(true);
}
public void chooseFile ()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
}
预先感谢您对我的帮助。 这是我基于以下内容创建的JFileChooser的Oracle示例:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
public class FileChooserDemo extends JPanel
implements ActionListener {
static private final String newline = "\n";
JButton openButton, saveButton;
JTextArea log;
JFileChooser fc;
public FileChooserDemo() {
super(new BorderLayout());
//Create the log first, because the action listeners
//need to refer to it.
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
//Create a file chooser
fc = new JFileChooser();
//Uncomment one of the following lines to try a different
//file selection mode. The first allows just directories
//to be selected (and, at least in the Java look and feel,
//shown). The second allows both files and directories
//to be selected. If you leave these lines commented out,
//then the default mode (FILES_ONLY) will be used.
//
//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//Create the open button. We use the image from the JLF
//Graphics Repository (but we extracted it from the jar).
openButton = new JButton("Open a File...",
createImageIcon("images/Open16.gif"));
openButton.addActionListener(this);
//Create the save button. We use the image from the JLF
//Graphics Repository (but we extracted it from the jar).
saveButton = new JButton("Save a File...",
createImageIcon("images/Save16.gif"));
saveButton.addActionListener(this);
//For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(openButton);
buttonPanel.add(saveButton);
//Add the buttons and the log to this panel.
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
//Handle save button action.
} else if (e.getSource() == saveButton) {
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would save the file.
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = FileChooserDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new FileChooserDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
答案 0 :(得分:0)
这很简单。要么使用setSize
方法,要么使用pack()
,但不要同时使用。
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FileChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new FileChooser());
//Display the window.
//frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.setSize(1024, 768);
//frame.pack();
frame.setVisible(true);
}