I tried to put a JLabel on a JButton but when I put my mouse pointer on the label it disappears

时间:2019-04-23 15:09:13

标签: java swing jlabel

Here I implement a JButton:

btnTotalCall = new JButton("Total calls");
        btnTotalCall.setFocusPainted(false);
        btnTotalCall.setVerticalAlignment(SwingConstants.TOP);
        btnTotalCall.setForeground(Color.BLACK);
        btnTotalCall.setBackground(Color.WHITE);
        btnTotalCall.setBounds(302, 39, 125, 125);
        contentPane.add(btnTotalCall);

Here a JLabel:

lblTotalCall = new JLabel("");
        lblTotalCall.setHorizontalAlignment(SwingConstants.CENTER);
        lblTotalCall.setBackground(Color.WHITE);
        lblTotalCall.setForeground(Color.BLACK);
        lblTotalCall.setFont(new Font("Tahoma", Font.BOLD, 20));
        lblTotalCall.setBounds(302, 39, 125, 125);
        contentPane.add(lblTotalCall);

Here in a MainController I tried to update lblTotalCall with my database all the seconds:

public MainController(MainView mainView)
    {
        this.mainView = mainView;

        ActionListener timerListener = new ActionListener()
        {
            DAO<Call> call_dao = new CallDAO();
            List<Call> call_list;
            int call_time;
            DAO<User> user_dao = new UserDAO();
            List<User> user_list;

            public void actionPerformed(ActionEvent e)
            {
                // Put the current date and time in the label date and update it
                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                DateFormat heureFormat = new SimpleDateFormat("HH:mm:ss");
                Date cal = Calendar.getInstance().getTime();
                mainView.getLblDate().setText("<html>" + dateFormat.format(cal).toString() + "<br>" +
                                heureFormat.format(cal) + "</html>");

                call_list = call_dao.recupAll();

                        // Update the lblTotalCall
                if(call_list.size() > 0)
                {
        mainView.getLblTotalCall().setText(Integer.toString(call_list.size()));
                }
                else
                {
                    mainView.getLblTotalCall().setText("0");
                }


                for (Call call : call_list) 
                {
                    if (call.getDate() == cal && mainView.getRdbtnToday().isSelected())
                    {
                        call_time = call_time + call.getDuration();
                        call_time = call_time / call_list.size();
                    }
                }

                user_list = user_dao.recupAll();

                if(user_list.size() > 0)
                {
                    mainView.getLblOperators().setText(Integer.toString(user_list.size()));
                }
                else
                {
                    mainView.getLblOperators().setText("0");
                }
            }
        };

        Timer timer = new Timer(1000, timerListener);
        // to make sure it doesn't wait one second at the start
        timer.setInitialDelay(0);
        timer.start();
    }

I've already tried to put a default value in the JLabel, and change the value of the properties focusable and enabled to false but that's not fix it.

I launch the application, (sorry I can't post images)

enter image description here

I see the right data but when I put my mouse pointer on the btnTotalCall or btnOperators the labels disapear.

enter image description here

1 个答案:

答案 0 :(得分:1)

  

我试图在Jbutton上放置一个Jlabel

我所看到的是:

contentPane.add(btnTotalCall);
...
contentPane.add(lblTotalCall);

这意味着您要在面板上添加两个组件,而不是在按钮上添加标签。

  

但是当我将鼠标指针放在jlabel上时消失了

默认情况下,Swing假定组件不重叠,并且当您将鼠标移到按钮上时,需要重新绘制按钮以在按钮效果上绘制鼠标,因此整个按钮都被重新绘制,并且您将不再看到标签。

如果要在同一位置绘制两个组件,则需要重写面板的isOptimizedDrawingEnabled()方法以返回false以确保两个组件都被重新绘制。

请参阅:Overlap Layout,了解一种布局管理器,一旦您覆盖上述方法,即可将多个组件堆叠在一起。

或更简单的方法是将标签添加到按钮:

JButton button = new JButton("Total Calls");
button.setVerticalAlignment(SwingConstants.TOP);
button.setLayout( new BorderLayout() );

JLabel center = new JLabel("0");
center.setHorizontalAlignment(SwingConstants.CENTER);
button.add(center, BorderLayout.CENTER);

这不是真正的布局管理器,因为按钮的大小仅取决于按钮的属性,而不取决于添加到按钮的标签。

另一个选择是将HTML用作按钮的文本:

JButton button2 = new JButton("<html>Total Calls<br><br><center>0</center></html>");