往java里输入坐标值_java.让用户输入x坐标,和y坐标。当用户输入完x坐标(比如200),敲enter,...
展開全部
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){
62616964757a686964616fe4b893e5b19e31333332633039new?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);
}
}
總結(jié)
以上是生活随笔為你收集整理的往java里输入坐标值_java.让用户输入x坐标,和y坐标。当用户输入完x坐标(比如200),敲enter,...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LeetCode 493. 翻转对(归并
- 下一篇: LeetCode 173. 二叉搜索树迭