Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JButtons的Java Swing绘制问题_Java_Swing_Jframe_Jpanel - Fatal编程技术网

JButtons的Java Swing绘制问题

JButtons的Java Swing绘制问题,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我正在尝试使用Swing创建一个桌面应用程序。我需要添加一个背景图像到我的框架,还需要添加一些按钮在一些特定的位置,不应该有他们的内容区填充。这就是我到目前为止所做的 public class MainGUI extends JFrame { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() {

我正在尝试使用Swing创建一个桌面应用程序。我需要添加一个背景图像到我的框架,还需要添加一些按钮在一些特定的位置,不应该有他们的内容区填充。这就是我到目前为止所做的

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);

  }

}

执行此操作时,setContentAreaFilled(false)功能不起作用。我想这与那幅画有关,但我不确定。有人能帮我吗?

所以我把你的代码修改了

主要是:

  • 添加了
    btn1.setOpaque(false)
  • 并添加了
    rightButtonPanel.setBackground(颜色.绿色)
范例

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);

    }

}
这产生了

所以,不是按钮(至少在这一点上)出了问题

所以,我改变了
rightButtonPanel.setBackground(颜色.绿色)
to
rightButtonPanel.setOpaque(false)并生成


尝试同时添加
btn1.setOpaque(false)
以使按钮透明。您不应该在
paintComponent
中加载资源,此方法可以快速连续调用多次,这样加载资源将导致应用程序性能下降degrade@MadProgrammerbtn1.setOpaque(假)没有帮助。另外,您能解释一下关于在paintComponent中加载资源的更多信息吗?更好的方法是什么?“另外,您能解释一下在paintComponent中加载资源的更多信息吗?更好的方法是什么?”-在构造函数中加载资源将是一个开始,您是否尝试将
set不透明(false)
添加到
rightButtonPanel
?因为它对我来说是不透明的