开发者

using file created in one private actionperformed event in another

I am a beginner in java and I want to create a very simple audio steganography for my project. First I started with a frame containing three buttons, which take a text file and .wav file from the user, and one button to begin encoding. I used filechoosers with the first two buttons to input files. I want the actionPerformed event of my "begin" button to convert the content of these two files to bytes. After that I plan to use an LSB algorithm to hide my text file in the .wav file. My query is how can I use the files "tfile" and "afile" in private void beginActionPerformed method?

import java.io.File;
import java.io.FileInputStream;
import javax.swing.JFileChooser;

public class second extends javax.swing.JFrame {

    /** Creates new form second */
    public second() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        text = new javax.swing.JButton();
        audio = new javax.swing.JButton();
        tname = new javax.swing.JLabel();
        aname = new javax.swing.JLabel();
        begin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        text.setText("TEXT FILE");
        text.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textActionPerformed(evt);
            }
        });

        audio.setText("AUDIO FILE");
        audio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                audioActionPerformed(evt);
            }
        });

        begin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        begin.setText("BEGIN");
        begin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                beginActionPerformed(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()
                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(text, javax.swing.GroupLayout.Alignment.LEADING,     javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(audio, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(aname, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(270, 270, 270)
                        .addComponent(begin, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(101, 101, 101)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(text)
                    .addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(audio)
                    .addComponent(aname, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(begin, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void textActionPerformed(java.awt.event.ActionEvent evt) {                                     
        JFileChooser opentext=new JFileChooser();
        opentext.showOpenDialog(this);
        int returnVal = opentext.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File tfile = opentext.getSelectedFile();
        String tfname=tfile.getName();
        tname.setText(tfname);
        // ... code that loads the contents of the file in the text area
    } 

    }                                    

    private void audioActionPerformed(java.awt.event.ActionEvent evt) {                                      
        JFileChooser openaudio=new JFileChooser();
        openaudio.showOpenDialog(this);
        int returnVal = openaudio.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File afile = openaudio.getSelectedFile();
        aname.setText(afile.getName());

    } 
    }                                  

    private void beginActionPerformed(java.awt.event.ActionEvent evt) {                                      
        FileInputStream fin = new FileInputStream(tfile);**//error here**
    }                                     

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new second().setVisible(true);
                FileInputStream fin = new FileInputStream(tfile);
            }
        });
    }

    private javax.swing.JLabel aname;
    private javax.swing.JButton audio;
    private javax.swing.JButton begin;
    private javax.swing.JButton text;
    private javax.swing.JLabel tname;
    // End of variables declaration                   
}

Also, is it fine if I use 开发者_如何学运维only FileInputStream for converting my .txt and .wav file into bytes here, or do I need to use anything else?


An easy way is to make them instance variables of the surrounding class (you should write your class names in camel-case to follow Java conventions, it helps others looking at your code):

public class Second extends javax.swing.JFrame {
    private File afile;
    private File tfile;

    ...

    private void textActionPerformed(java.awt.event.ActionEvent evt)  {                                     
        ...
        tfile = opentext.getSelectedFile();
        //set instance variable instead of local variable        
        ...
    } 



    private void audioActionPerformed(java.awt.event.ActionEvent evt) {                                      
        ...
        afile = openaudio.getSelectedFile(); 
        //set instance variable instead of local variable
        ...
    }                                  

    private void beginActionPerformed(java.awt.event.ActionEvent evt) {                                      
        FileInputStream fin = new FileInputStream(tfile); //works now
    }
}

This will not work in the main method though, since it's in a static context. So you could add a getter method for the Files in your Second class

    public File getTFile() { return tfile; }
    public File getAFile() { return afile; }

to be able to access the files from outside the class.

Please note that both afile and tfile will only be set after you clicked the buttons and chose a file for them, otherwise these references will still be null. So what you want to do in your run method can't work, because you try to use tfile immediately afer showing the frame, there's no way you can have clicked and set the file reference that fast :)


The easy answer is : Put afile and tfile as class member :

private javax.swing.JLabel aname;
private javax.swing.JButton audio;
private javax.swing.JButton begin;
private javax.swing.JButton text;
private javax.swing.JLabel tname;

private File afile;     // put at bottom of class
private File tfile;   

The better solution is to have another class to separate your date manipulation (model) from your view (JFrame)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜