0
public class ExamplePane extends javax.swing.JFrame {

    public ExamplePane() {
        // this is constructor 
        initComponents();

    }
    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        jPanel1.setVisible(true);
        jPanel2.setVisible(false);
        jPanel2.invalidate();
    }                                    

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {                                     
        jPanel2.setVisible(true);
        jPanel1.setVisible(false);
        jPanel1.invalidate();
    }                                    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new ExamplePane().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JPanel jPanel4;
        // End of variables declaration                   
    }

当您尝试在单击事件上一一显示 jpanel 时,面板会在其中重叠。所以请帮助我。

4

2 回答 2

2
  1. 您必须使用CardLayout,只有两个JPanel,仅覆盖mouseClicked()(来自 MouseEvents)MouseAdapter

  2. 您对已经可见的 Swing GUI 进行更改,需要通知使用的LayoutManager 有更改,使用(re)validate()andrepaint()而不是invalidate();,然后setVisible也不需要使用

于 2013-10-28T12:58:58.743 回答
0
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class ScrollLabel extends javax.swing.JFrame {

    public  ScrollLabel() {

        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        jPanel1 = new JPanel();
        jPanel1.setBackground(Color.red);
        jPanel1.add(new JLabel("Panel 1"));

        jPanel2 = new JPanel();
        jPanel2.setBackground(Color.green);
        jPanel2.add(new JLabel("Panel 2"));

        jLabel1 = new JLabel("jPanel 1");
        jLabel2 = new JLabel("jPanel 2");

        jPanel1.setBounds(0, 0, getWidth(),getHeight()-50));
        jPanel2.setBounds(0, 0, getWidth(),getHeight()-50));
        jLabel1.setBounds(0, getHeight()-40, 100,30);
        jLabel2.setBounds(getWidth()/2, getHeight()-40, 100,30);

        jLabel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        jLabel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));


        add(jPanel1);
        add(jPanel2);
        add(jLabel1);
        add(jLabel2);

        repaint();

        jLabel1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jLabel1MouseClicked(e);
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });


        jLabel2.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jLabel2MouseClicked(e);
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });

    }

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
        jPanel1.setVisible(true);
        jPanel2.setVisible(false);
        jPanel2.invalidate();
    }

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
        jPanel2.setVisible(true);
        jPanel1.setVisible(false);
        jPanel1.invalidate();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        new ScrollLabel().setVisible(true);

    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration                   
}
于 2013-10-28T16:11:11.353 回答