Jframe中的Jpanel火灾事件

时间:2014-01-26 21:14:56

标签: java jframe jpanel jbutton

我有一个JFrame,其中包含一个带有三个butons的jpanel。 我希望当我在孩子中点击一个按钮时,一个事件会被触发到父母父母身上。 我希望添加Jcardlayout并单击Jpanel中的按钮,jcardlayout的值会发生变化。

public class Project{

private static JPanel pane = null, p = null;
private static CardLayout card = null;

public static void main(String[] args) {
    // TODO code application logic here
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createFrame();
        }
    });
}

private static void createFrame() {


    JFrame jf = new JFrame("Principal Frame");
    p = new JpMenu();
    jf.getContentPane().add(pane, BorderLayout.CENTER);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize(new Dimension(300, 200));
    jf.setVisible(true);
    jf.setResizable(false);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    jf.setExtendedState(JFrame.MAXIMIZED_BOTH);


}

}

Jpanel为netbeans生成

public class JpMenuPrincipal extends javax.swing.JPanel  {

public JpMenuPrincipal() {
    initComponents();
}

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

    btnOptionClient = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    btnOptionClient.setText("jButton1");
    btnOptionClient.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnOptionClientActionPerformed(evt);
        }
    });

    jButton2.setText("jButton2");

    jButton3.setText("jButton3");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(19, 19, 19)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton3)
                .addComponent(jButton2)
                .addComponent(btnOptionClient))
            .addContainerGap(32, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(btnOptionClient)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton3)
            .addContainerGap(245, Short.MAX_VALUE))
    );
}// </editor-fold>   
 private void btnOptionClientActionPerformed(java.awt.event.ActionEvent evt) {                                                

}                                               

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




private javax.swing.JButton btnOptionClient;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
}

1 个答案:

答案 0 :(得分:3)

Project传递给JpMenuPrincipal的构造函数,并为CardLayout类中的Project获取一个getter。

private Project project;

public JpMenuPrincipal(final Project project) {
    initComponents();
    this.project = project;
}

private void btnOptionClientActionPerformed(java.awt.event.ActionEvent evt) {                                                
    CardLayout card = project.getCardLayout();
    // do something with card
}

另请注意,您在框架中添加了jpMenu而非JpMenuPrincipal

同样,@ HovercraftFullOfEels指出,您无需在static中创建字段Project。只需对要访问的字段使用getter。