gpt4 book ai didi

java - jframe 父级中的 Jpanel 火灾事件

转载 作者:行者123 更新时间:2023-12-01 23:15:00 25 4
gpt4 key购买 nike

我有一个 JFrame,其中包含一个带有三个按钮的 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);


}

}

以及为netbeans生成的Jpanel

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;
}

最佳答案

Project 传递给 JpMenuPrincipal 的构造函数,并在 Project 类中为 CardLayout 提供 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 指出的,您不需要将 Project 中的字段设置为 static。只需对您想要访问的字段使用 getter 即可。

关于java - jframe 父级中的 Jpanel 火灾事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com