0

前回の質問で、私は Java の JFrame 機能について少し初心者であると述べました。指定されたファイルが保存されるディレクトリを取得する JFileChooser を作成しようとしていますsetup.exe。しかし、それは関係ありません。次のエラーが表示されます。

C:\Users\b\Downloads\files\thankyousanta>javac UpdateMechanism.java
UpdateMechanism.java:31: error: <identifier> expected
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                               ^
UpdateMechanism.java:31: error: <identifier> expected
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                                                             ^
UpdateMechanism.java:33: error: <identifier> expected
        openButton.addActionListener(openButton);
                                    ^
UpdateMechanism.java:33: error: <identifier> expected
        openButton.addActionListener(openButton);
                                               ^
4 errors

...このコードの場合:

import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner; 

import javax.swing.filechooser.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFileChooser;
import javax.swing.BoxLayout;
import javax.Swing.SwingUtilities;
import javax.swing.BorderFactory;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;

public class definitiveVoid extends JFileChooser implements ActionListener {
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    JButton openButton = new JButton("The update is waiting on you.");
    openButton.addActionListener(openButton);
}
public class UpdateMechanism extends JPanel
                                implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == openButton) {
            int returnVal = fc.showOpenDialog(UpdateMechanism.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();

            } else {
                // operation cancelled
            }
        } else {
            // destroy life
        }
    }
    private static String readURL(String targetURL) {
        String returnish = "";
        try {
            URL tempURL = new URL(targetURL); 
            Scanner s = new Scanner(tempURL.openStream()); 
            while (s.hasNextLine()) {
                returnish = returnish+s.nextLine(); 
            }
        } catch (IOException e) {
            System.out.println(e); 
        }
        return returnish;
    }

    private static String readFile(String targetFile) { 
        String returnString = "";
        try {
            File tempFile = new File(targetFile);
            Scanner s = new Scanner(tempFile);
            while (s.hasNextLine()) {
                returnString = returnString + s.nextLine(); 
            }
        } catch(IOException e) { 
            // !
            System.out.println(e);
        }
        return returnString;
    }

    private static void showGUI() {
        JFrame frame = new JFrame("The Neverhood Restoration Project");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(1024, 600));
        frame.setExtendedState(frame.MAXIMIZED_BOTH);
        frame.getContentPane().setBackground(new Color(0xA64343));

        File fileCheck = new File("C:/Program Files (x86)");
        String returnString = null;
        String rootDirectory = null;
        if (fileCheck.exists()) {
            rootDirectory = "C:/Program Files (x86)/DreamWorks Interactive"; 
            String checkFile = rootDirectory+"/Neverhood/version.txt"; 
            File tempFile = new File(checkFile);
            if (tempFile.exists()) {
                returnString = readFile(checkFile);
            } else {
                returnString = "It appears you do not have the Neverhood Restoration Project installed, or you are using an earlier version."; 
            }
        } else {
            rootDirectory = "C:/Program Files/DreamWorks Interactive";
            String checkFile = rootDirectory+"/Neverhood/version.txt"; 
            File tempFile = new File(checkFile);
            if (tempFile.exists()) {
                returnString = readFile(checkFile);
            } else {
                returnString = "It appears you do not have the Neverhood Restoration Project installed, or you are using an earlier version.";
            }
        }
        if (returnString.equals(readURL("http://theneverhood.sourceforge.net/version.txt"))) {
            returnString = "You are updated to the recent version!"; 
        } else { 
            returnString = "It appears you're not updated.";
        }

        JLabel headerLabel = new JLabel("The Neverhood Restoration Project");
        headerLabel.setHorizontalAlignment(JLabel.CENTER);
        JPanel heapPanel = new JPanel();
        heapPanel.setLayout(new BoxLayout(heapPanel, BoxLayout.PAGE_AXIS));
        heapPanel.setPreferredSize(new Dimension(500, heapPanel.getPreferredSize().height));
        JTextArea heapLabel = new JTextArea(50, 50);        
        heapLabel.setLineWrap(true);
        heapLabel.setWrapStyleWord(true);
        heapLabel.setEditable(false);
        heapLabel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        heapLabel.setFont(new Font("Serif", Font.PLAIN, 14));
        heapLabel.append("Current version: "+readURL("http://theneverhood.sourceforge.net/prettyversion.txt")+".\nInstalled version: "+readFile(rootDirectory+"/Neverhood/prettyversion.txt")+".\n"+returnString+"\n" + 
            "You can read the full version of the document to the left at http://theneverhood.sourceforge.net."
            + "\nBelow is the download button. Just click, choose your directory to save setup.exe in and enjoy!");
        heapPanel.add(heapLabel);

        try {
            Font sFont = Font.createFont(Font.TRUETYPE_FONT, new File("DUGFB___.TTF"));
            sFont = sFont.deriveFont(Font.PLAIN, 48);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(sFont);
            headerLabel.setFont(sFont);
        } catch (FontFormatException | IOException e) {
            System.out.println(e);
        }

        BufferedImage icoImage = null;
        try {
            icoImage = ImageIO.read(
                frame.getClass().getResource("/nhood.bmp"));
        } catch (IOException e) {
            System.out.println(e);
        }
        frame.setIconImage(icoImage);

        JEditorPane updateLog = new JEditorPane();
        JScrollPane scrollPane = new JScrollPane(updateLog);
        updateLog.setEditable(false);

        try {
            updateLog.setPage("http://theneverhood.sourceforge.net/");
        } catch (IOException e) {
            updateLog.setContentType("text/html");
            updateLog.setText("<html>The application could not load the webpage.</html>");
        }

        frame.add(headerLabel, BorderLayout.NORTH);
        frame.add(scrollPane);
        frame.add(heapPanel, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                showGUI();
            }
        });
    }
}
4

1 に答える 1

2

Java では、パブリック クラスは、クラス名と同じ名前 (.java サフィックス付き) を持つ独自のファイルで定義する必要があります。

definitiveVoidファイルで名前が付けられた public クラスを定義しましたUpdateMechanism.java。そして、メソッドの外で、このクラスにコードを直接入れようとしました。それは有効な Java ではありません。

スイングは難しくて複雑です。Java の基本を理解する前に使用しようとしないでください。

さらに、クラスは慣例により、Java では大文字で始まります。

于 2013-03-24T17:48:15.683 に答える