只的JButton显示鼠标悬停 [英] JButton only show up on mouseover

查看:225
本文介绍了只的JButton显示鼠标悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个电子世界争霸战的游戏,并为我的菜单我用paint方法绘制背景菜单。一切都只是直到我把我的鼠标在它随后便出现了3 Jbutton将被隐藏背景后面的好。如果有一种方法来保持的按钮可见,我会AP preciate呢!

我的code:

 进口java.awt.BorderLayout中;
进口java.awt.Graphics;
进口java.awt.GridBagConstraints中;
进口java.awt.GridBagLayout中;
进口java.awt.Image中;
进口java.awt.Insets中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口javax.swing.ImageIcon中;
进口javax.swing.JButton中;
进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
公共类菜单扩展的JFrame {
    私有静态最后的serialVersionUID长1L =;
    字符串名称;
    图象BK;
    公共菜单(){
        超(TRON);
        //设置窗口的大小
        的setSize(800,800);
        //设置默认操作为窗口
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置背景
        ImageIcon的IBK =新的ImageIcon(background.jpg);
        BK = ibk.getImage();
        //设置一个新的面板
        JPanel的P =新JPanel(新的GridBagLayout());
        //使按钮!
        JButton的B =的新的JButton(玩!);
        JButton的B2 =的新的JButton(高分);
        JButton的B3 =的新的JButton(退出);
        b.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                新GameFrame();
            }
        });
        b2.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                新hsview();
            }
        });
        b3.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                System.exit(0);
            }
        });
        //设置GridBagConstraints的组织布局
        GridBagConstraints的GBC =新的GridBagConstraints();
        gbc.insets =新插图(15,15,15,15);
        //使用GBC太空,每个按钮
        gbc.gridx =(0);
        gbc.gridy =(0);
        //他们,那么加入到面板的JFrame
        p.add(B,GBC);
        gbc.gridx =(0);
        gbc.gridy =(1);
        p.add(B2,GBC);
        gbc.gridx =(0);
        gbc.gridy =(2);
        p.add(B3,GBC);
        //添加到框架底部
        增加(P,BorderLayout.SOUTH);
    }
    //背景的东西
    @覆盖
    公共无效漆(图形G){
        super.paint(G);
        g.drawImage(BK,0,0,NULL);
    }
}


解决方案

Swing运用分层的概念为它的绘画......

油漆通话的paintComponent 的paintBorder paintChildren 。通过覆盖油漆并没有打电话给 super.paint ,你$ P $从绘画pvented组件它的各种层。

在摇摆,这是pferred使用的paintComponent 来提供定制的绘画,它可以让你画可能被添加到组件的任何其他组件的下方$ P $。

 公共类TestPaint01 {  公共静态无效的主要(字串[] args){
    新TestPaint01();
  }  公共TestPaint01(){
    EventQueue.invokeLater(新的Runnable(){
      @覆盖
      公共无效的run(){
        尝试{
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }赶上(例外前){
        }        JFrame的帧=新的JFrame(测试);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(新TestPane());
        frame.pack();
        frame.setLocationRelativeTo(NULL);
        frame.setVisible(真);      }
    });
  }  公共类TestPane继承JPanel {    私人形象和backgroundImage;    公共TestPane(){
      尝试{
        BufferedImage的背景= ImageIO.read(新文件(/路径/要/ image.jpg文件));
        //将backgroundImage = background.getScaledInstance(-1,background.getHeight()/ 4,Image.SCALE_SMOOTH);
        将backgroundImage =背景;
      }赶上(IOException异常前){
        ex.printStackTrace();
      }
      的setLayout(新的GridBagLayout());
      添加(新的JButton(你好));
    }    @覆盖
    公共尺寸的get preferredSize(){
      返回将backgroundImage == NULL? super.get preferredSize():新的Dimension(backgroundImage.getWidth(本),backgroundImage.getHeight(本));
    }    @覆盖
    保护无效paintComponent(图形G){
      super.paintComponent方法(G);
      INT X =(的getWidth() - backgroundImage.getWidth(本))/ 2;
      INT Y =(的getHeight() - backgroundImage.getHeight(本))/ 2;
      g.drawImage(将backgroundImage,X,Y,这一点);
    }  }}

您可能会发现在画图机制细看和绘画在AWT和Swing 信息。

I'm working on a "Tron" game, and for my menu I used a paint method to paint a background to the menu. Everything is good except that the 3 JButtons are "hiding" behind the background until I put my mouse over it then it appears. If there is a way to keep the buttons visible, I would appreciate it!

My code:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Menu extends JFrame {
    private static final long serialVersionUID = 1L;
    String name;
    Image bk;
    public Menu() {
        super("TRON");
        // Set the size of window
        setSize(800, 800);
        // Set the default operation for the window
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // Set a background
        ImageIcon ibk = new ImageIcon("background.jpg");
        bk = ibk.getImage();
        // Set a new panel
        JPanel p = new JPanel(new GridBagLayout());
        // Make buttons!
        JButton b = new JButton("Play!");
        JButton b2 = new JButton("High Scores");
        JButton b3 = new JButton("Exit");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new GameFrame();
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new hsview();
            }
        });
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        // Set the GridBagConstraints to organize the layout
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets (15, 15, 15, 15);
        // Use gbc to space out each button
        gbc.gridx = (0);
        gbc.gridy = (0);
        // Add them to the panel then to JFrame
        p.add(b, gbc);
        gbc.gridx = (0);
        gbc.gridy = (1);
        p.add(b2, gbc);
        gbc.gridx = (0);
        gbc.gridy = (2);
        p.add(b3, gbc);
        // Add to the frame at the bottom
        add(p, BorderLayout.SOUTH);
    }
    // Background stuff
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bk, 0, 0, null);
    }
}

解决方案

Swing uses a "layering" concept for it's painting...

paint calls paintComponent, paintBorder and paintChildren. By overriding paint and failing to call super.paint, you've prevented the component from painting it's various layers.

In Swing, it is preferred to use paintComponent to provide custom painting, which allows you to paint underneath any other components that might be added to the component.

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

  }

}

You might find A Closer look at the Paint Mechanism and Painting in AWT and Swing informative.

这篇关于只的JButton显示鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆