java 鼠标事件问题 15
我想实现鼠标移入和移出时显示不同的文字,为什么时间实现不了?importjava.awt.BorderLayout;importjava.awt.Graphics;imp...
我想实现鼠标移入和移出时显示不同的文字,为什么时间实现不了?
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class Messagejiaoti {
public static void main(String[] args) {
TestPaint2 frame=new TestPaint2();
frame.setTitle("JAVA计算器");
//frame.setResizable(false);
frame.setSize(300,350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TestPaint2 extends JFrame
{
Newpanel p=new Newpanel();
static Graphics g;
String s1="w";
String s2="nimei";
public TestPaint2()
{
add(p,BorderLayout.CENTER);
p.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
p.repaint();
g.drawString(s2, 120, 150);
}
});
}
class Newpanel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
TestPaint2.g = g;
g.drawString(s1, 120, 150);
}
} 展开
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class Messagejiaoti {
public static void main(String[] args) {
TestPaint2 frame=new TestPaint2();
frame.setTitle("JAVA计算器");
//frame.setResizable(false);
frame.setSize(300,350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TestPaint2 extends JFrame
{
Newpanel p=new Newpanel();
static Graphics g;
String s1="w";
String s2="nimei";
public TestPaint2()
{
add(p,BorderLayout.CENTER);
p.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
p.repaint();
g.drawString(s2, 120, 150);
}
});
}
class Newpanel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
TestPaint2.g = g;
g.drawString(s1, 120, 150);
}
} 展开
1个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Test {
private JFrame frame;
private JLabel label1;
private boolean flag=true;
public Test()
{
frame=new JFrame("标签测试");
label1=new JLabel("变红",JLabel.CENTER);
label1.setOpaque(true);
label1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(flag)
{
label1.setBackground(Color.red);
flag=false;
}
else
{
label1.setBackground(Color.white);
flag=true;
}
}
}
);
frame.getContentPane().add(label1,BorderLayout.CENTER);
frame.setSize(300,300);
frame.setLocation(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test t=new Test();
}
}
请采纳。
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Test {
private JFrame frame;
private JLabel label1;
private boolean flag=true;
public Test()
{
frame=new JFrame("标签测试");
label1=new JLabel("变红",JLabel.CENTER);
label1.setOpaque(true);
label1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(flag)
{
label1.setBackground(Color.red);
flag=false;
}
else
{
label1.setBackground(Color.white);
flag=true;
}
}
}
);
frame.getContentPane().add(label1,BorderLayout.CENTER);
frame.setSize(300,300);
frame.setLocation(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test t=new Test();
}
}
请采纳。
追问
我不想用标签的,标签我也会,我想用画笔
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询