Java嵌套面板具有alpha值的透明度问题

时间:2014-12-26 14:34:59

标签: java swing user-interface jpanel alpha-transparency

我是一名三年级计算机工程师学生,我正在尝试做一个游戏项目。我在JFrame中添加了背景图片。我试图使其他面板透明,我添加到框架。我使用alpha值,例如:new Color(0,0,0,125)。我也在我的程序中使用cardLayout,并在中心面板上每次调用一个新段或新页面; alphavalue将整个面板透明化并将其实现到选定的面板,这会产生问题。示例:我在左侧面板上有7个按钮,当我点击犯罪按钮时,犯罪面板进入中央面板,左侧面板再次进入中心面板内部(透明地)。

我有16个班级,所以我只添加了主班。 抱歉语法不好。我希望你能理解我并帮助我。

    import java.util.*;

import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;

public class TheMafia {

    public static ImageIcon scale(ImageIcon i,int x,int y) {
        Image img = i.getImage();
        Image newimg = img.getScaledInstance(x,y,Image.SCALE_SMOOTH);
        i = new ImageIcon(newimg);
        return i;
    }

    public static void setButton (JButton b,int x,int y) {
        b.setPreferredSize(new Dimension(x,y));
        b.setBackground(Color.gray);
        b.setForeground(Color.white);
        b.setBorder(new LineBorder(Color.black,1));
        b.setFont(new Font("Serif",Font.BOLD,18));
    }

    public static void main(String[] args) {

        ImageIcon home2 = new ImageIcon("home.jpg");
        home2 = scale(home2,1366,768);
        JFrame theMafia = new JFrame();
        theMafia.setTitle("The Mafia Game - Best game in the world!");
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        theMafia.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theMafia.setContentPane(new JLabel(home2));
        theMafia.setLayout(new BorderLayout());
        //theMafia.setLayout(new FlowLayout());
        //theMafia.setExtendedState(JFrame.MAXIMIZED_BOTH);
        theMafia.setSize(800,700);
        theMafia.setLocationRelativeTo(null);
        theMafia.setVisible(true);


        p1.setBackground(new Color(0,0,0,35));
        p2.setBackground(new Color(0,0,0,65));
        p1.setPreferredSize(new Dimension(250,150));
        p2.setPreferredSize(new Dimension(250,150));
        //theMafia.add(p1);
        //theMafia.add(p2);







        // kullanıcı oluşturuldu
        User u1 = new User();

        // suçlar oluşturuldu
        Crime c1 = new Crime();
        c1.setName("Yaşlı Kadın");
        c1.setDifficulty(5);
        c1.setStrength(1);
        c1.setMoney(11);

        Crime c2 = new Crime();
        c2.setName("Dükkan Hırsızlığı");
        c2.setDifficulty(10);
        c2.setStrength(3);
        c2.setMoney(67);

        Crime c3 = new Crime();
        c3.setName("Araba Hırsızlığı");
        c3.setDifficulty(20);
        c3.setStrength(6);
        c3.setMoney(133);










        // suçun seçilmesi
        final JPanel crimes = new JPanel(new CardLayout());
        //crimes.setBackground(new Color(0,0,0,65));

        ImageIcon suçişle = new ImageIcon("suçişle.jpg");
        suçişle = scale(suçişle,50,50);
        JButton yap = new JButton("Suçu işle!",suçişle);
        setButton(yap,100,65);

        JPanel crime1 = new JPanel(new GridLayout(2,1));
        crime1.setBackground(new Color(0,0,0,35));
        crime1.setForeground(Color.white);

        JLabel crime1Info = new JLabel("Suç : "+c1.getName()+"\n Para : "+c1.getMoney()+"\n Yapabilme ihtimali : "+c1.getCapable()+"\n Güç : "+c1.getStrength());
        crime1Info.setFont(new Font("Serif",Font.BOLD,15));

        crime1.add(crime1Info);
        crime1.add(yap);

        JPanel crime2 = new JPanel(new GridLayout(2,1));
        crime2.setBackground(new Color(0,0,0,35));
        crime2.setForeground(Color.white);

        JLabel crime2Info = new JLabel("Suç : "+c2.getName()+"\n Para : "+c2.getMoney()+"\n Yapabilme ihtimali : "+c2.getCapable()+"\n Güç : "+c2.getStrength());
        crime2Info.setFont(new Font("Serif",Font.BOLD,15));

        crime2.add(crime2Info);
        crime2.add(yap);

        JPanel crime3 = new JPanel();
        crime3.setBackground(new Color(0,0,0,35));
        crime3.setForeground(Color.white);

        JLabel crime3Info = new JLabel("Suç : "+c3.getName()+"\n Para : "+c3.getMoney()+"\n Yapabilme ihtimali : "+c3.getCapable()+"\n Güç : "+c3.getStrength());
        crime2Info.setFont(new Font("Serif",Font.BOLD,15));

        crime3.add(crime3Info);
        crime3.add(yap);

        crimes.add(crime1,c1.getName());
        crimes.add(crime2,c2.getName());
        crimes.add(crime3,c3.getName());

        String crimesNames [] =   {c1.getName(),c2.getName(),c3.getName()};
        JComboBox crimesbox = new JComboBox(crimesNames);
        crimesbox.setEditable(false);
        crimesbox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                CardLayout cl = (CardLayout) (crimes.getLayout());
                cl.show(crimes,(String)evt.getItem());
            }
        });

        // menu
        final JPanel menus = new JPanel(new CardLayout());
        //menus.setBackground(new Color(0,0,0,35));
        // crime
        JPanel crime = new JPanel(new BorderLayout());
        crime.setBackground(new Color(0,0,0,35));
        crime.add(crimesbox,BorderLayout.PAGE_START);
        crime.add(crimes,BorderLayout.SOUTH);

        ImageIcon crimeimage = new ImageIcon("thief.png");
        crimeimage = scale(crimeimage,50,50);

        final JButton crimeButton = new JButton("Suçlar",crimeimage);
        setButton(crimeButton,178,76);

        crimeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) (menus.getLayout());
                if (e.getSource() == crimeButton) {
                    cl.show(menus,"suç");
                }
            }
        });

        // weapon shop
        JPanel weaponShop = new JPanel();
        //weaponShop.setBackground(new Color(0,0,0,125));

        final JButton weaponShopButton = new JButton("Silah Dükkanı");
        setButton(weaponShopButton,178,76);

        // building
        JPanel buildingPanel = new JPanel();
        //buildingPanel.setBackground(new Color(0,0,0,125));

        final JButton buildingButton = new JButton("Binalar");
        setButton(buildingButton,178,76);

        // nightlife
        JPanel nightLife = new JPanel();
        //nightLife.setBackground(new Color(0,0,0,35));

        final JButton nightLifeButton = new JButton("Gece Hayatı");
        setButton(nightLifeButton,178,76);

        // treatment center
        JPanel treatmentCenter = new JPanel();
        //treatmentCenter.setBackground(new Color(0,0,0,35));

        final JButton treatmentCenterButton = new JButton("Tedavi Merkezi");
        setButton(treatmentCenterButton,178,76);

        // casino
        JPanel casinoPanel = new JPanel();
        //casinoPanel.setBackground(new Color(0,0,0,35));

        final JButton casinoButton = new JButton("Gazino");
        setButton(casinoButton,178,76);

        // home page
        JPanel home = new JPanel();
        home.setBackground(new Color(0,0,0,35));

        ImageIcon homeimage = new ImageIcon("home.jpg");
        homeimage = scale(homeimage,1200,800);

        JLabel homelabel= new JLabel();
        home.add(homelabel);

        ImageIcon homeicon = new ImageIcon("home_icon.png");
        homeicon = scale(homeicon,50,50);

        final JButton homeButton = new JButton("Home",homeicon);
        setButton(homeButton,178,76);

        homeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) (menus.getLayout());
                if (e.getSource() == homeButton) {
                    cl.show(menus,"home");
                }
            }
        });

        menus.add(home,"home");
        menus.add(crime,"suç");
        menus.add(weaponShop,"silahDükkanı");
        menus.add(buildingPanel,"bina");
        menus.add(nightLife,"geceHayatı");
        menus.add(treatmentCenter,"TedaviMerkezi");
        menus.add(casinoPanel,"gazino");

        Color grisi=new Color(13,13,13);
        JPanel menusButton = new JPanel(new GridLayout(10,1));
        //menusButton.setBackground(grisi);
        menusButton.add(homeButton);
        menusButton.add(crimeButton);
        menusButton.add(weaponShopButton);
        menusButton.add(buildingButton);
        menusButton.add(nightLifeButton);
        menusButton.add(treatmentCenterButton);
        menusButton.add(casinoButton);
        menusButton.setOpaque(false);





        theMafia.add(menusButton,BorderLayout.WEST);
        theMafia.add(menus,BorderLayout.CENTER);
    }
}

1 个答案:

答案 0 :(得分:1)

Swing无法正确处理透明背景。 Swing期望组件是不透明的或不透明的,并且透明度会导致问题,因为组件都不是。

查看Background With Transparency以获取更多信息以及解决问题的几种解决方案。