JPanel.setBounds(xx,xx,variable,xx) 不工作。为什么?
JPanel.setBounds(xx,xx,variable,xx) isn't working. why?
public void colortemperatureJSliderStateChanged(ChangeEvent event) {
fahrenheitdegree = colortemperatureJSlider.getValue();
fahrenheitJPanel.setBounds(100,270, fahrenheitdegree, 20);
celsiusdegree = (fahrenheitdegree - 32.0)*5.0/9.0;
celsiusJPanel.setBounds(100,220,celsiusdegree, 20);
}// end of public void colortemperatureJSliderStateChanged..
我的教授想要两个变量(摄氏度和华氏度)作为双精度变量
我为 celsiusdegree 设置了 double 声明;和华氏度;
不知何故,编译在 JPanel.setBounds(xxx,xxx, variable, xx) 的两行上发现了两个错误;因为它是 "incompatible types: possible lossy conversion from double to int."
当我尝试将变量更改为 int 时。错误是 celsiusdegree 的公式无法识别 int。那么如何让它适用于双变量呢?
要使其适用于 double,您应该将变量保持为 double,并告诉编译器无论如何都要执行有损对话。
所以:
celsiusJPanel.setBounds(100, 220, (int) celsiusdegree, 20);
应该可以正常工作。
可以在 "Why can int/byte/short/long be converted to float/double without typecasting but vice-versa not possible", as well as the relevant section of the JLS
中找到更多信息
JPanel 的 setBounds 将面板的右上角设置为第一个和第二个参数作为 x,y 坐标。第三个和第四个参数分别指定宽度和高度。您需要创建一个标签或文本字段来显示您的值。
我建议将 celsiusdegree
和 fahrenheitdegree
保留在 Double
中,然后按以下方式使用 setBounds:
celsiusJPanel.setBounds(100, 220, celsiusdegree.intValue(), 20);
因为你不能 cast Double to int。
public void colortemperatureJSliderStateChanged(ChangeEvent event) {
fahrenheitdegree = colortemperatureJSlider.getValue();
fahrenheitJPanel.setBounds(100,270, fahrenheitdegree, 20);
celsiusdegree = (fahrenheitdegree - 32.0)*5.0/9.0;
celsiusJPanel.setBounds(100,220,celsiusdegree, 20);
}// end of public void colortemperatureJSliderStateChanged..
我的教授想要两个变量(摄氏度和华氏度)作为双精度变量 我为 celsiusdegree 设置了 double 声明;和华氏度;
不知何故,编译在 JPanel.setBounds(xxx,xxx, variable, xx) 的两行上发现了两个错误;因为它是 "incompatible types: possible lossy conversion from double to int."
当我尝试将变量更改为 int 时。错误是 celsiusdegree 的公式无法识别 int。那么如何让它适用于双变量呢?
要使其适用于 double,您应该将变量保持为 double,并告诉编译器无论如何都要执行有损对话。
所以:
celsiusJPanel.setBounds(100, 220, (int) celsiusdegree, 20);
应该可以正常工作。
可以在 "Why can int/byte/short/long be converted to float/double without typecasting but vice-versa not possible", as well as the relevant section of the JLS
中找到更多信息JPanel 的 setBounds 将面板的右上角设置为第一个和第二个参数作为 x,y 坐标。第三个和第四个参数分别指定宽度和高度。您需要创建一个标签或文本字段来显示您的值。
我建议将 celsiusdegree
和 fahrenheitdegree
保留在 Double
中,然后按以下方式使用 setBounds:
celsiusJPanel.setBounds(100, 220, celsiusdegree.intValue(), 20);
因为你不能 cast Double to int。