- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个程序可以比较然后合并两个文件并创建一个新文件。我现在正在尝试使用 Swing 编写一个可以与该程序交互的 GUI。到目前为止,一切似乎都已就位,但是当我尝试运行该程序时,我收到大量 Swing 和 awt 错误。下面是我尝试运行的程序。
/*
* 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 HelloCKL;
import java.util.ArrayList;
import java.io.*;
public class HelloCKL {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String sourceFile1Path = helloSwingCKL.fileSource;
String sourceFile2Path = helloSwingCKL.fileToAppend;
String mergedFilePath = "merged_test2.ckl";
File[] files = new File[2];
files[0] = new File(sourceFile1Path);
files[1] = new File(sourceFile2Path);
File mergedFile = new File(mergedFilePath);
mergeFiles(files, mergedFile);
}
public static void mergeFiles(File[] files, File mergedFile) {
// NEW
ArrayList<String> list = new ArrayList<String>();
FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(mergedFile, false);
out = new BufferedWriter(fstream);
} catch (IOException e1) {
e1.printStackTrace();
}
// Going in a different direction. We are using a couple booleans to tell us when we want to copy or not. So at the beginning since we start
// with our source file we set copy to true, we want to copy everything and insert vuln names into our list as we go. After that first file
// we set the boolean to false so that we dont start copying anything from the second file until it is a vuln. We set to true when we see vuln
// and set it to false if we already have that in our list.
// We have a tmpCopy to store away the value of copy when we see a vuln, and reset it to that value when we see an </VULN>
Boolean copy = true;
Boolean tmpCopy = true;
for (File f : files) {
System.out.println("merging: " + f.getName());
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
while ((aLine = in.readLine()) != null) {
// Skip the close checklist and we can write it in at the end
if (aLine.trim().equals("</CHECKLIST>")){
continue;
}
if (aLine.trim().equals("<VULN>")){
// Store our current value of copy
tmpCopy = copy;
copy = true;
String aLine2 = in.readLine();
String aLine3 = in.readLine();
String nameLine = in.readLine();
if(list.contains(nameLine.trim())){
System.out.println("Skipping: " + nameLine);
copy = false;
// while (!(aLine.trim().equals("</VULN>"))){
// aLine = in.readLine();
// }
// continue; // this would skip the writing out to file part
}
else{
list.add(nameLine.trim());
System.out.println("::: List is now :::" );
System.out.println(list.toString());
}
if(copy){
out.write(aLine);
out.newLine();
out.write(aLine2);
out.newLine();
out.write(aLine3);
out.newLine();
out.write(nameLine);
out.newLine();
}
}
else{
if(copy){
out.write(aLine);
out.newLine();
}
}
// after we have written to file, if the line was a close vuln, switch copy back to original value
if (aLine.trim().equals("</VULN>")){
copy = tmpCopy;
}
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
copy = false;
}
// Now lets add the close checklist tag we omitted before
try{
out.write("</CHECKLIST>");
} catch (IOException e){
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我正在使用的 Swing 编码。
/*
* 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 hellockl;
import static HelloCKL.HelloCKL.mergeFiles;
import java.io.File;
import javax.swing.JFileChooser;
import java.util.ArrayList;
import java.io.*;
public class helloSwingCKL extends javax.swing.JFrame {
/**
* Creates new form helloSwingCKL
*/
public helloSwingCKL() {
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() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(java.awt.Color.lightGray);
jButton1.setText("Source");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("New .ckl");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jTextField3.setText("Save merged file as...");
jButton3.setText("Merge");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField3)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(5, 5, 5)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(92, 92, 92)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(36, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(104, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File source = chooser.getSelectedFile();
String fileSource = source.getAbsolutePath();
jTextField1.setText(fileSource);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File toAppend = chooser.getSelectedFile();
String fileToAppend = toAppend.getAbsolutePath();
jTextField2.setText(fileToAppend);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
HelloCKL.mergeFiles();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(helloSwingCKL.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(helloSwingCKL.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(helloSwingCKL.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(helloSwingCKL.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new helloSwingCKL().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}
以下是“大量错误”...
*run:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: hellockl.HelloCKL.main
at hellockl.helloSwingCKL.jButton3ActionPerformed(helloSwingCKL.java:145)
at hellockl.helloSwingCKL.access$300(helloSwingCKL.java:18)
at hellockl.helloSwingCKL$4.actionPerformed(helloSwingCKL.java:73)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 15 seconds)*
下面是编译器错误。
Compiling 1 source file to C:\Users\blakey\Documents\NetBeansProjects\helloCKL\build\classes
C:\Users\blakey\Documents\NetBeansProjects\helloCKL\src\hellockl\helloSwingCKL.java:3: error: package HelloCKL does not exist
import static HelloCKL.HelloCKL.mergeFiles;
C:\Users\blakey\Documents\NetBeansProjects\helloCKL\src\hellockl\helloSwingCKL.java:3: error: static import only from classes and interfaces
import static HelloCKL.HelloCKL.mergeFiles;
C:\Users\blakey\Documents\NetBeansProjects\helloCKL\src\hellockl\helloSwingCKL.java:129: error: cannot access HelloCKL
HelloCKL.main();
bad source file: C:\Users\blakey\Documents\NetBeansProjects\helloCKL\src\hellockl\HelloCKL.java
file does not contain class hellockl.HelloCKL
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
3 errors
C:\Users\blakey\Documents\NetBeansProjects\helloCKL\nbproject\build-impl.xml:952: The following error occurred while executing this line:
C:\Users\blakey\Documents\NetBeansProjects\helloCKL\nbproject\build-impl.xml:269: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
最佳答案
when I attempt to run the program I get a ton of Swing and awt errors
您似乎正在尝试运行无法编译的代码永远不要这样做 - 在尝试运行程序之前首先修复所有编译错误,如果您无法弄清楚如何做到这一点并需要帮助,那么至少将编译错误与您的问题一起发布在此处,因为“大量错误”并不能告诉我们太多可以处理的信息。
快速查看代码的主要问题是您错误地调用了 mergeFiles(...)
方法。您在不传递任何参数的情况下调用它:
HelloCKL.mergeFiles();
并且它已被写入需要两个参数:
public static void mergeFiles(File[] files, File mergedFile) {
解决方案:就像在原始 main 方法中尝试调用该方法一样,通过传入要合并的文件数组以及要合并的最终结果文件来调用该方法。看起来您还有其他代码试图获取这些文件,但似乎没有对它获得的结果执行任何操作,而您应该使用它。
关于java - 使用 Swing GUI 在一个类中执行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317782/
我是 Haskell 世界的新手,我编译了一个简单的 exe.program main = do putStrLn "Hello, what's your name?" name
我正在使用 JUnit,现在我想在运行测试之前执行 Java 程序(主方法)。 即在我的项目中,我有一个包含一个具有 main 方法的类的包。我想在运行测试之前运行它(可能在一个单独的进程中),因为被
我的代码是: char* arg_list[] = { "gnuplot", "gnuplot_script.sh", NULL }; printf("Ready %s %s\n", arg_list
exec() 似乎在服务器上启用(function_exists('exec') 返回 true,并且命令未在 'disable_functions' 变量中列出)但我们得到 警告:出于安全原因,ex
我想从 Adobe AIR 应用程序中按下一个按钮并执行一些已安装的程序。例如,我会有一个名为“Start Winamp”的按钮。当按下它时,它应该直接启动 Winamp.exe...我不想执行一
我学习 CS 有一段时间了,似乎我(或我的许多 friend )从来不了解在制作、安装等方面幕后发生的事情。 纠正我,但是 make 是一种编译一组文件的方法吗? 在 Windows 上“将程序安装到
如何执行 MemoryStream 中的程序,这样我就不必先将它保存到硬盘上。该文件可能不会临时保存到硬盘。该程序必须在内存中 100% 正常运行。 static string s
我使用了一个程序(在 Windows 上),我不会透露其名称,它可以从命令行打开而无需通过任何身份验证。我正在尝试创建一些安全措施以防止其他人以这种方式访问它。 我计划用批处理文件替换这个程序的内
我正在开发一个 Web 应用程序,但我陷入了困境: 我想创建一个简单的标签来触发本地程序的执行如gedit、mozilla firefox等 我的项目基于 HTML、Javascript 和 PHP。
我有一个 C++ 程序需要 root 权限才能执行某些功能。如果我在 su 模式下运行编译后的代码,例如 linux-c5b6:/home/suman # 它满足了我的要求,但是在运行时我想检查我的进
我正在创建一个 shell 副本,但在执行自制程序时遇到了问题。我的意思是,当我想执行诸如 java、ls、wc 等之类的东西时,一切都很好......已经存在于路径变量。 现在我希望能够执行“myl
我试图让一些值显示在电子墨水显示器上(通过 SPI)。我已经编写了软件来初始化显示并显示作为命令行参数传递的值。问题是,由于电子墨水技术,显示需要几秒钟才能完全实现,所以此时显示程序也在运行。 另一个
所以我在 Oracle 11g 上创建了以下过程: create or replace PROCEDURE calc_fee (proc_borrowed_date IN Borrowing.borr
我想让 Maven 目标触发 java 类的执行。我正在尝试使用以下行迁移 Makefile: neotest: mvn exec:java -Dexec.mainClass="org.dha
我想用 os.system 运行命令,但出现错误 c:/fe ' is not recognized as an internal or external command, operable prog
我将Spark 1.5.2用于Spark Streaming应用程序。 Web UI的“执行者”选项卡中的存储内存是什么?如何达到530 MB?如何更改该值? 最佳答案 小心:您使用的非常,非常老旧且
我正在阅读 GitLab CI Multirunner documentation它介绍了如何设置 GitLab CI Multirunner 执行器,但我似乎无法在文档或在线搜索中找到有关更改初始设
我是 C 编程新手。我正在尝试使用 fork()、exec() 和 waitpid() 运行由用户指定的路径给出的程序命令。我已经尝试让它正确运行几个小时了,但我不断收到错误,我不知道如何排除故障,一
我是 Java 和 Selenium 的初学者,我在工作时遇到了 JavascriptExecutor。 想知道:虽然 Remote webdriver 和 Firefox driver 实现了 ja
我正在尝试通过 linux c++ 中的邮件命令发送电子邮件,但 execl 导致错误。 如何使用 exec 发送此命令? /bin/echo llol | /usr/bin/mail -s "tes
我是一名优秀的程序员,十分优秀!