这里我实现了一个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);
这里有一个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);
在 MainController
中,我一直尝试用我的数据库更新 lblTotalCall
:
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();
}
我已经尝试在 JLabel
中设置默认值,并将属性 focusable
和 enabled
的值更改为 false但这并不能解决问题。
我启动应用程序,(抱歉我无法发布图像)
我看到了正确的数据,但是当我将鼠标指针放在 btnTotalCall
或 btnOperators
上时,标签消失了。
最佳答案
I tried to put a Jlabel on a Jbutton
我看到的是:
contentPane.add(btnTotalCall);
...
contentPane.add(lblTotalCall);
这意味着您要向面板添加两个组件,而不是向按钮添加标签。
but when i put my mouse pointer on the jlabel is disappear
默认情况下,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>");
关于java - 我尝试将 JLabel 放在 JButton 上,但是当我将鼠标指针放在标签上时,它消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55814259/