java se jlabel边框问题
我给jlabel的鼠标进入事件设置了一个borderfactry.creatloweredbevelborder,我想在鼠标出来时设成默认的边框...
我给jlabel的鼠标进入事件设置了一个borderfactry.creatloweredbevelborder,我想在鼠标出来时设成默认的边框
展开
3个回答
展开全部
你看一下,下边的代子可以不?
--------------------------------------------------------------------
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class JLabelDemo extends JFrame implements MouseListener {
private JLabel lblNewLabel = null;
public JLabelDemo() {
getContentPane().setLayout(null);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(80, 80, 166, 86);
lblNewLabel.setBorder(BorderFactory.createLineBorder(Color.RED));
lblNewLabel.addMouseListener(this);
getContentPane().add(lblNewLabel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 380);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new JLabelDemo();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
Border border = BorderFactory.createLineBorder(Color.BLUE);
lblNewLabel
.setBorder(BorderFactory.createTitledBorder(border, "hello"));
}
public void mouseExited(MouseEvent e) {
lblNewLabel.setBorder(BorderFactory.createLineBorder(Color.RED));
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
--------------------------------------------------------------------
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class JLabelDemo extends JFrame implements MouseListener {
private JLabel lblNewLabel = null;
public JLabelDemo() {
getContentPane().setLayout(null);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(80, 80, 166, 86);
lblNewLabel.setBorder(BorderFactory.createLineBorder(Color.RED));
lblNewLabel.addMouseListener(this);
getContentPane().add(lblNewLabel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 380);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new JLabelDemo();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
Border border = BorderFactory.createLineBorder(Color.BLUE);
lblNewLabel
.setBorder(BorderFactory.createTitledBorder(border, "hello"));
}
public void mouseExited(MouseEvent e) {
lblNewLabel.setBorder(BorderFactory.createLineBorder(Color.RED));
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在设置border的之前,先用个Border把jlabel.getBorder把默认的border存起,然后添加个MouseListener事件监听,在mouseExited的时候在把你的jlabel.setBorder(你之前存起来的border);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议不要这么做,可以替换成一个变色的边框。设置边框会可能会导致整个布局变化。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询