我正在做簡單的圖形編輯器,並在開始一個問題。我怎樣才能刷新窗口
我正在做雙窗口程序,我想單擊一個窗口中的按鈕,選擇圖像的路徑,然後在第二個窗口出現圖像。我已經做了幾乎所有的事情,但看起來像這樣:我啓動程序什麼都沒有出現在第二個窗口中,我讀取圖像,它不出現在第二個窗口,它看起來像我需要一些刷新第二個窗口後,我讀取圖像,但我不知道該怎麼做,請幫忙。
我知道有重繪()方法,但我不知道如何使用重繪在一個窗口,並迫使它重新繪製第二個
第一個文件:
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
}
可以請你發佈你試過的代碼 – poisonedYouth