0

我的文本字段是 main 的本地字段,因此我无法从 actionPerformed 访问它,我相信我需要将其设为实例变量,但我不知道如何做,因为我的框架也在 main 中,所以我不确定我会怎么做将其添加到框架中。

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) {}

    }
}
4

3 回答 3

0

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

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) {
            // ...
        }
    }
}
于 2012-04-14T07:37:48.193 回答
0

创建您的框架并在构造函数中对其进行初始化。

public static void main(String[] args) {
    new Test();
}
于 2012-04-15T13:41:13.173 回答
-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));
    }
}
于 2012-04-14T08:09:39.287 回答