使用按钮将多个球(红色和蓝色)添加到面板内的面板

时间:2015-04-12 22:50:05

标签: java swing

有人可以帮助我完成这个小项目吗?

我创建了一个内部有大型JPanel的JFrame窗口。 在这个JPanel中,我已经包含了六个按钮和一个较小的JPanel," drawPanel"。我打算这样做,当我点击一个按钮时,一个红色或蓝色的弹跳球将出现在较小的JPanel中。如果我点击另一个,它会移除一个弹跳球。我知道这将需要Graphics和ArrayLists,但我不确定如何继续。

以下是我所说的:http://i.stack.imgur.com/4mSff.png

这是我的源代码:

package ball;
public class ballForm extends javax.swing.JFrame { 

public ballForm() {
    initComponents();
}

private void initComponents() {

    AddRedButton = new javax.swing.JButton();
    RemoveRedButton = new javax.swing.JButton();
    AddBlueButton = new javax.swing.JButton();
    RemoveBlueButton = new javax.swing.JButton();
    drawPanel = new javax.swing.JPanel();
    RedIncreaseButton = new javax.swing.JButton();
    BlueIncreaseButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    AddRedButton.setText(" Add Red ball");
    AddRedButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            AddRedButtonActionPerformed(evt);
        }
    });

    RemoveRedButton.setText("Remove Red ball");
    RemoveRedButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            RemoveRedButtonActionPerformed(evt);
        }
    });

    AddBlueButton.setText("Add Blue ball");

    RemoveBlueButton.setText("Remove Blue ball");

    drawPanel.setBackground(new java.awt.Color(255, 255, 255));

    javax.swing.GroupLayout drawPanelLayout = new javax.swing.GroupLayout(drawPanel);
    drawPanel.setLayout(drawPanelLayout);
    drawPanelLayout.setHorizontalGroup(
        drawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 262, Short.MAX_VALUE)
    );
    drawPanelLayout.setVerticalGroup(
        drawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 285, Short.MAX_VALUE)
    );

    RedIncreaseButton.setText("Increase speed of Red balls");
    RedIncreaseButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            RedIncreaseButtonActionPerformed(evt);
        }
    });

    BlueIncreaseButton.setText("Increase speed of Red balls");
    BlueIncreaseButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BlueIncreaseButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(36, 36, 36)
            .addComponent(drawPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(AddRedButton)
                    .addGap(18, 18, 18)
                    .addComponent(RemoveRedButton)
                    .addGap(18, 18, 18)
                    .addComponent(RedIncreaseButton)
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(AddBlueButton)
                    .addGap(18, 18, 18)
                    .addComponent(RemoveBlueButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(BlueIncreaseButton)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(91, 91, 91)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(AddRedButton)
                        .addComponent(RemoveRedButton)
                        .addComponent(RedIncreaseButton))
                    .addGap(59, 59, 59)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(AddBlueButton)
                        .addComponent(RemoveBlueButton)
                        .addComponent(BlueIncreaseButton)))
                .addComponent(drawPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(139, Short.MAX_VALUE))
    );

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

private void AddRedButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
}                                            

private void RedIncreaseButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    // TODO add your handling code here:
}                                                 

private void BlueIncreaseButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    // TODO add your handling code here:
}                                                  

private void RemoveRedButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    // TODO add your handling code here:
}                                               

/**
 * @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(ballForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ballForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ballForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ballForm.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 ballForm().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton AddBlueButton;
private javax.swing.JButton AddRedButton;
private javax.swing.JButton BlueIncreaseButton;
private javax.swing.JButton RedIncreaseButton;
private javax.swing.JButton RemoveBlueButton;
private javax.swing.JButton RemoveRedButton;
private javax.swing.JPanel drawPanel;
// End of variables declaration                   

}

非常感谢任何帮助...

0 个答案:

没有答案