ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – JButton只出现在鼠标上?

2019-07-23 06:13:29  阅读:223  来源: 互联网

标签:java swing paint jbutton


Here是我的代码:我拿出了一些我觉得没必要的东西.我也可能已经取出了一些括号,但我只想展示我的内容.

当我运行程序时,背景图像描绘(它是资源中的PNG),只出现一个按钮(我的PLAY按钮),这是第一个按钮 – 它是自动选择的.

我实际上有四个按钮,但我只在我的代码中包含了播放和说明.除非我将鼠标放在它们上面,否则其他三个不显示.我知道这可能与paint方法有些奇怪,但我不知道如何修复它.

如果我选择一个不同的按钮并最小化窗口然后再次打开它,则所选按钮是唯一出现的按钮.我必须将鼠标移开才能显示其他按钮.

我也在paint方法中添加了super.paint(),我得到了所有按钮,但背景是灰色的.
我认为问题是super.paint()绘制了我的所有按钮,而g.drawImage(bg,0,0,null)只绘制我的背景,我不能不绘制另一个.

对不起,如果这是一团糟.我是Java的新手,我很难说出我想说的话.

public class MainMenu extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */

    //variables
    public static Image bg;

    public static void main(String[] args) {

        MainMenu mainFrame = new MainMenu();
        mainFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        mainFrame.setResizable(false);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setTitle ("Zumby");
        mainFrame.setLayout(null);

        // Loads the background image and stores in bg object.
        try {
            bg = ImageIO.read(new File("zumby.png"));
        } catch (IOException e) {
        }
        mainFrame.setVisible(true);
    }

    /**
     * Overrides the paint method.
     * MONDAY
     */
     public void paint(Graphics g)
     {
        // Draws the img to the BackgroundPanel.
        System.out.println("paint");
        g.drawImage(bg, 0, 0, null);
     }

    /**
     */
    public MainMenu() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 500);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setOpaque(false);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        //create buttons
        JButton btnPlay = new JButton("PLAY");
        btnPlay.setBackground(Color.BLACK);
        btnPlay.setForeground(Color.WHITE);
        btnPlay.setFont(font);
        btnPlay.setBorder(border);
        btnPlay.setFocusPainted(false);

        //if "Play" is clicked

        btnPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent click) {
                setVisible(false);
                new GamePlay(); //opens up GamePlay window
            }
        });
        btnPlay.setBounds(600, 64, 141, 61);
        contentPane.add(btnPlay);

        JButton btnInstructions = new JButton("INSTRUCTIONS");

        btnInstructions.setBounds(600, 160, 141, 61);
        btnInstructions.setBackground(Color.BLACK);
        btnInstructions.setFocusPainted(false);
       // btnInstructions.setEnabled(true);

        contentPane.add(btnInstructions);
        repaint();
        pack(); 
        setVisible(true);

    }

}

解决方法:

Swing使用“分层”概念进行绘画……

paint调用paintComponent,paintBorder和paintChildren.通过覆盖绘制并且无法调用super.paint,您已经阻止组件绘制它的各个层.

在Swing中,最好使用paintComponent来提供自定义绘制,这允许您在可能添加到组件的任何其他组件下面绘制.

public class TestPaint01 {

  public static void main(String[] args) {
    new TestPaint01();
  }

  public TestPaint01() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
        }

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

      }
    });
  }

  public class TestPane extends JPanel {

    private Image backgroundImage;

    public TestPane() {
      try {
        BufferedImage background = ImageIO.read(new File("/path/to/image.jpg"));
        //backgroundImage = background.getScaledInstance(-1, background.getHeight() / 4, Image.SCALE_SMOOTH);
        backgroundImage = background;
      } catch (IOException ex) {
        ex.printStackTrace();
      }
      setLayout(new GridBagLayout());
      add(new JButton("Hello"));
    }

    @Override
    public Dimension getPreferredSize() {
      return backgroundImage == null ? super.getPreferredSize() : new Dimension(backgroundImage.getWidth(this), backgroundImage.getHeight(this));
    }

    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      int x = (getWidth() - backgroundImage.getWidth(this)) / 2;
      int y = (getHeight() - backgroundImage.getHeight(this)) / 2;
      g.drawImage(backgroundImage, x, y, this);
    }

  }

}

您可能会发现A Closer look at the Paint MechanismPainting in AWT and Swing信息丰富.

标签:java,swing,paint,jbutton
来源: https://codeday.me/bug/20190723/1510706.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有