按钮被背景图像隐藏

时间:2013-10-28 09:55:29

标签: java swing

我在JPanel上设置了背景图像但是当我尝试添加按钮并选择自定义背景面板时,按钮被隐藏,直到我将鼠标移到按钮上。我在下面列出了代码段。

以下是我自定义的JPanel

package au.com.tankwarz.view.custompanels;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class BackgroundPanel extends JPanel
{

    /**
     * 
     */
    private static final long serialVersionUID = 1659728640545162103L;

    public BackgroundPanel()
    {
    }

    @Override
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(loadBackgroundImage(), 0, 0, this);    

        g2d.dispose();  
    }   

    private static BufferedImage loadBackgroundImage()
    {
         BufferedImage bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
         Graphics2D g2d = bi.createGraphics();
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

         // Paint a gradient from top to bottom
        GradientPaint gp = new GradientPaint( 0, 0, Color.BLACK, 0, 600, new Color(0, 0, 255).darker( ).darker() );

        g2d.setPaint( gp );
        g2d.fillRect( 0, 0, 800, 600 );

        g2d.dispose();

        return bi;
    }
}

这里是我尝试使用它来显示面板上的按钮的地方。

package au.com.tankwarz.view;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import org.jdesktop.application.Application;

import au.com.tankwarz.view.custompanels.BackgroundPanel;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

public class NewGamePanel extends BackgroundPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JLabel numberOfPlayersLable;
    private JComboBox numberOfPlayersCB;
    private JButton cancelButton;
    private JButton createPlayersButton;
    private JComboBox numberOfTanksCB;
    private JLabel numberOfTanksLable;
    private JComboBox numberOfRoundsCB;
    private JLabel numberOfRounds;

    public static final String[] numberOfPlayersCBValues = new String[] { "Two", "Three", "Four" };

    public static final String[] numberOfRoundsCBValues = new String[] { "One", "Two", "Three", "Four" };

    public static final String[] numberOfTanksCBValues = new String[] { "One", "Two", "Three", "Four" };

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                //new NewGamePanel();


                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new NewGamePanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });


    }

    public NewGamePanel() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            this.setLayout(thisLayout);
            this.setPreferredSize(new java.awt.Dimension(800, 600));
            this.setSize(800, 600);
            this.setOpaque(true);
            this.setName("this");
            {
                numberOfPlayersLable = new JLabel();
                this.add(numberOfPlayersLable, new AnchorConstraint(144, 320, 201, 77, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                numberOfPlayersLable.setName("numberOfPlayersLable");
                numberOfPlayersLable.setPreferredSize(new java.awt.Dimension(60, 40));
            }
            {
                ComboBoxModel numberOfPlayersCBModel = 
                    new DefaultComboBoxModel(numberOfPlayersCBValues);
                numberOfPlayersCB = new JComboBox();
                this.add(numberOfPlayersCB, new AnchorConstraint(125, 697, 219, 386, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                numberOfPlayersCB.setModel(numberOfPlayersCBModel);
                numberOfPlayersCB.setPreferredSize(new java.awt.Dimension(60, 40));
            }
            {
                numberOfRounds = new JLabel();
                this.add(numberOfRounds, new AnchorConstraint(298, 371, 355, 77, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                numberOfRounds.setName("numberOfRounds");
                numberOfRounds.setPreferredSize(new java.awt.Dimension(60, 40));
            }
            {
                ComboBoxModel numberOfRoundsCBModel = 
                    new DefaultComboBoxModel(numberOfRoundsCBValues);
                numberOfRoundsCB = new JComboBox();
                this.add(numberOfRoundsCB, new AnchorConstraint(283, 697, 366, 386, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                numberOfRoundsCB.setModel(numberOfRoundsCBModel);
                numberOfRoundsCB.setName("numberOfRoundsCB");
                numberOfRoundsCB.setPreferredSize(new java.awt.Dimension(60, 40));
            }
            {
                numberOfTanksLable = new JLabel();
                this.add(numberOfTanksLable, new AnchorConstraint(453, 320, 509, 77, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                numberOfTanksLable.setName("numberOfTanksLable");
                numberOfTanksLable.setPreferredSize(new java.awt.Dimension(60, 40));
            }
            {
                ComboBoxModel numberOfTanksCBModel = 
                    new DefaultComboBoxModel(numberOfTanksCBValues);
                numberOfTanksCB = new JComboBox();
                this.add(numberOfTanksCB, new AnchorConstraint(437, 697, 520, 386, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                numberOfTanksCB.setModel(numberOfTanksCBModel);
                numberOfTanksCB.setPreferredSize(new java.awt.Dimension(60, 40));
            }
            {
                createPlayersButton = new JButton();
                this.add(createPlayersButton, new AnchorConstraint(795, 758, 878, 511, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                createPlayersButton.setName("createPlayersButton");
                createPlayersButton.setPreferredSize(new java.awt.Dimension(99, 25));
            }
            {
                cancelButton = new JButton();
                this.add(cancelButton, new AnchorConstraint(795, 248, 878, 128, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                cancelButton.setName("cancelButton");
                cancelButton.setPreferredSize(new java.awt.Dimension(48, 25));
            }
            Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setCreatePlayersButtonActionListener(ActionListener actionListener)
    {
        createPlayersButton.addActionListener(actionListener);
    }

    public void setCancelButtonActionListener(ActionListener actionListener)
    {
        cancelButton.addActionListener(actionListener);
    }

    public JComboBox getNumberOfPlayersCB()
    {
        return numberOfPlayersCB;
    }

    public void setNumberOfPlayersCB(JComboBox numberOfPlayersCB)
    {
        this.numberOfPlayersCB = numberOfPlayersCB;
    }

    public JComboBox getNumberOfTanksCB()
    {
        return numberOfTanksCB;
    }

    public void setNumberOfTanksCB(JComboBox numberOfTanksCB)
    {
        this.numberOfTanksCB = numberOfTanksCB;
    }

    public JComboBox getNumberOfRoundsCB()
    {
        return numberOfRoundsCB;
    }

    public void setNumberOfRoundsCB(JComboBox numberOfRoundsCB)
    {
        this.numberOfRoundsCB = numberOfRoundsCB;
    }

}

任何帮助都会受到赞赏,因为我一直在努力解决这个问题。

1 个答案:

答案 0 :(得分:2)

  1. 不要在任何paintXxx方法中更改组件的状态,这可能会导致重启事件,重复此过程,直到CPU运行不热。
  2. 永远不要在任何paintXxx内更改组件的不透明度状态,这将导致一系列重复的重绘,因为Swing突然开始尝试找出当前可见的组件...
  3. 不要处置您未创建的Graphics上下文,这样做可能会阻止您在某些系统上呈现之后所绘制的内容。
  4. 尽量不要在每个绘制周期中创建背景图像,这不仅从内存的角度来看很昂贵,而且从时间的角度来看也很昂贵
  5. 我不确定你为什么要翻转不透明状态,你真的不希望它是透明的。只需致电super.paintComponent以准备图形状态,然后将图像绘制在顶部。

    您还应避免使用setPreferredSize,有关详细信息,请参阅Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? ...

    只是说明一点,这是你编码产生的(在我纠正了油漆问题之后)

    enter image description here

    这是我的测试代码产生的

    enter image description here

    已更新测试代码

    import com.apple.eawt.Application;
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.RenderingHints;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.ComboBoxModel;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class BackgroundPanel extends JPanel {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    //new NewGamePanel();
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new NewGamePanel());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
    
        }
    
        public BackgroundPanel() {
        }
    
        private static BufferedImage bi;
    
        @Override
        public void paintComponent(final Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(loadBackgroundImage(), 0, 0, this);
            g2d.dispose();
        }
    
        private static BufferedImage loadBackgroundImage() {
            if (bi == null) {
                bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = bi.createGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
                // Paint a gradient from top to bottom
                GradientPaint gp = new GradientPaint(0, 0, Color.BLACK, 0, 600, new Color(0, 0, 255).darker().darker());
    
                g2d.setPaint(gp);
                g2d.fillRect(0, 0, 800, 600);
    
                g2d.dispose();
            }
    
            return bi;
        }
    
        public static class NewGamePanel extends BackgroundPanel {
    
            /**
             *
             */
            private static final long serialVersionUID = 1L;
            private JLabel numberOfPlayersLable;
            private JComboBox numberOfPlayersCB;
            private JButton cancelButton;
            private JButton createPlayersButton;
            private JComboBox numberOfTanksCB;
            private JLabel numberOfTanksLable;
            private JComboBox numberOfRoundsCB;
            private JLabel numberOfRounds;
    
            public static final String[] numberOfPlayersCBValues = new String[]{"Two", "Three", "Four"};
    
            public static final String[] numberOfRoundsCBValues = new String[]{"One", "Two", "Three", "Four"};
    
            public static final String[] numberOfTanksCBValues = new String[]{"One", "Two", "Three", "Four"};
    
            public NewGamePanel() {
                super();
                initGUI();
            }
    
            private void initGUI() {
                try {
                    GridBagLayout thisLayout = new GridBagLayout();
                    this.setLayout(thisLayout);
                    this.setPreferredSize(new java.awt.Dimension(800, 600));
                    this.setSize(800, 600);
                    this.setOpaque(true);
                    this.setName("this");
    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridwidth = gbc.REMAINDER;
                    {
                        numberOfPlayersLable = new JLabel();
                        this.add(numberOfPlayersLable, gbc);
                        numberOfPlayersLable.setName("numberOfPlayersLable");
                    }
                    {
                        ComboBoxModel numberOfPlayersCBModel
                                        = new DefaultComboBoxModel(numberOfPlayersCBValues);
                        numberOfPlayersCB = new JComboBox();
                        this.add(numberOfPlayersCB, gbc);
                        numberOfPlayersCB.setModel(numberOfPlayersCBModel);
                    }
                    {
                        numberOfRounds = new JLabel();
                        this.add(numberOfRounds, gbc);
                        numberOfRounds.setName("numberOfRounds");
                    }
                    {
                        ComboBoxModel numberOfRoundsCBModel
                                        = new DefaultComboBoxModel(numberOfRoundsCBValues);
                        numberOfRoundsCB = new JComboBox();
                        this.add(numberOfRoundsCB, gbc);
                        numberOfRoundsCB.setModel(numberOfRoundsCBModel);
                        numberOfRoundsCB.setName("numberOfRoundsCB");
                    }
                    {
                        numberOfTanksLable = new JLabel();
                        this.add(numberOfTanksLable, gbc);
                        numberOfTanksLable.setName("numberOfTanksLable");
                    }
                    {
                        ComboBoxModel numberOfTanksCBModel
                                        = new DefaultComboBoxModel(numberOfTanksCBValues);
                        numberOfTanksCB = new JComboBox();
                        this.add(numberOfTanksCB, gbc);
                        numberOfTanksCB.setModel(numberOfTanksCBModel);
                    }
                    {
                        createPlayersButton = new JButton();
                        this.add(createPlayersButton, gbc);
                        createPlayersButton.setName("createPlayersButton");
                    }
                    {
                        cancelButton = new JButton();
                        this.add(cancelButton, gbc);
                        cancelButton.setName("cancelButton");
                    }
    //                Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            public void setCreatePlayersButtonActionListener(ActionListener actionListener) {
                createPlayersButton.addActionListener(actionListener);
            }
    
            public void setCancelButtonActionListener(ActionListener actionListener) {
                cancelButton.addActionListener(actionListener);
            }
    
            public JComboBox getNumberOfPlayersCB() {
                return numberOfPlayersCB;
            }
    
            public void setNumberOfPlayersCB(JComboBox numberOfPlayersCB) {
                this.numberOfPlayersCB = numberOfPlayersCB;
            }
    
            public JComboBox getNumberOfTanksCB() {
                return numberOfTanksCB;
            }
    
            public void setNumberOfTanksCB(JComboBox numberOfTanksCB) {
                this.numberOfTanksCB = numberOfTanksCB;
            }
    
            public JComboBox getNumberOfRoundsCB() {
                return numberOfRoundsCB;
            }
    
            public void setNumberOfRoundsCB(JComboBox numberOfRoundsCB) {
                this.numberOfRoundsCB = numberOfRoundsCB;
            }
    
        }
    
    }