不将 TextField 添加到 jFrame [英] Not adding TextField to jFrame

查看:29
本文介绍了不将 TextField 添加到 jFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本字段没有添加到我的 Jframe 中,我想让这个文本字段可用,以便我可以使用它来更改在油漆中绘制的矩形的高度.在 actionperformred 中,我试图从该字段中获取值,并希望它能用正确的值重新绘制图像

My text Field is not being added to my Jframe, I want to have this textfield available so that I can use it to change the height of a rectangle im drawing in paint. In actionperfomred im trying to get the value from that field and hopefully it will repaint the image with the correct value

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;


public class Test extends JPanel implements ActionListener{

    JTextField textField;
    JFrame f=new JFrame();
    int x=77, y=441, w=23, h=10, entry;
    BufferedImage img=null;

    public void init(){
        JTextField textField=new JTextField(); 
        f.add(textField);
        textField.setBounds(10,10,40,30);
        textField.setVisible(true);
        textField.addActionListener(this);
    }

   // BufferedImage img;

   public static void main(String[] args) {
        BufferedImage img =new BufferedImage(100, 50,BufferedImage.TYPE_INT_ARGB); 
        //textField = new JTextField();
        JFrame f = new JFrame("Load Image Sample");

       /*textField=new JTextField();
        textField.addActionListener(this);
        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 paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);

        Graphics2D i = img.createGraphics();
       Color myColor = Color.decode("#32004b");
       i.setColor(myColor);
       i.fillRect(x,y,w,h);

           // g.fillRect(10,10,10,10);
    }

    public Test() {

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



                //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 void actionPerformed(ActionEvent e) {

        Graphics g= getGraphics();


               if (e.getSource() == textField) {
                   entry= Integer.parseInt(textField.getText());
                   g.drawString("Test",50,50);

                   entry=h;

                }

    }
}

推荐答案

我猜是因为你从不调用方法 init

I would suppose it is because you never call the method init

这篇关于不将 TextField 添加到 jFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆