当前位置: 首页 > 知识库问答 >
问题:

带有jPanel的jFrame

白侯林
2023-03-14

你好,我实际上正在尝试从jPanel切换到我的JFrame中的另一个jPanel。我已经更改了我的代码,下面是我遵循以下内容的样子:

 package testjframe;
public class TestJframe {
public static void main(String[] args) {
    new MyFrame().setVisible(true);
}

}
 package testjframe;
import java.awt.CardLayout;
public class MyFrame extends javax.swing.JFrame {

public MyFrame() {
    initComponents();

}

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

    mainPanel = new javax.swing.JPanel();
    panel2 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    panel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    panel3 = new javax.swing.JPanel();
    jLabel3 = new javax.swing.JLabel();
    btn1 = new javax.swing.JButton();
    btn2 = new javax.swing.JButton();
    btn3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    mainPanel.setLayout(new java.awt.CardLayout());

    jLabel2.setText("jLabel2");

    javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(panel2);
    panel2.setLayout(panel2Layout);
    panel2Layout.setHorizontalGroup(
        panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 792, Short.MAX_VALUE)
        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(0, 0, Short.MAX_VALUE)))
    );
    panel2Layout.setVerticalGroup(
        panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 460, Short.MAX_VALUE)
        .addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(0, 0, Short.MAX_VALUE)))
    );

    mainPanel.add(panel2, "card3");

    jLabel1.setText("jLabel1");

    javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
    panel1.setLayout(panel1Layout);
    panel1Layout.setHorizontalGroup(
        panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 792, Short.MAX_VALUE)
        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE)))
    );
    panel1Layout.setVerticalGroup(
        panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 460, Short.MAX_VALUE)
        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE)))
    );

    mainPanel.add(panel1, "card2");

    jLabel3.setText("jLabel3");

    javax.swing.GroupLayout panel3Layout = new javax.swing.GroupLayout(panel3);
    panel3.setLayout(panel3Layout);
    panel3Layout.setHorizontalGroup(
        panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 792, Short.MAX_VALUE)
        .addGroup(panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel3Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(0, 0, Short.MAX_VALUE)))
    );
    panel3Layout.setVerticalGroup(
        panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 460, Short.MAX_VALUE)
        .addGroup(panel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel3Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(0, 0, Short.MAX_VALUE)))
    );

    mainPanel.add(panel3, "card4");

    btn1.setText("1");
    btn1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btn1ActionPerformed(evt);
        }
    });

    btn2.setText("2");
    btn2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btn2ActionPerformed(evt);
        }
    });

    btn3.setText("3");
    btn3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btn3ActionPerformed(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()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(btn1)
                    .addGap(261, 261, 261)
                    .addComponent(btn2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn3)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btn1)
                .addComponent(btn3)
                .addComponent(btn2))
            .addContainerGap())
    );

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

private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    CardLayout card = (CardLayout)mainPanel.getLayout();
    card.show(mainPanel, "panel1");
    System.out.println("1");
}                                    

private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    CardLayout card = (CardLayout) mainPanel.getLayout();
    card.show(mainPanel, "panel2");
    System.out.println("2");
}                                    

private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    CardLayout card = (CardLayout)mainPanel.getLayout();
    card.show(mainPanel, "panel3");
    System.out.println("3");
}                                    

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

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

// Variables declaration - do not modify                     
private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel mainPanel;
private javax.swing.JPanel panel1;
private javax.swing.JPanel panel2;
private javax.swing.JPanel panel3;
// End of variables declaration                   

共有1个答案

狄飞鹏
2023-03-14

目前,您正在重新绘制面板,然后重新验证。这意味着您重新绘制它,使其看起来相同,然后删除和添加组件。在重组组件后,您需要重新绘制。

试试这个:

monPanel.removeAll();
monPanel.add(new panel1());
monPanel.revalidate();
monPanel.repaint();
 类似资料:
  • 我有一个,名为,布局设置为。在面板内部有三个,分别添加到、和。现在,我的要求是,如果单击Settings按钮,它将在上方显示一个透明的。这个透明的面板将包含一个不透明的,更小的,居中的面板,其中将包含设置的东西。 我知道我可以使用来实现这一点,但是查看教程,它说您只能使用绝对定位来放置不同深度的组件,我知道这是非常不鼓励的。 是否有其他方法可以做到这一点,而不使用绝对定位?

  • 我有一个JFrame,它被设置为大小为NxN的GridLayout。N由用户在程序开始时作为命令行给出。NxN模式中的JButton被添加到JPanels中的窗口中,由GridLayout(我认为)在位置中设置。 每个JButton是否需要自己的JPanel来使用GridLayout?我的印象是,您可以为所有按钮设置一个JPanel,并将JPanel设置为JButtons的GridLayout。我

  • 我希望我的解释足够好,有人能帮助我:-) 编辑:随着camickr的改进而更新,垂直尺寸问题得到解决。

  • 简而言之,主类是,它是的扩展。还有另一个非常重要的类,称为,它充当主类的。 我注意到的第一个奇怪的事情是查看器没有对控制器的引用。构造函数实例化的匿名实例,如下所示: 在我看来,这似乎是一个糟糕的设计选择,因为控制器有大量的方法(选项、切换等--如下所示的示例),这些方法现在根本无法访问,那么它们有什么好处呢? 然而,后来我想到了更奇怪的事情!如果监听器的这个匿名实例没有引用,为什么还允许它生存呢

  • 我的问题是:我在JPanel中显示了一个JLabel的数组列表和一个JPanel的按钮,当我按下按钮时,我想在JLabel的上方显示我的JPanel。但是当我按下按钮时,我的JPanel在标签下面。 请不要告诉我使用JLayerPane,因为如果我可以不用它,那将是最好的。感谢您的解决方案。 这是我的代码的一个例子:要运行这个,请在这里找到图像100x100: http://www.html5ga

  • 嘿,我正在尝试将一个面板中的所有面板对齐到较大面板的左侧。 以下是我目前面临的情况: 对于主面板(包含所有其他面板的面板-我将称之为主面板!)我在创建它时使用了以下代码: 对于其中的每个面板,我也使用BoxLayout,但是我已经在每个面板上尝试了[jpanel].setAlignmentX(Component.LEFT_ALIGNMENT)之类的东西,但这似乎不起作用。 任何帮助都将不胜感激!