【问题标题】:Im not sure how to get the data from my text field我不确定如何从我的文本字段中获取数据
【发布时间】:2012-04-13 17:29:31
【问题描述】:

我想从我的文本字段中获取数据并将其设置为 int h。并改变了我绘制的矩形的大小,但我不确定如何从文本字段中获取数据,我厌倦了在 actionperfomred 中使用 e.getsource 但它找不到我的文本字段。我的代码如下:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.net.*;
import java.sql.*;
import java.lang.Object;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * This class demonstrates how to load an Image from an external file
 */
public class test extends Component {

    int x=77, y=441, w=23, h=10;

    BufferedImage img =
  new BufferedImage(100, 50,
                    BufferedImage.TYPE_INT_ARGB);    
   // BufferedImage img;

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
           // g.fillRect(10,10,10,10);
    }

    public test() {
       try {
           img = ImageIO.read(new File("sales-goal.png"));
       } catch (IOException e) {}


       Graphics2D g = img.createGraphics();
       Color myColor = Color.decode("#32004b");
       g.setColor(myColor);
       g.fillRect(x,y,w,h);
                //77,441,23,10
    }

    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension(100,100);
        } else {
           //return new Dimension(img.getWidth(null), img.getHeight(null));
            return new Dimension(300,600);
       }
    }

    public static void main(String[] args) {

        JFrame f = new JFrame("Load Image Sample");
        JTextField textField=new JTextField();
        f.add(textField);
        textField.setBounds(10,10,40,30);
        textField.setVisible(true);

        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        f.add(new test());
        f.pack();
        f.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
               // if (e.getSource() == textField) {}

    }
}

【问题讨论】:

  • 这是一个很好的问题:codereview.stackexchange.com
  • 不要将 Swing (JFrame) 与 AWT (Component) 组件混合使用。 test 类应该扩展JPanel(并且应该正确地大写为Test)。它需要覆盖paintComponent() 而不是paint()
  • 已经完成了,我仍然不确定如何通过actionperformed获取main中的数据。

标签: java swing awt review


【解决方案1】:

变量textFieldmain 的本地变量。如果您想从actionPerformed 访问它,您需要将其更改为实例变量。

【讨论】:

  • 我如何使它成为一个实例变量,然后我将如何将它添加到 j 框架中,添加到框架中,因为这也是主要的。我应该移动 main 中的所有内容吗?
【解决方案2】:

是的。我同意@jpm。您需要将其声明为实例变量。 执行以下操作:-

  public class test extends Component {
       //Declare the variable here.
       private static JTextField textfield;

    public static void main(String[] args) {
       //Whenever you use the textfield use like this. Remove the keyword 'JTextField'.
       textfield = new JTextField();
  }
  }

【讨论】:

  • 我这样做并将其添加到我的 actionperformed 但我没有得到数据 public void actionPerformed(ActionEvent e) { Graphics g= getGraphics(); if (e.getSource() == textField) { entry= Integer.parseInt(textField.getText()); g.drawString("测试",50,50);条目=h; }
  • 我听不懂你在说什么。简要说明。
  • 检查是否使用了相同的变量名:private JTextField textField;和 textField = new JTextField();
  • 我像你说的那样声明了它们,但我没有将它们设为私有 JTextField textField; JFrame f=新的 JFrame();使用我在 main 和我的 actionperformed 中使用的相同名称,我将在哪里将 actionlistner 添加到文本字段,我认为这就是为什么当我在 actionperformed 的 texfield 中输入一个 int 时我没有得到任何东西我放 if (e.getSource( ) == textField) { entry=Integer.parseInt(textField.getText());还是它没有重新加载或重新绘制之后
猜你喜欢
  • 2013-10-27
  • 1970-01-01
  • 2021-08-17
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
相关资源
最近更新 更多