簡體   English   中英

JTextField 不會按預期將值從文本字段傳遞到對象字段

[英]JTextField does not pass value from textfield to object field as intended in swing

我正在使用javax.swing包編寫一個 2D 繪圖應用程序。 想法是創建一個DrawingApplicationFrame (擴展Jframe類),其中實例化了幾個JButttonJCheckBoxJTextFields以更改繪制的狀態,此外還有一個DrawingPanel實例drawpanel嵌入在此DrawingApplicationFrame類中。 我使用JTextField實例LineWidthTextDashLengthText來更改實例drawpanelWidthLength屬性。 但是,文本字段似乎不接受其中的值並更改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”鍵! 事實證明,代碼工作得很好。 僅當將值簡單地輸入到文本字段而不進行任何進一步操作時,它才起作用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM