|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求大神们帮忙,本来是将设计的窗口定位在屏幕中间位置,但结果却是错误的,为什么呢? 新手上路,请多关照
代码如下:
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JIAFA {
public static void main(String[] args) {
ASD a = new ASD();
a.setVisible(true);
}
}
class ASD extends Frame {
Panel p = new Panel();
Label l = new Label("+");
TextField t1 = new TextField(6);
TextField t2 = new TextField(6);
TextField t3 = new TextField(6);
Button b = new Button("=");
public ASD() {
setSize(300, 300);
setResizable(false);
Toolkit tk = getToolkit();
Dimension d = tk.getScreenSize();
int screenWidth = d.width;
int screenHeight = d.height;
setLocation((screenHeight - 300) / 2, (screenWidth - 300) / 2);
p.add(t1);
p.add(l);
p.add(t2);
p.add(b);
p.add(t3);
add(p);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
请将 setLocation((screenHeight - 300) / 2, (screenWidth - 300) / 2) 改为如下:
setLocation( (screenWidth - 300) / 2,(screenHeight - 300) / 2)
|
|