【发布时间】:2018-09-04 09:20:52
【问题描述】:
我需要访问 Surface 类中的两个变量(screenWidth 和 screenHeight)。这就是我目前实现的方式:
public class WheelOfFortune extends JFrame {
public WheelOfFortune() {
initUI();
}
private void initUI() {
Surface newSurface = new Surface();
add(newSurface);
setSize(newSurface.screenWidth, newSurface.screenHeight);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
WheelOfFortune ex = new WheelOfFortune();
ex.setVisible(true);
ex.setResizable(false);
});
}
}
是否可以通过使用此代码来做同样的事情?怎么样?
public class WheelOfFortune extends JFrame {
public WheelOfFortune() {
initUI();
}
private void initUI() {
add(new Surface());
setTitle("The Wheel of Fortune");
setSize(???.screenWidth, ???.screenHeight);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
WheelOfFortune ex = new WheelOfFortune();
ex.setVisible(true);
ex.setResizable(false);
});
}
}
【问题讨论】:
-
一个问题:为什么?
-
更少的代码行数、好奇心和正确做事的意愿。