简体   繁体   English

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..

My professor want both variables (Celsius and Fahrenheit) as a double I set declarations double for celsiusdegree; 我的教授希望将两个变量(摄氏温度和华氏温度)都设置为双精度,因此我将摄氏度设置为双精度。 and fahrenheitdegree; 华氏度;

Somehow, compile identified two error on both row of JPanel.setBounds(xxx,xxx, variable, xx); 不知何故,编译在JPanel.setBounds(xxx,xxx,variable,xx)的两行上识别出两个错误; because it is "incompatible types: possible lossy conversion from double to int." 因为它是“不兼容的类型:可能从双精度转换为整数”。

When I tried to change the variable to int. 当我尝试将变量更改为int时。 The error is that the formula for celsiusdegree won't recognize int. 错误是摄氏温度的公式无法识别int。 So how do I make it work for double variables? 那么如何使它适用于双变量?

To make this work for double, you should keep the variables as double and tell the compiler to perform the lossy conversation anyways. 为了使此功能成倍增加,您应该将变量保留为double并告诉编译器无论如何都要执行有损对话。

so: 所以:

celsiusJPanel.setBounds(100, 220, (int) celsiusdegree, 20);

should work just fine. 应该工作正常。

More information can be found in "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 有关更多信息,请参见“为什么int / byte / short / long可以在不进行类型转换的情况下转换为float / double,但反之亦然” ,以及JLS相关部分

JPanel's setBounds sets the top right of the panel to the first and second argument as an x,y coordinate. JPanel的setBounds将面板的右上角设置为第一个和第二个参数,作为x,y坐标。 The third and fourth arguments specify the width and height respectively. 第三个和第四个参数分别指定宽度和高度。 You would need to create a label or text field to display your values. 您将需要创建标签或文本字段以显示您的值。

I would suggest to keep celsiusdegree and fahrenheitdegree in Double and in then use setBounds as folows: 我建议将celsiusdegreefahrenheitdegree保留为Double ,然后将setBounds用作以下内容:

celsiusJPanel.setBounds(100, 220, celsiusdegree.intValue(), 20);

Because you cant cast Double to int . 因为您不能将Double转换为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么-XX:+ ExplicitGCInvokesConcurrent是使用G1收集器时的默认值? - Why isn't -XX:+ExplicitGCInvokesConcurrent the default when using the G1 collector? 为什么它告诉我将时间转换为int? 时间的格式为XX:XX,此变量应为哪种类型? - Why it tells me to convert time to int? the format of time is XX:XX, what this type of this variable should be? 为什么未应用-XX:ReservedCodeCacheSize? - Why is -XX:ReservedCodeCacheSize not getting applied? 为什么 jvm -XX:+EliminateAllocations 失败 - why jvm -XX:+EliminateAllocations fail XX:HeapDumpSegmentSize和XX:SegmentedHeapDumpThreshold的用法 - Usage of XX:HeapDumpSegmentSize and XX:SegmentedHeapDumpThreshold jvm options -XX:+SafepointTimeout -XX:SafepointTimeoutDelay 看起来不起作用 - jvm options -XX:+SafepointTimeout -XX:SafepointTimeoutDelay look don't work Java垃圾收集优化--XX:GCTimeLimit和-XX:GCHeapFreeLimit无法正常工作? - Java Garbage Collection Tuning - -XX:GCTimeLimit and -XX:GCHeapFreeLimit not working? PermGen的标志未按预期运行:-XX:+ CMSClassUnloadingEnabled和-XX:+ CMSPermGenSweepingEnabled - Flags for PermGen not working as expected: -XX:+CMSClassUnloadingEnabled and -XX:+CMSPermGenSweepingEnabled 得到明天的时间xx:xx - Get the time of tomorrow xx:xx -XX:MaxPermSize带有或不带有-XX:PermSize - -XX:MaxPermSize with or without -XX:PermSize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM