Java E错误是什么?我使用的是netbeans,它说找不到symbol.getSelectedFiles(),你问的是“为什么我的代码不工作”,但看起来你没有显示足够的信息让其他人回答。为了获得更好的帮助,请发布一个小的可编译(或尽可能接近可编译)程序,为我们演
Java E错误是什么?我使用的是netbeans,它说找不到symbol.getSelectedFiles(),你问的是“为什么我的代码不工作”,但看起来你没有显示足够的信息让其他人回答。为了获得更好的帮助,请发布一个小的可编译(或尽可能接近可编译)程序,为我们演,java,swing,Java,Swing,E错误是什么?我使用的是netbeans,它说找不到symbol.getSelectedFiles(),你问的是“为什么我的代码不工作”,但看起来你没有显示足够的信息让其他人回答。为了获得更好的帮助,请发布一个小的可编译(或尽可能接近可编译)程序,为我们演示您的问题。否则你会强迫我们去猜,而我们大多数人都不擅长这个。我仍然完全迷失在这里。也许其他人可以看到问题,但如果不是,你真的想考虑创建和发布一个,换句话说,隔离问题,并发布演示它的最小程序。同时发布完整的错误消息,没有遗漏任何内容。我发布了您
E错误是什么?我使用的是netbeans,它说找不到symbol.getSelectedFiles(),你问的是“为什么我的代码不工作”,但看起来你没有显示足够的信息让其他人回答。为了获得更好的帮助,请发布一个小的可编译(或尽可能接近可编译)程序,为我们演示您的问题。否则你会强迫我们去猜,而我们大多数人都不擅长这个。我仍然完全迷失在这里。也许其他人可以看到问题,但如果不是,你真的想考虑创建和发布一个,换句话说,隔离问题,并发布演示它的最小程序。同时发布完整的错误消息,没有遗漏任何内容。我发布了您要求的内容(我想)。不是100%确定我在做什么。所以,如果我遗漏了什么,我向您道歉。@Loismustdie555:您正在调用CustomContentButton上的
getSelectedFiles()
,一个JButton(??).Woops。看来我是。但如果我将CustomContentButton.getSelectedFiles()重命名为CustomContentResults.getSelectedFiles()),它会抛出另一个error@Loismustdie555:您的整个程序设置看起来不对劲。请参见编辑以回答。@Loismustdie555:好的,但我不确定这与您当前的问题有什么关系。同样,我建议您将文件[]存储到一个名为selectedFiles
的字段中,然后在run方法中使用该数组。
try
{
//Basic game launching command.
StringBuilder sb = new StringBuilder("cmd.exe /C start C:\\mygame\\game.exe");
}
for(File f : CustomContent.getSelectedFiles()){
sb.append(" -file ").append(f.getName());
}
Process process = Runtime.getRuntime().exec(sb.toString());
}
private void CustomContentActionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser CustomContent = new JFileChooser();
CustomContent.setMultiSelectionEnabled(true);
int returnVal = CustomContent.showOpenDialog(CustomContent);
String file = CustomContent.getSelectedFile().toString();
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication5;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
/**
*
* @author Loismustdie555
*/
public class NewJPanel extends javax.swing.JPanel {
/**
* Creates new form NewJPanel
*/
public NewJPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
CustomContentButton = new javax.swing.JButton();
RunGameButton = new javax.swing.JButton();
CustomContentButton.setText("Files");
CustomContentButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CustomContentButtonActionPerformed(evt);
}
});
RunGameButton.setText("Run");
RunGameButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RunGameButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(CustomContentButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(RunGameButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(CustomContentButton)
.addComponent(RunGameButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
private void RunGameButtonActionPerformed(java.awt.event.ActionEvent evt) {
try
{
//Basic game launching command.
StringBuilder sb = new StringBuilder("cmd.exe /C start C:\\mygame\\game.exe");
for(File f : CustomContentButton.getSelectedFiles()){
sb.append(" -file ").append(f.getName());
}
//Launch the game
Process process = Runtime.getRuntime().exec(sb.toString());
}
catch(IOException e)
{
//Log Error
}
}
private void CustomContentButtonActionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser CustomContentResults = new JFileChooser();
CustomContentResults.setMultiSelectionEnabled(true);
int returnVal = CustomContentResults.showOpenDialog(CustomContentResults);
String file = CustomContentResults.getSelectedFile().toString();
}
// Variables declaration - do not modify
private javax.swing.JButton CustomContentButton;
private javax.swing.JButton RunGameButton;
// End of variables declaration
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.*;
@SuppressWarnings("serial")
public class NewJPanel2 extends JPanel {
private File[] selectedFiles = null;
// Actions that are used by my buttons
private RunAction runAction = new RunAction("Run");
private FileAction fileAction = new FileAction("Files");
public NewJPanel2() {
setLayout(new GridLayout(1, 0, 5, 5));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// create my buttons using the actions
add(new JButton(fileAction));
add(new JButton(runAction));
}
// my run action
private class RunAction extends AbstractAction {
public RunAction(String name) {
super(name); // give button its name text
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic); // alt-key mnemonic
setEnabled(false); // disable this action at start
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO use selectedFiles to launch new program
// just to show that the files are accessable
for (File file : selectedFiles) {
System.out.println(file);
}
}
}
private class FileAction extends AbstractAction {
public FileAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
final JFileChooser customContentResults = new JFileChooser();
customContentResults.setMultiSelectionEnabled(true);
int returnVal = customContentResults.showOpenDialog(customContentResults);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = customContentResults.getSelectedFiles();
if (selectedFiles != null && selectedFiles.length > 0) {
// only enable runAction if Files have been selected
runAction.setEnabled(true);
}
}
}
}
private static void createAndShowGui() {
NewJPanel2 mainPanel = new NewJPanel2();
JFrame frame = new JFrame("NewJPanel2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}