English 中文(简体)
How do I work with the Card Layout in the NetBeans GUI builder? [closed]
原标题:
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 2 years ago.

Does anyone know how to work with the Card Layout in the NetBeans GUI builder tool? I want to show panels as per the JRadioButton selection, so I want to lay this out using the Card Layout.

问题回答

card.next(yourPanel); will loop through all the components in your mainpanel then come to first one. To show a component with your own desire try following (think if there are 5 components and you are on the 2 and want to show first then you have to go through rest of all in the Vincent Ramdhanie s example, JRL s answer is good according to that gives a quick jump to one you want, but here is another way.

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;

}

This way is used when you have a tree and show a panel or component on a tree selection. It shows directly that component. On the tree add a value change listener and get the selection item and show appropriate panel.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签