永发信息网

JFrame对话框显示问题

答案:1  悬赏:0  手机版
解决时间 2021-05-05 10:40
  • 提问者网友:你挡着我发光了
  • 2021-05-04 10:15

import java.awt.Color;
import java.awt.event.MouseAdapter;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class MouseEvent extends JFrame{

JFrame wanttoview=new JFrame("你看不见我");

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
new MouseEvent();
}

public MouseEvent(){
super("鼠标事件");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
this.getContentPane().setBackground(Color.blue);
this.setLocation(200, 200);
this.addMouseListener(new MouseListen());

this.setVisible(true);
JLabel wanttoadd=new JLabel("要添加的...");
wanttoview.setSize(200, 200);
wanttoview.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
wanttoview.getContentPane().setBackground(Color.black);
wanttoview.setLocation(200, 200);
wanttoview.add(wanttoadd);
}

class MouseListen extends MouseAdapter{

@Override
public void mousePressed(java.awt.event.MouseEvent e) {
System.out.println("mousePressed");
wanttoview.setVisible(true);
}

@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
System.out.println("mouseReleased");
wanttoview.setVisible(false);
}

}
}

我修改其中的:

wanttoview.setVisible(true);
// wanttoview.setUndecorated(true);
wanttoview.setTitle("Point Info:");
wanttoview.setSize(230, 200);
wanttoview.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
wanttoview.getContentPane().add(new JTextField("X position is:"+x+" "));
wanttoview.getContentPane().add(new JTextField("Y position is:"+y+" "));
wanttoview.getContentPane().setBackground(Color.white);
wanttoview.getContentPane().getMousePosition();
wanttoview.setLocation(x, y);
发现,只会显示Y的坐标,不会显示X坐标,请为是代码中哪有错误了?

再问,如果我想一行一行逐行显示一些信息,比如:

X= ?

Y=?

z=...

...

我应该用什么代码?

最佳答案
  • 五星知识达人网友:怙棘
  • 2021-05-04 10:45

wanttoview.getContentPane().add(new JTextField("X position is:"+x+" "));
wanttoview.getContentPane().add(new JTextField("Y position is:"+y+" "));



后面一个把前面的覆盖了,改成下面这种


wanttoview.getContentPane().add(new JTextField("X position is:"+x+" ,Y postion is :"+y));


我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯