6

有谁知道如何使用 NetBeans GUI 构建器工具中的卡片布局?我想根据JRadioButton选择显示面板,所以我想使用卡片布局来布置它。

4

1 回答 1

1

card.next(yourPanel);将遍历你的所有组件mainpanel然后来到第一个。要显示您自己想要的组件,请尝试关注(想想如果有 5 个组件并且您在第 2 个并且想要首先显示,那么您必须通过Vincent Ramdhanie 的示例中的所有其他内容,根据JRL 的答案很好快速跳转到您想要的,但这是另一种方式。

import javax.swing.JLabel;
import javax.swing.JPanel;

public class myJFrame extends javax.swing.JFrame {

    private JPanel panel1, panel2;
    /**
     * Creates new form myJFrame
     */
    public myJFrame() {
        initComponents();
        panel1=new JPanel();
        panel2=new JPanel();

        JLabel lb1=new JLabel("This is panel 1");
        JLabel lb2=new JLabel("This is panel 2");
        panel1.add(lb1);
        panel2.add(lb2);
//make more if you want
//        contentPanel.add(panel1);//show any of the panel first

    }
  private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        buttonPanel1 = new javax.swing.JButton();
        buttonPanel2 = new javax.swing.JButton();
        contentPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        buttonPanel2.setText("Panel 2");
        buttonPanel2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPanel2ActionPerformed(evt);
            }
        });
  ....
  }
  private void buttonPanel2ActionPerformed(java.awt.event.ActionEvent evt) {

        contentPanel.removeAll();
        contentPanel.add(panel2);
        contentPanel.repaint();
        contentPanel.revalidate();
    }

    private void buttonPanel1ActionPerformed(java.awt.event.ActionEvent evt) {
        contentPanel.removeAll();
        contentPanel.add(panel1);
        contentPanel.repaint();
        contentPanel.revalidate();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new myJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton buttonPanel1;
    private javax.swing.JButton buttonPanel2;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel jPanel1;

}

当您拥有一棵树并在树选择上显示面板或组件时使用这种方式。它直接显示该组件。在树上添加一个value change listener并获取选择项并显示适当的面板。

于 2013-07-24T12:31:08.470 回答