Cardlayout,切换低不透明度的面板出现问题

时间:2015-06-10 10:07:32

标签: java opacity cardlayout

我使用JFrame添加具有特定背景图像的相同大小的JPanel。 然后我使用CardLayout在该背景上添加了3个较小的Jpanel。

我使用CardLayout上50%的面板定义了不透明度,以便在面板上看到不透明度较低的mainJPanel背景。我希望你们到目前为止都能理解。

现在我的问题是,当我展示第一个面板时,它的效果很好。但是当我切换到Cardlayout上的下一个面板并使用后退按钮返回第一个面板时,由于不透明度,可以通过第一个面板看到第二个面板。

但我希望看到背景,而不是背景中的第二个面板。有没有可能解决这个问题?我几乎尝试了一切但没有解决方案。 谢谢你的帮助!

package GUIElements;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import java.awt.CardLayout;

import javax.swing.JButton;

import net.proteanit.sql.DbUtils;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class MainFrameSoftware extends JFrame implements CardLayoutSwitch{
private GUIOrgaCustomer customer;
private CardLayout c1;
private JPanel panelCard;
private JPanel panel1;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;

private JPanel panelMainContent;
private JPanel showCustomer;
private JPanel background;
private JButton btnNewCustomer;
private JButton btnShowAllCustomer;
private JButton btnUpdateCustomer;
private JButton btnDeleteCustomer;
private JButton backbtn;
private JButton backbtn2;
private JTable table;
private JScrollPane scrollPane;
private JLabel label;
private JLabel label_1;

public  MainFrameSoftware() {

    this.setSize(1037,666);
    this.setLocationRelativeTo(null);
    this.setResizable(false);

    background = new BackgroundPanel(new ImageIcon("\\\\I-INTRA-03\\IIS-Lehre\\DAPRO-WF4.informatik.hs-ulm.de\\15\\group_together\\GUI_graphics\\background.jpg"));
    getContentPane().add(background);
    background.setLayout(null);

    c1 = new CardLayout();
    panelCard = new JPanel();
    panelCard.setBounds(348, 26, 665, 577);
    panelCard.setLayout(c1);
    background.add(panelCard);

    panel1 = new JPanel();
    panel1.setBackground(new Color(0,0,0,50));
    panel1.setBounds(348, 26, 665, 577);
    panel1.setLayout(null);

    b1 = new JButton("Manage Customer");
    b1.setBounds(12, 45, 274, 90);
    panel1.add(b1);

    b2 = new JButton("Manage Employee");
    b2.setBounds(12, 170, 274, 90);
    panel1.add(b2);

    b3 = new JButton("Manage Equipment");
    b3.setBounds(12, 295, 274, 90);
    panel1.add(b3);

    b4 = new JButton("Manage Event");
    b4.setBounds(379, 45, 274, 90);
    panel1.add(b4);

    b5 = new JButton("Manage Task");
    b5.setBounds(379, 170, 274, 90);
    panel1.add(b5);

    b6 = new JButton("Manage Service Provider");
    b6.setBounds(379, 295, 274, 90);
    panel1.add(b6);




    //Panel Manage Customer
    panelMainContent = new JPanel();
    panelMainContent.setBackground(new Color(0, 191, 255));
    panelMainContent.setBorder(new EmptyBorder(5, 5, 5, 5));
    panelMainContent.setLayout(null);

    btnShowAllCustomer = new JButton("show all Customer");
    btnShowAllCustomer.setBounds(192, 50, 286, 104);
    panelMainContent.add(btnShowAllCustomer);

    btnNewCustomer = new JButton("add Customer");
    btnNewCustomer.setBounds(192, 167, 286, 104);
    panelMainContent.add(btnNewCustomer);

    btnUpdateCustomer = new JButton("update Customer");
    btnUpdateCustomer.setBounds(192, 286, 286, 104);
    panelMainContent.add(btnUpdateCustomer);

    btnDeleteCustomer = new JButton("delete Customer");
    btnDeleteCustomer.setBounds(192, 407, 286, 104);
    panelMainContent.add(btnDeleteCustomer);

    backbtn = new JButton("Back");
    backbtn.setBounds(489,514,148,50);
    panelMainContent.add(backbtn);

    //ende neu
    //Adding show all customer panel

    showCustomer = new JPanel();
    showCustomer.setLayout(null);
    backbtn2 = new JButton("Back");
    backbtn2.setBounds(489, 514,148,50);
    showCustomer.add(backbtn2);

    panelCard.add(panel1,"1");
    panelCard.add(panelMainContent,"2");
    panelCard.add(showCustomer,"3");

    JPanel panel = new JPanel();
    panel.setBackground(new Color(0,0,0,50));
    panel.setBounds(22, 56, 300, 358);
    background.add(panel);

    JPanel panel_1 = new JPanel();
    panel_1.setBackground(new Color(0,0,0,50));
    panel_1.setBounds(22, 447, 300, 130);
    background.add(panel_1);
}

@Override
public void showPanel(String panel) {
    System.out.println(panel);

    c1.show(panelCard, panel);
}

public void setActionListenerMainCustomerButton(ActionListener actionListener) {
    b1.addActionListener(actionListener);
}
public JButton getB1Button(){
    return b1;
}

public JPanel getCustomerPanel(){
    return customer;
}



//Init ActionListener Show all Customer called from Controller
public void setActionListenerCustomerHandle(ActionListener e) {
    btnShowAllCustomer.addActionListener(e);
}
// returns the JButton Show All Customer to the Controller Class
public JButton getBtnShowAllCust() {
    return btnShowAllCustomer;
}

public void showAllCustomer(ResultSet res){
    table = new JTable();
    table.setBounds(1048,899, 1, 1);
    scrollPane = new JScrollPane();
    scrollPane.setBounds(186, 38, 515, 227);
    showCustomer.add(scrollPane);
    scrollPane.setViewportView(table);
    table.setModel(DbUtils.resultSetToTableModel(res));
}
public void setActionListenerBackButton(ActionListener actionListener) {
    backbtn.addActionListener(actionListener);
}
public JButton getBackButton(){
    return backbtn;
}
}


 /*
 * Author: Christian Frindt
 * Description: Class Controller which passes all Data from GUI's to the             
 * */
package ControllerCollection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import addOns.Login;
import DAOCollection.CustomerDAOImpl;
import EntityClasses.Customer;
import GUIElements.GUILogin;
import GUIElements.GUIOrgaCustomer;
import GUIElements.GUI_Main;
import GUIElements.MainFrameSoftware;
import notesDBMySQL.DatabaseHandler;

public class Controller {

private GUILogin gui;
private DatabaseHandler handler;
private GUIOrgaCustomer customerGui;
private CustomerDAOImpl customer;
private MainFrameSoftware main;

public Controller() throws SQLException{

    gui = new GUILogin();
    handler = new DatabaseHandler();
    customerGui = new GUIOrgaCustomer();
    main = new MainFrameSoftware();
    controllActionListenerLogin();
    controllActionListenerBack();
    controllActionListenerMainFrameCustomer();
    controllActionListenerMainFrame();
    //controllActionListenerBack();
}
//ActionListener showall Customer
public void controllActionListenerMainFrameCustomer() {
    main.setActionListenerCustomerHandle(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource()== main.getBtnShowAllCust()){
                customer = new CustomerDAOImpl();
                try {
                    ResultSet re = customer.getAllRows();
                    main.showAllCustomer(re);
                    main.showPanel("3");
                    System.out.println("panel shown");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }   
        }
    });

    /*customerGui.(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource()==customerGui.getBtnaddCustomer()){

            }
        }

    });
    */
}

public void controllActionListenerBack(){
    main.setActionListenerBackButton(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent a) {
            if(a.getSource()== main.getBackButton()){
                main.showPanel("1");
            }
        }
    });
}

//ActionListener Loginbutton
public void controllActionListenerLogin(){

    gui.setActionListenerLogin(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
                if(e.getSource()==gui.getBtnLogin()){
                    try {
                        int result = new Login(getUser(),getPass()).checkLogin() ;

                        switch(result){

                        case 0 : gui.jLabelErrorsetText("Username or Password is missing!");
                            break;

                        case 1 : gui.jLabelErrorsetText("Error - Wrong Username or Password!");
                            break;

                        case 2 : gui.successColorJLabel();
                                 gui.jLabelErrorsetText(" ");
                                 gui.destroyFrame();
                                 main.setVisible(true);
                            break;

                        }
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
}
//ActionListener ManageCustomer Button
public void controllActionListenerMainFrame(){

    main.setActionListenerMainCustomerButton(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource()==main.getB1Button()){
                main.showPanel("2");
            }
        }
    });
}

public String getUser(){
    System.out.println(gui.getUsername());
    return gui.getUsername();
}
public char[] getPass(){
    return gui.getPassword();
}
public static void main(String[] args) throws SQLException{
    Controller control = new Controller();
}
}

0 个答案:

没有答案