往java里输入坐标值_怎么样写敲完enter以后?

这篇博客展示了如何在Java中创建一个窗口,并允许用户输入新的坐标值。当用户输入坐标并按下回车键后,窗口将移动到指定位置。程序通过监听键盘事件来获取输入的坐标值,并更新窗口的位置。
摘要由CSDN通过智能技术生成

import java。awt。event。ActionEvent;

import java。awt。event。ActionListener;

import javax。swing。JButton;

import javax。

swing。JFrame;

import javax。swing。JLabel;

import javax。swing。JTextField;

import javax。swing。SwingConstants;

public class SetLocationFrame extends JFrame{

private JTextField t1,t2;

JLabel l;

int x,y;

public static void main(String[]args){

new SetLocationFrame();

}

public SetLocationFrame(){

super("Where should I be?");

this。

setLayout(null);

this。setDefaultCloseOperation(3);

init();

this。setVisible(true);

}

private void init() {

setBounds(100, 100, 300, 200);

x = this。

getLocation()。x;

y = this。getLocation()。y;

JLabel lblNewLabel = new JLabel("Enter new X here");

lblNewLabel。setBounds(44, 10, 136, 20);

add(lblNewLabel);

JLabel lblEnterNewY = new JLabel("Enter new Y here");

lblEnterNewY。

setBounds(44, 40, 136, 20);

add(lblEnterNewY);

t1 = new JTextField(x+"");

t1。setBounds(190, 10, 66, 21);

add(t1);

t1。

setColumns(10);

t1。addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

x = Integer。

parseInt(t1。getText());

l。setText("x is "+ x +" and y is "+y);

t2。requestFocus();

}

});

t2 = new JTextField(y+"");

t2。

setColumns(10);

t2。setBounds(190, 40, 66, 21);

add(t2);

t2。addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

y = Integer。

parseInt(t2。getText());

l。setText("x is "+ x +" and y is "+y);

}

});

JButton b = new JButton("Move The Window");

b。

addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

setLocation(x,y);

System。

out。println(x +":" + y);

}

});

b。setBounds(54, 70, 197, 37);

add(b);

JLabel lblComponentOfCurrent = new JLabel("Component of Current Location:");

lblComponentOfCurrent。

setBounds(44, 114, 197, 20);

add(lblComponentOfCurrent);

l = new JLabel("x is "+ x +" and y is "+y);

l。setHorizontalAlignment(SwingConstants。

CENTER);

l。setBounds(64, 144, 156, 20);

add(l);

}

}

全部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值