Java 无法使用setSize或setPreferredSize更改JFrame的大小
我一直在研究Java的JFileChooser。我使用Oracles FileChooserDemo作为基本模板。我对Oracles演示做了一些轻微的修改。我的问题是,当我运行程序时,JFrame非常小。即使使用setSize或setPreferredSize,JFrame也保持不变。演示使用pack方法。我以为这会限制帧的大小,但通过删除该行,当我运行程序时,窗口会出现,但顶部只有最小化、最大化和关闭按钮。这是我的完整代码:Java 无法使用setSize或setPreferredSize更改JFrame的大小,java,swing,size,jfilechooser,Java,Swing,Size,Jfilechooser,我一直在研究Java的JFileChooser。我使用Oracles FileChooserDemo作为基本模板。我对Oracles演示做了一些轻微的修改。我的问题是,当我运行程序时,JFrame非常小。即使使用setSize或setPreferredSize,JFrame也保持不变。演示使用pack方法。我以为这会限制帧的大小,但通过删除该行,当我运行程序时,窗口会出现,但顶部只有最小化、最大化和关闭按钮。这是我的完整代码: import javax.swing.*; import java.
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();
}
});
}
}
}
这很简单。您可以使用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);
}
即使添加了导入,发布的代码也不会编译。为了更快地获得更好的帮助,请发布一个or。您能解释为什么不使用pack&setSize或提供一个参考吗?@PaulEdison,因为它们做的是相同的事情:更改帧大小。pack根据LayoutManager确定的内容窗格大小设置窗口大小。setSize设置窗口的固定大小。@SergiyMedvynskyy我知道我不应该同时使用这两个按钮,但如果我取出包装,窗口将消失,除了窗口的关闭、最小化和最大化按钮。当我取出包装并设置尺寸宽度、高度时,窗口仍然不存在,除了三个窗口按钮。@Sergiymedvynsky我已经添加了我的所有代码以及Oracle示例,该示例没有无法更改JFrame窗口大小的问题。@PaulEdison我仍然无法理解您的问题。我已经运行了您发布的代码,至少在我的方法createAndShowGUI版本中,这些代码对我来说都很好。
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);
}