我该如何刷新窗口

时间:2017-05-20 10:51:48

标签: java jframe

我正在制作简单的图形编辑器,并且在开始时遇到问题。

我正在做双窗口程序,我想在一个窗口中单击按钮,选择图像的路径,然后在第二个窗口上显示该图像。我几乎做了所有的事情,但它看起来像是:我启动程序没有出现在第二个窗口,我读取图像,它没有出现在第二个窗口,看起来我需要的东西刷新第二个窗口后我读取图像,但我不知道该怎么做,请帮忙。

我知道有repaint()方法,但我不知道如何在一个窗口中使用重绘并强制它重新绘制第二个

第一档:

package edytor;

import java.awt.*;
import javax.swing.*;

public class Edytor extends JFrame {
    public static boolean pom, pom1;
    public static Image  obraz; 
    public static String sciezka;

    public Edytor() {
        super("Edytor 2D");
        setBounds(420,50,800,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container con = this.getContentPane();
        CObrazek obraz = new CObrazek();
        con.add(obraz);
        przybornik przyb = new przybornik();
        pom = false;
        pom1=false;
        Edytor.obraz = new ImageIcon(Edytor.sciezka).getImage();
        setVisible(true);
    }

    public static void main(String args[]) {
        new Edytor();
    }
}

class CObrazek extends Canvas {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(Edytor.obraz, 0,0,null);
        g2d.drawString("Wsp x: " , 300, 400);
    }
}

第二档:

package edytor;

import java.io.*;
import javax.swing.*;

public class przybornik extends javax.swing.JFrame {

    public przybornik() {
        initComponents();
        setVisible(true);
    }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  private void initComponents() {

    read_but = new javax.swing.JButton();
    save_but = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    read_but.setText("Wczytaj");
    read_but.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            read_butActionPerformed(evt);
        }
    });

    save_but.setText("Zapisz");
    save_but.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            save_butActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new 
     javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(

     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(read_but, javax.swing.GroupLayout.PREFERRED_SIZE, 
     184, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(save_but, javax.swing.GroupLayout.DEFAULT_SIZE, 
     178, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(

      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(read_but)
                .addComponent(save_but))
            .addContainerGap(266, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>                        

     private void read_butActionPerformed(java.awt.event.ActionEvent evt) {                                         
         JFileChooser fileChooser = new JFileChooser();

         fileChooser.setCurrentDirectory(new 
         File(System.getProperty("user.home")));
         int path = fileChooser.showOpenDialog(this);
         if (path == JFileChooser.APPROVE_OPTION) {
             File selectedFile = fileChooser.getSelectedFile();
             System.out.println("Selected file: " + 
             selectedFile.getAbsolutePath());
             Edytor.sciezka = selectedFile.getAbsolutePath();
         }

         System.out.print(Edytor.sciezka);
     }                                        

         private void save_butActionPerformed(java.awt.event.ActionEvent evt) {                                         
      // TODO add your handling code here:
      }                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new przybornik().setVisible(true);

        }
    });
}  

// Variables declaration - do not modify                     
private javax.swing.JButton read_but;
private javax.swing.JButton save_but;
// End of variables declaration                   
}

1 个答案:

答案 0 :(得分:0)

我只是告诉你我改变和添加的地方。 修改

public class Edytor extends JFrame{
      CObrazek obraz = new CObrazek();//here (new place)
      public static boolean pom, pom1;
        public static Image  image; 
        public static String sciezka;
        public void EdytorShow(String image_path) {//changed
            setBounds(420,50,800,600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container con = this.getContentPane();
            //CObrazek obraz = new CObrazek();//disabled
            przybornik przyb = new przybornik();//here (new place)
            /**
             * We do not want the window to appear without selecting an image.
             * For this reason we will use  "if"
             */
            if(!image_path.equals("null")){
               con.add(obraz);
           // przybornik przyb = new przybornik();//Moved out of if ;)
            pom = false;
            image = new ImageIcon(image_path).getImage();
            setVisible(true);
            }//if
        }

        public static void main(String args[]) {
           Edytor et = new Edytor();
           et.EdytorShow("null");
        }


}

在&#34; przybornik.java&#34;文件:

    private static boolean isWindowActive = false; //for window active.
public przybornik() {
    initComponents();
    if(isWindowActive == false){ //window active check here.
     setVisible(true);
     isWindowActive = true;
    }
}

// --------和:

     Edytor edytor = new Edytor();//here (new place)
 private void read_butActionPerformed(java.awt.event.ActionEvent evt) {                                         
     JFileChooser fileChooser = new JFileChooser();

     fileChooser.setCurrentDirectory(new 
     File(System.getProperty("user.home")));
     int path = fileChooser.showOpenDialog(this);
     if (path == JFileChooser.APPROVE_OPTION) {
         File selectedFile = fileChooser.getSelectedFile();
         System.out.println("Selected file: " + 
         selectedFile.getAbsolutePath());
         //Edytor.sciezka = selectedFile.getAbsolutePath();
         //new Edytor().EdytorShow(selectedFile.getAbsolutePath());//disabled(Moved out of function ;)
         edytor.EdytorShow(selectedFile.getAbsolutePath());//newly added
     }

     System.out.print(Edytor.sciezka);
 }