好的,我编译代码并运行程序,图像应该出现在屏幕中央。我将尺寸放在 JTextFields 中,然后按调整大小按钮。一切正常。现在,如果我关闭窗口并再次运行,图像就会出现,但是如果我第一次按下调整大小按钮,它就会消失,如果我再次按下它,它就会出现并且一切正常。只有当我在窗口运行时编译代码并运行时,我才没有那个小问题。
有人知道发生了什么吗?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class ImageApp extends JFrame implements ActionListener{
//*********************************Variables*********************************
//***************************************************************************
private JButton align_l,align_c,align_r,rsz;
private JLabel w,h,pic;
private JTextField w_txt,h_txt;
private static final String IMG_PATH = "./hamster.jpg";
private static int img_w,img_h;
private static ImageIcon icon;
private JMenuItem reset_option,double_option;
//********************************Constructor********************************
//***************************************************************************
public ImageApp(String title){
super(title);
//****************************Align Panel****************************
align_l = new JButton("Align Left");
align_c = new JButton("Align Center");
align_r = new JButton("Align Right");
align_l.addActionListener(this);
align_c.addActionListener(this);
align_r.addActionListener(this);
JPanel align_panel = new JPanel();
align_panel.setLayout(new GridLayout(1,3));
align_panel.add(align_l);
align_panel.add(align_c);
align_panel.add(align_r);
add("North",align_panel);
//***************************Picture Label***************************
try {
BufferedImage img = ImageIO.read(new File(IMG_PATH));
img_w = img.getWidth();
img_h = img.getHeight();
ImageIcon icon = new ImageIcon(img);
pic = new JLabel(icon);
add(pic);
} catch (IOException e) {
e.printStackTrace();
}
//****************************Resize Panel***************************
w = new JLabel("Width:");
h = new JLabel("Height:");
w_txt = new JTextField(String.valueOf(img_w),4);
h_txt = new JTextField(String.valueOf(img_h),4);
rsz = new JButton("Resize");
rsz.addActionListener(this);
JPanel resize_panel = new JPanel();
resize_panel.setLayout(new GridLayout(3,2));
resize_panel.add(w);
resize_panel.add(w_txt);
resize_panel.add(h);
resize_panel.add(h_txt);
resize_panel.add(rsz);
add("South",resize_panel);
//*************************Menu Options Panel************************
JMenuBar menu_bar = new JMenuBar();
setJMenuBar(menu_bar);
JMenu options_menu = new JMenu("Options");
menu_bar.add(options_menu);
JMenuItem reset_option = new JMenuItem("Reset");
JMenuItem double_option = new JMenuItem("Double");
options_menu.add(reset_option);
options_menu.add(double_option);
reset_option.addActionListener(this);
double_option.addActionListener(this);
//********************************************************************
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
//**********************************Methods**********************************
//***************************************************************************
public void actionPerformed(ActionEvent e){
//Object b = e.getSource();
String b = e.getActionCommand();
if (b.equals("Align Left")){pic.setHorizontalAlignment(SwingConstants.LEFT);}
else if (b.equals("Align Center")){pic.setHorizontalAlignment(SwingConstants.CENTER);}
else if (b.equals("Align Right")){pic.setHorizontalAlignment(SwingConstants.RIGHT);}
else if (b.equals("Resize")){
int wid,hei;
try{
wid=Integer.parseInt(w_txt.getText());
hei=Integer.parseInt(h_txt.getText());
}catch(NumberFormatException nfe){
wid=img_w;
hei=img_h;
}
if(wid == 0 || hei == 0){
wid=img_w;
hei=img_h;
}
if(wid > 4096){
wid=4096;
}
if( hei > 2048){
hei=2048;
}
resize_image(wid,hei);
}
else if (b.equals("Reset")){
resize_image(img_w,img_h);
}
else if (b.equals("Double")){
Icon newIcon = pic.getIcon();
int wid = 2 * newIcon.getIconWidth();
int hei = 2 * newIcon.getIconHeight();
if(wid > 4096){
wid=4096;
}
if( hei > 2048){
hei=2048;
}
resize_image(wid,hei);
}
}
public void resize_image(int w,int h){
Image im = Toolkit.getDefaultToolkit().getImage(IMG_PATH);
BufferedImage b_i = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = b_i.createGraphics();
g.drawImage(im, 0, 0, w, h, null);
ImageIcon newIcon = new ImageIcon(b_i);
pic.setIcon(newIcon);
w_txt.setText(String.valueOf(w));
h_txt.setText(String.valueOf(h));
}
public static void main(String [] args){
ImageApp i = new ImageApp("Image Application.");
i.setSize(800,600);
i.setLocationRelativeTo(null);
i.setVisible(true);
}
}