如何解决parseInt 没有从 jTextField
我一直在创建一个尝试根据用户输入绘制形状的 GUI。但是我的 parseInt 函数似乎无法从 JTextField 检索输入并使用它。 因为我不确定错误来自哪里,因为我的编译器没有显示错误,所以只构建成功。我想我只是把我所有的东西都以粘贴箱的形式放在这里。
https://pastebin.com/5sH8xGZm(JFrame 表单) https://pastebin.com/d9a41EWd(四边形,工作正常) https://pastebin.com/qnQ6b3gD(矩形继承自四边形。不绘制任何内容) https://pastebin.com/NwJU1pCN(Square 继承自 Rectangle。不绘制任何内容)
为了具体展示我在 parseInt 中的尝试,这里是具体部分
private boolean fillRecSquare()
{
//Retrieve values from text boxes
String strWidth = txtWidth.getText();
String strHeight = txtHeight.getText();
if(strWidth.equals("") || strWidth.isEmpty())
{
JOptionPane.showMessageDialog(null,"Please fill Width");
return false;
}
if(strHeight.equals("") || strHeight.isEmpty())
{
JOptionPane.showMessageDialog(null,"Please fill Height");
return false;
}
int x = Integer.parseInt(strWidth);
int y = Integer.parseInt(strHeight);
r1.setWidthHeight(x,y);
return true;
}
private void btnDrawActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
// Graphics g = this.getGraphics();
String cmbText = cmbQuad.getItemAt(cmbQuad.getSelectedIndex());
clear();
//DO NOT EDIT THE LINES ABOVE
// Draw the appropriate polygon depending on the value of cmbText
Graphics g = this.getGraphics();
if(cmbText.equals("Quadrilateral"))
{
Quadrilateral quad = new Quadrilateral();
//g.drawLine(quad.getXPoint1(),quad.getYPoint1(),quad.getXPoint1(),quad.getYPoint2());
quad.draw(g);
}
if(cmbText.equals("Rectangle"))
{
if(!fillRecSquare())
{
Rectangle rec = new Rectangle();
String strWidth = txtWidth.getText();
String strHeight = txtHeight.getText();
int x = Integer.parseInt(strWidth);
int y = Integer.parseInt(strHeight);
rec.setWidthHeight(x,y);
rec.draw(g);
}
}
if(!fillRecSquare())
{
if(cmbText.equals("Square"))
{
Square square = new Square(100);
square.draw(g);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。