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);
}
}
。
全部