![](/img/trans.png)
[英]Why is my JLabel (ImageIcon) giving me an error when i try to rotate it?
[英]JOptionPane giving me an error when I try to use it as input
我正在使用Java中的一個類來計算三角形的面積/高度等,但是我很難通過JOptionPane設置坐標的輸入,這是我的嘗試
import javax.swing.*;
import java.awt.*;
public class Triangle
{
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
private int input;
public Triangle(){
int input = JOptionPane.showInputDialog("Enter x1 for point A");
x1 = input;
}
}
這是錯誤Triangle.java:15:錯誤:不兼容的類型:字符串不能轉換為int
showInputDialog(String s)方法返回一個String。 如果要接受一個int,則需要對其進行解析。
方法Integer.parseInt(String s)應該可以幫助您做到這一點。 這是一個例子:
String inputString = JOptionPane.showInputDialog("Enter x1 for point A");
int x1 = Integer.parseInt(inputString);
注意:如果用戶放入除Integer之外的任何內容,則在運行時將收到NumberFormatException。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.