简体   繁体   English

鼠标悬停之前不显示JButton

[英]JButtons not displaying until mouse hover

I'm creating a gui that has 2 panels. 我正在创建一个具有2个面板的GUI。 When the gui is first loaded only one panel is visible and when a button is pressed the new panel is displayed. 首次加载gui时,只有一个面板可见,并且当按下按钮时,将显示新面板。 The problem is that on the 2nd panel when it loads the buttons are invisible and only display when the mouse hovers over them. 问题在于,在第二个面板上加载按钮时,它们是不可见的,仅当鼠标悬停在它们上方时才显示。 On top of that when you move the screen they become invisible again and need to be hovered over to be displayed again. 最重要的是,当您移动屏幕时,它们再次变得不可见,需要将鼠标悬停以再次显示。 I really don't know what to try as I have looked at methods of having multiple panels and this seems the best way to do it and aswell as this the buttons are implmented the same way I have implemented them for the 1st panel and they render correctly. 我真的不知道尝试什么,因为我研究了具有多个面板的方法,这似乎是最好的方法,而且按钮的实现方式与我为第一个面板实现它们的方式相同,并且它们呈现正确。

Full Code 完整代码

import javax.swing.*;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Gui {
    private JFrame frame;
    private JPanel panel1;
    private JPanel panel2;
    private JButton btnShip1, btnShip2, btnShip3, btnTutorial, btnLeftControl, btnHull, btnTargeting, btnRadar, btnWarning, btnRightControl;
    private JTextField txtCharacterName, txtShipName;
    private JLabel welcome, background;

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

    public Gui(){
        createWindow();
        addButtons();
        addFields();
        addWelcome();

        frame.add(panel1);
        frame.setVisible(true);

        addBackground();
        addShipControls();
    }

    public void createWindow(){
        frame = new JFrame();
        frame.setTitle("Space Battle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(642, 518);

        panel1 = new JPanel();
        panel1.setLayout(null);
        panel1.setBackground(Color.decode("#242627"));

        panel2 = new JPanel();
        panel2.setLayout(null);
        panel2.setBackground(Color.decode("#242627"));
    }

    public void addButtons(){
        btnShip1 = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/cruiserSelectBtn.jpg");
            btnShip1.setIcon(img);
        } 
        catch (Exception e) {}
        try 
        {
            ImageIcon img = new ImageIcon ("resources/button_1_hover.gif");
            btnShip1.setRolloverIcon(img);
        } 
        catch (Exception e) {}
        btnShip1.setBounds(246,0,380,160);
        btnShip1.setMargin(new Insets(0, 0, 0, 0));
        btnShip1.addActionListener(new cruiserSelectHandler());
        btnShip1.setBorder(null);
        panel1.add (btnShip1);

        btnShip2 = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/fighterSelectBtn.jpg");
            btnShip2.setIcon(img);
        } 
        catch (Exception e) {}
        try 
        {
            ImageIcon img = new ImageIcon ("resources/button_2_hover.gif");
            btnShip2.setRolloverIcon(img);
        } 
        catch (Exception e) {}
        btnShip2.setBounds(246,160,380,160);
        btnShip2.setMargin(new Insets(0, 0, 0, 0));
        btnShip2.addActionListener(new fighterSelectHandler());
        btnShip2.setBorder(null);
        panel1.add (btnShip2);

        btnShip3 = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/battleSelectBtn.jpg");
            btnShip3.setIcon(img);
        } 
        catch (Exception e) {}
        try 
        {
            ImageIcon img = new ImageIcon ("resources/button_3_hover.gif");
            btnShip3.setRolloverIcon(img);
        } 
        catch (Exception e) {}
        btnShip3.setBounds(246,320,380,160);
        btnShip3.setMargin(new Insets(0, 0, 0, 0));
        btnShip3.addActionListener(new battleSelectHandler());
        btnShip3.setBorder(null);
        panel1.add (btnShip3);

        btnTutorial = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/tutorialBtn.jpg");
            btnTutorial.setIcon(img);
        } 
        catch (Exception e) {}
        btnTutorial.setBounds(5,426,234,49);
        btnTutorial.setMargin(new Insets(0, 0, 0, 0));
        panel1.add (btnTutorial);
    }

    public void addFields(){
        txtCharacterName = new JTextField("Insert Character Name");
        txtCharacterName.setBounds(12,260,220,35);
        panel1.add(txtCharacterName);

        txtShipName = new JTextField("Insert Ship Name");
        txtShipName.setBounds(12,315,220,35);
        panel1.add(txtShipName);
    }

    public void addWelcome(){
        welcome =  new JLabel ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/welcome.jpg");
            welcome.setIcon(img);
        } 
        catch (Exception e) {}
        welcome.setBounds(0,0,247,229);
        panel1.add(welcome);
    }
    //Class used to change panels
    class cruiserSelectHandler implements ActionListener {
        public void actionPerformed(ActionEvent even) {
            frame.add(panel2);
            panel1.setVisible(false);
        }
    }

    class fighterSelectHandler implements ActionListener {
        public void actionPerformed(ActionEvent even) {
            frame.add(panel2);
            panel1.setVisible(false);
        }
    }

    class battleSelectHandler implements ActionListener {
        public void actionPerformed(ActionEvent even) {
            frame.add(panel2);
            panel1.setVisible(false);
        }
    }

    public void addBackground(){
            //Is the background interfering?
        background =  new JLabel ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/background.jpg");
            background.setIcon(img);
        } 
        catch (Exception e) {}
        background.setBounds(0,0,640,480);
        panel2.add(background);
    }

    //Class used to add controls/buttons
    public void addShipControls(){
        btnLeftControl = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/left_controls.png");
            btnLeftControl.setIcon(img);
        } 
        catch (Exception e) {}
        btnLeftControl.setBounds(-8,319,131,160);
        btnLeftControl.setMargin(new Insets(0, 0, 0, 0));
        btnLeftControl.setBorder(null);
        panel2.add (btnLeftControl);

        btnHull = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/hull_controls.png");
            btnHull.setIcon(img);
        } 
        catch (Exception e) {}
        btnHull.setBounds(123,319,91,160);
        btnHull.setMargin(new Insets(0, 0, 0, 0));
        btnHull.setBorder(null);
        panel2.add (btnHull); 

        btnTargeting = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/targeting_controls.png");
            btnTargeting.setIcon(img);
        } 
        catch (Exception e) {}
        btnTargeting.setBounds(214,319,202,160);
        btnTargeting.setMargin(new Insets(0, 0, 0, 0));
        btnTargeting.setBorder(null);
        panel2.add (btnTargeting);

        btnWarning = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/warning_controls.png");
            btnWarning.setIcon(img);
        } 
        catch (Exception e) {}
        btnWarning.setBounds(416,411,86,68);
        btnWarning.setMargin(new Insets(0, 0, 0, 0));
        btnWarning.setBorder(null);
        panel2.add (btnWarning);

        btnRadar = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/radar_idea.gif");
            btnRadar.setIcon(img);
        } 
        catch (Exception e) {}
        btnRadar.setBounds(416,319,86,92);
        btnRadar.setMargin(new Insets(0, 0, 0, 0));
        btnRadar.setBorder(null);
        panel2.add (btnRadar);

        btnRightControl = new JButton ();
        try 
        {
            ImageIcon img = new ImageIcon ("resources/right_controls.png");
            btnRightControl.setIcon(img);
        } 
        catch (Exception e) {}
        btnRightControl.setBounds(502,319,131,160);
        btnRightControl.setMargin(new Insets(0, 0, 0, 0));
        btnRightControl.setBorder(null);
        panel2.add (btnRightControl);

    }
}

when a button is pressed the new panel is displayed. 当按下按钮时,将显示新面板。

The problem is: 问题是:

  1. you are using a null layout 您正在使用空布局
  2. you are add two different panels to the frame. 您将在框架中添加两个不同的面板。

By default Swing paints components in the reverse order that they were added. 默认情况下,Swing以相反的顺序绘制组件。

So when you add panel1, it is the only panel of the frame so it is painted. 因此,当添加panel1时,它是框架中唯一的面板,因此将其绘制。

Then you click a button and add panel2. 然后单击一个按钮并添加panel2。 So Swing paints panel2 and then repaints panel1 on top, so panel2 is hidden. 因此,Swing绘制panel2,然后在顶部重新绘制panel1,因此panel2被隐藏。 When you mouse over the button it appears because buttons listen for mouseEntered events and the button gets repainted. 当您将鼠标悬停在按钮上时,它会出现,因为按钮会监听mouseEntered事件,并且按钮会重新绘制。

When you resize the screen panel2 is painted and then panel1 is painted so you have the problem again. 调整屏幕大小时,首先绘制了panel2的颜色,然后又绘制了panel1的颜色,所以您再次遇到问题。

The solution is to use a proper layout manager. 解决方案是使用适当的布局管理器。 In your case you should be using a Card Layout. 在您的情况下,您应该使用卡片布局。 The Card Layout will swap panels making sure only one panel is ever visible at a time. 卡布局将交换面板,以确保一次只能看到一个面板。 Read the Swing tutorial on Using Card Layout . 阅读有关使用卡布局的Swing教程。 Then get rid of all the null layouts and setBounds() methods. 然后摆脱所有的null布局和setBounds()方法。

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

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