当鼠标放在一个jLabel上时,jLabel上的字添加下划线并改变颜色,怎么实现,Java的
1个回答
展开全部
package c;
import java.awt.BorderLayout;
public class a extends JFrame {
private JPanel contentPane;
private boolean isOn=false;//判断鼠标是否按过
private JButton button;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
a frame = new a();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public a() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final JLabel label = new JLabel("New label");
label.setBounds(0, 0, 430, 257);
label.setLayout(null);
button = new JButton("New button");
button.setBounds(0, 0, 43, 25);
label.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent arg0) {
if(isOn==true) {button.setLocation(arg0.getX(), arg0.getY());
label.updateUI();
repaint();
}
}
});
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
isOn=isOn==false?true:false;
}
});
label.add(button);
contentPane.add(label);
}
}
这里只实现了一个按钮,不过不管多少个其实都是一样的,lz可以照着样子自己添加,不过注意多一个按钮就要多一个isOn和相关的判断。
如果还不明白的话hi我
import java.awt.BorderLayout;
public class a extends JFrame {
private JPanel contentPane;
private boolean isOn=false;//判断鼠标是否按过
private JButton button;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
a frame = new a();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public a() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final JLabel label = new JLabel("New label");
label.setBounds(0, 0, 430, 257);
label.setLayout(null);
button = new JButton("New button");
button.setBounds(0, 0, 43, 25);
label.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent arg0) {
if(isOn==true) {button.setLocation(arg0.getX(), arg0.getY());
label.updateUI();
repaint();
}
}
});
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
isOn=isOn==false?true:false;
}
});
label.add(button);
contentPane.add(label);
}
}
这里只实现了一个按钮,不过不管多少个其实都是一样的,lz可以照着样子自己添加,不过注意多一个按钮就要多一个isOn和相关的判断。
如果还不明白的话hi我
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询