gpt4 book ai didi

java - 在 Java 中创建 GUI 时出现编译错误

转载 作者:行者123 更新时间:2023-12-02 07:06:31 25 4
gpt4 key购买 nike

我的程序打开一个文件,允许您查看或编辑它,然后再次保存它。到目前为止,我只有 GUI 布局。

因此,当我尝试编译程序时,出现以下错误:

    FileChooser.java:26: error: <identifier> expected
fileChooser = newJFileChooser();

我收到了 ^^^ 错误四次,对于我初始化的每个组件一次。

    FileChooser.java:39: error: invalid method declaration; return type required
public FileChooserGUI() {

当我尝试创建构造函数时,出现此 ^^^ 错误。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

public class FileChooser extends JFrame { //JFrame is a container class in Swing

//declare all components and other variables
private JTextArea textArea; //b
private JButton openFileButton; //c
private JButton saveFileButton;
private JButton clearButton;

//Declare and initialize a JFileChooser object using JFileChooser's
//default constructor.
private JFileChooser fileChooser;
fileChooser = newJFileChooser();

//Declare and initialize a FileNameExtensionFilter object for .txt files.
private FileFilter fileFilter;
fileFilter = new FileNameExtensionFilter("text file", "txt");

//Set the JFileChooser object's current file filter by calling it's
//setFileFilter method and passing it the filter you created in e.
fileChooser.setFileFilter(fileFilter);


//constructor - initialize all components, add them to the container,
//create listener objects, register them to listen for events
public JFileChooserLabGUI() {

//call superclass constructor
super("File Chooser");


//initialize all components
textArea = new JTextArea();
openFileButton = new JButton("Open");
saveFileButton = new JButton("Save");
clearButton = new JButton("Clear");

//create and register the listener object with
//sources of events (the JButtons in this example)
listener = new JFileChooserListener();
openFileButton.addActionListener(listener);
saveFileButton.addActionListener(listener);
clearButton.addActionListener(listener);


//arrange components in the window
//Create a layout manager object and set this window's
//layout manager to it
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(1,2));
textPanel.add(textArea);

buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,3,15,0));
buttonPanel.add(openFileButton);
buttonPanel.add(saveFileButton);
buttonPanel.add(clearButton);
this.setLayout(new GridLayout(3,1));



//Add components to the container
this.add(textPanel);
this.add(buttonPanel);


}//end of constructor
}//end of class

我还没有完成监听器类,但我认为这与仅显示 GUI 以确保布局正确没有任何关系?

我注意到,当我在同一行中声明和初始化时,我没有收到这四个错误。我初始化有问题吗?

最佳答案

我认为有几件事需要纠正

  1. 构造函数名称与类名称不同。

  2. 考虑到类方法之外的变量赋值:

    • 您必须同时声明并初始化变量。
    • 或者(更好的方法)在构造函数中声明并初始化。结账Java Variable declaration
  3. 此外,还有一个小语法错误。 new 和构造函数名称之间应该有空格。

    public FileChooser () {
    //call superclass constructor
    super("File Chooser");
    fileChooser=fileChooser = new JFileChooser();
    fileFilter=new FileNameExtensionFilter("text file", "txt");
    fileChooser.setFileFilter(fileFilter);

    ...
    }

希望这有帮助!

关于java - 在 Java 中创建 GUI 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16040782/

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