gpt4 book ai didi

java - JTextField 不会按预期将值从文本字段传递到对象字段

转载 作者:行者123 更新时间:2023-12-04 08:41:11 24 4
gpt4 key购买 nike

我正在使用 javax.swing 编写 2D 绘图应用程序包裹。这个想法是创建一个 DrawingApplicationFrame (扩展 Jframe 类),其中几个 JButtton , JCheckBox , JTextFields除了 DrawingPanel 之外,还被实例化以改变油漆的状态。实例drawpanel嵌入在此 DrawingApplicationFrame类(class)。我用 JTextField实例 LineWidthTextDashLengthText更改 WidthLength实例的属性 drawpanel .但是,文本字段似乎不接受其中的值并更改 drawpanel.Widthdrawpanel.Length值。如何修复错误以让文本字段传递值?
这是为文本字段创建监听器 LineWidthTextDashLengthText :

    JTextField LineWidthText=new JTextField(2);
JTextField DashLengthText=new JTextField(2);
        LineWidthText.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
drawpanel.Width=Float.parseFloat(LineWidthText.getText());
drawpanel.dashed=new BasicStroke(drawpanel.Width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{drawpanel.Length}, 10);
drawpanel.concrete=new BasicStroke(drawpanel.Width);
}
});

DashLengthText.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
drawpanel.Length=Float.parseFloat(DashLengthText.getText());
drawpanel.dashed=new BasicStroke(drawpanel.Width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{drawpanel.Length}, 10);
}
});
这是 DrawPanel 类的代码:
    public class DrawPanel extends JPanel
{
Color ColorOne=Color.BLACK;
Color ColorTwo=Color.BLACK;
Boolean FilledOrNot=false, UseGradientOrNot=false, DashedOrNot=false, isDragged=false;
float Width=10;
float Length=10;
String ShapeChoice="Line";
ArrayList<MyShapes> ShapeObjects=new ArrayList<MyShapes>();

Paint gradient=new GradientPaint(100, 100, ColorOne, 100+Width*2, 100+Width*2, ColorTwo, true);
Paint mono=new GradientPaint(100, 100, ColorOne, 100+Width*2, 100+Width*2, ColorOne, true);
Stroke dashed=new BasicStroke(Width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{Length}, 10);
Stroke concrete=new BasicStroke(Width);

Point start=new Point();
Point stop=new Point();
Point drag=new Point();

JLabel label=new JLabel("(,)");

public DrawPanel(String caption)
{
this.setBackground(Color.WHITE);
this.setLayout(new BorderLayout());
this.add(label, BorderLayout.SOUTH);
this.label.setVisible(true);
}

@Override
@SuppressWarnings("empty-statement")
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//loop through and draw each shape in the shapes arraylist
addMouseListener(new MouseHandler());
for(int i=0; i<ShapeObjects.size(); i++){
ShapeObjects.get(i).draw(g2d);
}
}

void removeLastElement(){
ShapeObjects.remove(ShapeObjects.size()-1);
repaint();
}

void clearAllElements(){
ShapeObjects.clear();
repaint();
}

public void addShape(MyShapes shape){
ShapeObjects.add(shape);
}

public class MouseHandler extends MouseAdapter implements MouseMotionListener
{


public void mousePressed(MouseEvent event)
{
DrawPanel dp=(DrawPanel) event.getSource();
dp.start=(new Point(event.getX(), event.getY()));
}

public void mouseReleased(MouseEvent event)
{
DrawPanel dp=(DrawPanel) event.getSource();
dp.stop=(new Point(event.getX(), event.getY()));
switch(ShapeChoice){

case "Line":
if(UseGradientOrNot&&DashedOrNot)
dp.addShape(new MyLine(dp.start, dp.stop, dp.gradient, dp.dashed));
else if((UseGradientOrNot==false)&&DashedOrNot)
dp.addShape(new MyLine(dp.start, dp.stop, dp.mono, dp.dashed));
else if(UseGradientOrNot&&(DashedOrNot==false))
dp.addShape(new MyLine(dp.start, dp.stop, dp.gradient, dp.concrete));
else if((UseGradientOrNot==false)&&(DashedOrNot)==false)
dp.addShape(new MyLine(dp.start, dp.stop, dp.mono, dp.concrete));
break;

case "Oval":
if(UseGradientOrNot&&DashedOrNot)
dp.addShape(new MyOval(dp.start, dp.stop, dp.gradient, dp.dashed, dp.FilledOrNot));
else if((UseGradientOrNot==false)&&DashedOrNot)
dp.addShape(new MyOval(dp.start, dp.stop, dp.mono, dp.dashed, dp.FilledOrNot));
else if(UseGradientOrNot&&(DashedOrNot==false))
dp.addShape(new MyOval(dp.start, dp.stop, dp.gradient, dp.concrete, dp.FilledOrNot));
else if((UseGradientOrNot==false)&&(DashedOrNot)==false)
dp.addShape(new MyOval(dp.start, dp.stop, dp.mono, dp.concrete, dp.FilledOrNot));
break;

case "Rectangle":
if(UseGradientOrNot&&DashedOrNot)
dp.addShape(new MyRectangle(dp.start, dp.stop, dp.gradient, dp.dashed, dp.FilledOrNot));
else if((UseGradientOrNot==false)&&DashedOrNot)
dp.addShape(new MyRectangle(dp.start, dp.stop, dp.mono, dp.dashed, dp.FilledOrNot));
else if(UseGradientOrNot&&(DashedOrNot==false))
dp.addShape(new MyRectangle(dp.start, dp.stop, dp.gradient, dp.concrete, dp.FilledOrNot));
else if((UseGradientOrNot==false)&&(DashedOrNot)==false)
dp.addShape(new MyRectangle(dp.start, dp.stop, dp.mono, dp.concrete, dp.FilledOrNot));
break;
}

repaint();
}
}

}

最佳答案

是我自己解决的。有两种方法:一种是从按钮上的文本字段分配取值任务。另一种是简单地按“Enter”键!事实证明,代码工作得很好。仅当将值简单地输入到文本字段而不进行任何进一步操作时,它才起作用!

关于java - JTextField 不会按预期将值从文本字段传递到对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64565458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com