使用类实例创建无限循环

时间:2016-01-05 11:58:09

标签: java class instance infinite-loop

我正在尝试用Java编写一个简单的Photoviewer。

我想将控制方法和设计分为两类('Viewer'和'ViewerControls)。 要访问彼此的方法或变量,我必须创建每个的实例(我认为),这当然会引导我进入创建实例的无限循环,最后是一个stackoverflow异常。

所以我知道问题是什么,但不知道如何修复它。有人可以帮帮我吗?

观众代码:

public class Viewer extends javax.swing.JFrame {

/**
 * Creates new form viewer
 */


public Viewer() {
    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() {

    choosePnl = new javax.swing.JPanel();
    chooseWindow = new javax.swing.JFileChooser();
    buttonPnl = new javax.swing.JPanel();
    prevBtn = new javax.swing.JButton();
    playBtn = new javax.swing.JButton();
    nextBtn = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    choose = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList<>();
    lm = new DefaultListModel();
    jList1.setModel(lm);

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    choosePnl.setVisible(false);

    javax.swing.GroupLayout choosePnlLayout = new javax.swing.GroupLayout(choosePnl);
    choosePnl.setLayout(choosePnlLayout);
    choosePnlLayout.setHorizontalGroup(
        choosePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(chooseWindow, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
    );
    choosePnlLayout.setVerticalGroup(
        choosePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(choosePnlLayout.createSequentialGroup()
            .addComponent(chooseWindow, javax.swing.GroupLayout.PREFERRED_SIZE, 462, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 18, Short.MAX_VALUE))
    );

    getContentPane().add(choosePnl, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 660, 480));

    buttonPnl.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    prevBtn.setText("Prev");
    prevBtn.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    prevBtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            prevBtnActionPerformed(evt);
        }
    });

    playBtn.setText("Play");
    playBtn.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

    nextBtn.setText("Next");
    nextBtn.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    nextBtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nextBtnActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout buttonPnlLayout = new javax.swing.GroupLayout(buttonPnl);
    buttonPnl.setLayout(buttonPnlLayout);
    buttonPnlLayout.setHorizontalGroup(
        buttonPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(buttonPnlLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(prevBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(84, 84, 84)
            .addComponent(playBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(75, 75, 75)
            .addComponent(nextBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(23, Short.MAX_VALUE))
    );
    buttonPnlLayout.setVerticalGroup(
        buttonPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPnlLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(buttonPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(nextBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                .addComponent(playBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(prevBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );

    getContentPane().add(buttonPnl, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 480, 650, 80));

    jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    choose.setText("Choose Folder");
    choose.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            chooseActionPerformed(evt);
        }
    });

    jScrollPane1.setFocusTraversalPolicyProvider(true);

    jList1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    jScrollPane1.setViewportView(jList1);

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
            .addContainerGap(22, Short.MAX_VALUE)
            .addComponent(choose, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(24, 24, 24))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(choose, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 476, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(660, 0, 230, 570));

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


private void chooseActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (evt.getSource() == choose) {
        choosePnl.setVisible(true);

        chooseWindow.setMultiSelectionEnabled(true);
        chooseWindow.showOpenDialog(null);
        File[] selectedFiles = chooseWindow.getSelectedFiles();
        for (File f : selectedFiles) {
            lm.addElement(f.getPath());
        }
        listIndex = lm.getSize() - 1;
    }
}                                      

private void prevBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    controls.previous();
}                                       

private void nextBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    controls.next();
}                                       


public int getListIndex(){
    return listIndex;
}

public void setListIndex(int index){
    this.listIndex = index;
}

public static void main(String args[]) {
    /* Set the Nimbus look and feel */

/* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Viewer().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JPanel buttonPnl;
private javax.swing.JButton choose;
private javax.swing.JPanel choosePnl;
private javax.swing.JFileChooser chooseWindow;
private javax.swing.JList<String> jList1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton nextBtn;
private javax.swing.JButton playBtn;
private javax.swing.JButton prevBtn;
// End of variables declaration                   
private DefaultListModel lm;
private int listIndex;
ViewerControls controls = new ViewerControls();
}

ViewerControls代码:

public class ViewerControls {

Viewer viewer = new Viewer();

public void next(){
    viewer.setListIndex(viewer.getListIndex()+ 1);
}
public void previous(){
    viewer.setListIndex(viewer.getListIndex()- 1);
}
}

请记住,这是正在进行的工作,我是初学者,所以忽略其他错误。

先谢谢。

0 个答案:

没有答案