0

我正在尝试构建一个应用程序,其中允许JDialogJDialog. 我试过了,但是在A调用对话框B的时候,B对话框出现然后马上消失。这些问题有解决办法吗?

我的代码:

我有一个扩展的 A 类javax.swing.JDialog。然后有一个标签,它具有从类中调用对话框的操作,PadAndZoom如下所示:

 private void lblSuratRekomendasiMouseClicked(java.awt.event.MouseEvent evt) {
    try {
        PadAndZoom app = new PadAndZoom();

        BufferedImage img = ImageIO.read(PadAndZoom.class.getResource("/tampilan/background5.jpg"));

        app.tes(img);
    } catch (IOException ex) {
        Logger.getLogger(RincianBukuBesar.class.getName()).log(Level.SEVERE, null, ex);
    }
} 

然后我还有一个类要由 A 类的方法lblSuratRekomendasiMouseClicked( java.awt.event.MouseEvent evt)调用

package dispertasih;

import java.awt.*;  
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;  
import java.awt.image.BufferedImage;  
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;  
import javax.swing.event.*;  


public class PadAndZoom implements ChangeListener {  
    BufferedImage image;  
    PanPanelX label;  


    public void stateChanged(ChangeEvent e) {  
        int value = ((JSlider) e.getSource()).getValue();
        double scale = value / 200.0;
        BufferedImage scaled = getScaledImage(scale);
        label.setImage(scaled);
    }  


    public BufferedImage getScaledImage(double scale) {  
        int w = (int)(scale*image.getWidth());  
        int h = (int)(scale*image.getHeight());  
        BufferedImage bi = new BufferedImage(w, h, image.getType());  
        Graphics2D g2 = bi.createGraphics();  
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
                            RenderingHints.VALUE_INTERPOLATION_BICUBIC);  
        AffineTransform at = AffineTransform.getScaleInstance(scale, scale);  
        g2.drawRenderedImage(image, at);  
        g2.dispose();  
        return bi;  
    }  

    public PanPanelX getContent() {  
       // createAnImage();  
        label = new PanPanelX(image);  
        //label.setHorizontalAlignment(JLabel.CENTER);  
        return label;  
    }  

    public void createAnImage(BufferedImage imgbuf) {  
        int w = 500;  
        int h = 500;  
        int type = BufferedImage.TYPE_INT_RGB; // many options  

            image = imgbuf;


    }  

    public JSlider getControl() {  
        JSlider slider = new JSlider(JSlider.HORIZONTAL, 50, 200, 50);  
        slider.setMajorTickSpacing(50);  
        slider.setMinorTickSpacing(10);  
        slider.setPaintTicks(true);  
        slider.setPaintLabels(true);  
        slider.addChangeListener(this);  
        return slider;          
    }  

    public void tes(BufferedImage img) {  
        PadAndZoom app = new PadAndZoom();  
        JDialog f = new JDialog();  
        JButton b = new JButton();
          try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { }


             app.createAnImage(img);


        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
        f.getContentPane().add(new JScrollPane(app.getContent()));  

        //f.getContentPane().add(new JButton("tessss"));
        f.getContentPane().add(app.getControl(), "Last");  
        f.setSize(400, 400);  
        f.setLocation(200,200);  
        f.setVisible(true);  
    }  
}  
class PanPanelX extends JPanel {

public int x, y;
public int width = 800, height = 800;
BufferedImage img;
public final static RenderingHints textRenderHints = new      RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
public final static RenderingHints imageRenderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
public final static RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
static int startX, startY;
 public void setImage(BufferedImage img) {
        this.img = img;
        revalidate();
        repaint();
    }
public PanPanelX(BufferedImage img) {
    x = 20;
    y = 20;
    this.img = img;

    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent me) {
            super.mousePressed(me);
            startX = me.getX();
            startY = me.getY();
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent me) {
            super.mouseDragged(me);

            if (me.getX() < startX) {//moving image to right
                x -= 2;
            } else if (me.getX() > startX) {//moving image to left
                x += 2;
            }

            if (me.getY() < startY) {//moving image up
                y -= 2;
            } else if (me.getY() > startY) {//moving image to down
                y += 2;
            }
            repaint();
        }
    });
}

@Override
protected void paintComponent(Graphics grphcs) {
    super.paintComponent(grphcs);
    Graphics2D g2d = (Graphics2D) grphcs;

    //turn on some nice effects
    applyRenderHints(g2d);

    g2d.drawImage(img, x, y, null);
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(width, height);
}

public static void applyRenderHints(Graphics2D g2d) {
    g2d.setRenderingHints(textRenderHints);
    g2d.setRenderingHints(imageRenderHints);
    g2d.setRenderingHints(renderHints);
}
}

谢谢你 ..

4

0 回答 0