我不知道如何从我的文本字段中获取数据

时间:2012-04-13 18:44:59

标签: java swing

我的文本字段是main的本地字段,所以我无法从actionPerformed访问它,我相信我需要使它成为一个实例变量,但我不知道如何因为我的框架也是主要因此我不知道如何然后我会将它添加到框架中。

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 JPanel {

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

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

    public void paintComponent(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) {}

    }
}

3 个答案:

答案 0 :(得分:0)

我认为你不应该这样设置你的挥杆应用程序。您应该创建JFrame的子类并在其构造函数(或类似的东西)中进行初始化,而不是在main方法中创建所有内容。然后,该类可以保存对其包含的组件的引用。例如:

public class TestFrame extends JFrame {

    private JTextField textField;

    public TestFrame() {
        super("Load Image Sample");
        textField = new JTextField();
        this.add(textField);
        textField.setBounds(10,10,40,30);
        textField.setVisible(true);

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

        this.add(new Test());
        this.pack();
        this.setVisible(true);
    }

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

答案 1 :(得分:0)

创建框架并在构造函数中初始化它。

public static void main(String[] args) {
    new Test();
}

答案 2 :(得分:-1)

事件处理方法actionPerformed必须由ActionListener接口注册。由于textField和Test都是JFrame的组件,因此逻辑位置可以是JFrame,也可以是将所有内容连接在一起的单独控制器类。

我在一些细节上用更传统的方法重写了你的代码。为此,我引入了一个新类MyFrame。

public class MyFrame extends JFrame implements ActionListener {

    JTextField textField = new JTextField();

    public MyFrame(String title) {
        super(title); // Or constructor without parameters and:
        setTitle("Load Image Sample");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setLayout(...); for instance with null for absolute positioning

        add(textField);
        textField.setBounds(10, 10, 40, 30);
        textField.setVisible(true);

        Test panel = new Test();
        add(panel);

        pack();
    }

    public static void main(String[] args) {
        JFrame f = new MyFrame("Load Image Sample");
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Text: " + textField.getText());
    }
}

然后你的JPanel

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;


/**
* This class demonstrates how to load an Image from an external file
*/
class Test extends JPanel {

    int x = 77, y = 441, w = 23, h = 10;
    BufferedImage img;
    Color myColor = Color.decode("#32004b");

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(myColor);
        g.fillRect(x, y, w, h);

        g.drawImage(img, 0, 0, null);
        // g.fillRect(10,10,10,10);
    }

    public Test() {
        setBackground(Color.red);
        try {
            img = ImageIO.read(new File("sales-goal.png"));
        } catch (IOException e) {
            img = new BufferedImage(100, 50, BufferedImage.TYPE_INT_ARGB);
        }
        setPreferredSize(new Dimension(300, 600));
    }
}