简体   繁体   English

JButton的Java Swing绘画问题

[英]Java Swing Painting Issue with JButtons

What I'm trying to do is to create a desktop application using Swing. 我想做的是使用Swing创建一个桌面应用程序。 I need to add a background image to my frame and also add some buttons on some specific locations which should NOT have their content area filled. 我需要在框架中添加背景图片,并在某些特定位置添加一些按钮,这些按钮不应填充其内容区域。 So, here is what I've done so far; 因此,这是我到目前为止所做的;

public class MainGUI extends JFrame {

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainGUI window = new MainGUI();
                window.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
  }


  public MainGUI() {
    setUndecorated(true);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setSize(screenSize);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    initialize();
  }


  private void initialize() {

    JPanel mainPanel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            try {
                g.drawImage(new ImageIcon(ImageIO.read(new File("a.png"))).getImage(), 0, 0, null);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    mainPanel.setLayout(new BorderLayout());

    JButton btn1 = new JButton();
    btn1 .setAlignmentX(Component.CENTER_ALIGNMENT);
    btn1 .setContentAreaFilled(false);
    btn1 .setBorder(new EmptyBorder(0, 0, 0, 0));
    btn1 .setIcon(new ImageIcon("btn1.png"));

    JPanel rightButtonPanel = new JPanel();
    rightButtonPanel.setLayout(new BoxLayout(rightButtonPanel, BoxLayout.Y_AXIS));
    rightButtonPanel.add(btn1);

    mainPanel.add(rightButtonPanel, BorderLayout.EAST);

    this.setContentPane(mainPanel);

  }

}

When I do this, setContentAreaFilled(false) feature does not work. 当我这样做时,setContentAreaFilled(false)功能不起作用。 I suppose it's related to the painting but I'm not sure. 我想这与绘画有关,但我不确定。 Can anyone help me with this please? 有人可以帮我吗?

So I took your code and modified it. 因此,我接受了您的代码并对其进行了修改。

Primarily: 主要:

  • Added btn1.setOpaque(false); 添加了btn1.setOpaque(false);
  • And added rightButtonPanel.setBackground(Color.GREEN); 并添加了rightButtonPanel.setBackground(Color.GREEN);

Example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MainGUI extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainGUI window = new MainGUI();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MainGUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initialize();
        pack();
    }

    private void initialize() {

        JPanel mainPanel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
            }
        };

        mainPanel.setBackground(Color.RED);

        mainPanel.setLayout(new BorderLayout());

        JButton btn1 = new JButton();
        btn1.setAlignmentX(Component.CENTER_ALIGNMENT);
        btn1.setContentAreaFilled(false);
        btn1.setOpaque(false);
        btn1.setBorder(new EmptyBorder(0, 0, 0, 0));
        btn1.setText("This is a test");

        JPanel rightButtonPanel = new JPanel();
        rightButtonPanel.setBackground(Color.GREEN);
        rightButtonPanel.setLayout(new BoxLayout(rightButtonPanel, BoxLayout.Y_AXIS));
        rightButtonPanel.add(btn1);

        mainPanel.add(rightButtonPanel, BorderLayout.EAST);

        this.setContentPane(mainPanel);

    }

}

This produced 这产生了

填充

So, it's not the buttons (at least at this point) which are at fault. 因此,不是错误的按钮(至少在这一点上)。

So, I changed rightButtonPanel.setBackground(Color.GREEN); 因此,我更改了rightButtonPanel.setBackground(Color.GREEN); to rightButtonPanel.setOpaque(false); rightButtonPanel.setOpaque(false); and it produced 它产生了

未填满

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM