2

我正在尝试将方法 cardlayout.show 调用到下面的类中。

protected void makelabeltxt(final String text, GridBagLayout gridbag, GridBagConstraints c, int width, int height, final String panel) {
    final JLabel label = new JLabel(text);              
    Dimension d = new Dimension(width, height);
    gridbag.setConstraints(label, c);
    label.setFont(new Font("Sans Serif", 0, 11));
    label.setPreferredSize(d);


    label.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {                
            System.out.println(panel);
            fp.showPanel(panel);
            System.out.println("mouse clicked "+text);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            label.setText("  " + text);
            System.out.println("mouse pressed "+text);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            label.setText(" " + text);
            System.out.println("mouse release "+text);
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            label.setText(" " + text);
            System.out.println("mouse entered "+text);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            label.setText(text);

        }
    });
    add(label);
}

在另一个类中,我创建了这样的方法,这是我要调用的方法。我想交换jpanel,但在另一个jpanel:下面的方法是有效的,但是当 cardlayout 显示脚本时,什么也没发生,println 脚本很好

public void showPanel(String panel){
    System.out.println("panel : "+panel);       
    cl.show(mainInPanel, panel);
}

这就是我使用卡片布局将 JPanel 添加到 JPanel 的方式

public void initPanelFrm(JPanel panel, String gambar) {             
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        panel.setLayout(gridbag);
        panel.setPreferredSize(new Dimension(810, 280));
        panel.setBackground(Color.white);
        makelabelimg("com/reporting/image/"+gambar+".png", 444, 23, panel);     
        panel.setVisible(false);
        mainInPanel.add(panel, gambar); 
    }

    public void initInPanel(){
        rekappenjualanpsc = new JPanel();
        initPanelFrm(rekappenjualanpsc, "rekappenjualanpsc");
        rekapbastudenganmaskapai = new JPanel();
        initPanelFrm(rekapbastudenganmaskapai, "rekapbastudenganmaskapai");
        rekapitulasisetoranmaskapai = new JPanel();
        initPanelFrm(rekapitulasisetoranmaskapai, "rekapitulasisetoranmaskapai");
        rekappenjualanpscpermaskapai = new JPanel();
        initPanelFrm(rekappenjualanpscpermaskapai, "rekappenjualanpscpermaskapai");
        rekappungutanlangsungpsc = new JPanel();
        initPanelFrm(rekappungutanlangsungpsc, "rekappungutanlangsungpsc");
        laporandetailpetugas = new JPanel();
        initPanelFrm(laporandetailpetugas, "laporandetailpetugas");
        detailpembayaranpenumpang = new JPanel();
        initPanelFrm(detailpembayaranpenumpang, "detailpembayaranpenumpang");

    }

4

0 回答 0