嗨,我正在尝试制作一个简单的框架来显示一个文本字段和一个按钮的图像,但由于某种原因,文本字段是不可见的,因为我对 java 非常陌生,甚至更擅长这些图形的东西,你能帮忙吗:) 代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package solverapplet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.swing.JTextField;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
public class AwtImage extends Frame{
Image img;
String base="R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==";
/*public static void main(String[] args){
AwtImage ai = new AwtImage();
}*/
public void setbase(String a){
this.base=a;
}
public void refreshimage(){
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(this.base);
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imgBytes));
//File imgOutFile = new File("newLabel.png");
//ImageIO.write(bufImg, "png", imgOutFile);
img = bufImg;
} catch (IOException ex) {
Logger.getLogger(AwtImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
public AwtImage(){
super("Solve");
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(this.base);
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imgBytes));
MediaTracker mt = new MediaTracker(this);
img=bufImg;
mt.addImage(img,0);
JTextField textfield= new JTextField("Text field 2", 8);
add(textfield,"South");
setSize(400,400);
//pack();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
dispose();
}
});
} catch (IOException ex) {
Logger.getLogger(AwtImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(img != null)
g.drawImage(img, 100, 100, this);
else
g.clearRect(0, 0, getSize().width, getSize().height);
}
}
它被另一个类实例化。