Jpanel 添加入jframe,jpanel不显示 5
写了这个一个程序,将jpanel里面做了一个mousemotionlisenter的监听器,当鼠标移动字符串跟着移动,明明已经将Jpanel添加到Jframe了却只能显示...
写了这个一个程序,将jpanel里面做了一个mousemotionlisenter的监听器,当鼠标移动字符串跟着移动, 明明已经将Jpanel添加到Jframe了却只能显示jframe的内容,jpanel完全不显示想问下这是为啥,这个程序哪里有问题吗
public class mouseh extends JFrame{
public void mousein(){
String u=JOptionPane.showInputDialog("qingshuruyuju");
mouse panel=new mouse();
setLayout(new BorderLayout(5,5));
this. add(panel,BorderLayout.CENTER);
}
public static void main(String[] args){
mouseh frame=new mouseh();
//frame.add(panel);
frame.setTitle("shubiaohuodong");
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class mouse extends JPanel{
int x=20;int y=30;String n=null;
public void mouse(){
n=JOptionPane.showInputDialog("请输入数值");
addMouseMotionListener(new MouseAdapter(){
public void mouse(MouseEvent e){
x=e.getX();
y=e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g,String n){
super.paintComponents(g);
g.drawString(n,x,y);
}
}
额显示问题我解决了,不过拖动鼠标依旧动不了,求助 展开
public class mouseh extends JFrame{
public void mousein(){
String u=JOptionPane.showInputDialog("qingshuruyuju");
mouse panel=new mouse();
setLayout(new BorderLayout(5,5));
this. add(panel,BorderLayout.CENTER);
}
public static void main(String[] args){
mouseh frame=new mouseh();
//frame.add(panel);
frame.setTitle("shubiaohuodong");
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class mouse extends JPanel{
int x=20;int y=30;String n=null;
public void mouse(){
n=JOptionPane.showInputDialog("请输入数值");
addMouseMotionListener(new MouseAdapter(){
public void mouse(MouseEvent e){
x=e.getX();
y=e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g,String n){
super.paintComponents(g);
g.drawString(n,x,y);
}
}
额显示问题我解决了,不过拖动鼠标依旧动不了,求助 展开
1个回答
展开全部
已修改,复制粘贴即可
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class mouseh extends JFrame
{
public void mousein ()
{
mouse panel = new mouse ();
setLayout (new BorderLayout ());
add (panel, BorderLayout.CENTER);
}
public static void main ( String[] args )
{
mouseh frame = new mouseh ();
frame.setTitle ("shubiaohuodong");
frame.setSize (500, 500);
frame.setLocationRelativeTo (null);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.mousein ();
frame.setVisible (true);
}
}
class mouse extends JPanel
{
int x = 20;
int y = 30;
String n = null;
public mouse ()
{
n = JOptionPane.showInputDialog ("请输入数值");
addMouseMotionListener (new MouseAdapter ()
{
@Override
public void mouseMoved ( MouseEvent e )
{
x = e.getX ();
y = e.getY ();
repaint ();
}
});
}
@Override
public void paint ( Graphics g )
{
super.paint (g);
g.drawString (n, x, y);
g.dispose ();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询