编辑:下面是我对此示例的SSCCE尝试,让您有机会自己编译。我需要:通知班级PanelNumber2
,班级Generator g
中的对象PanelNumber1
已被更改。
我尝试使用ChangeListner
和ActionPerformed
但是命令:
listener.stateChanged(new ChangeEvent(this));
我有nullpointer异常。如果你有任何解决方案,请纠正我。或者更好的方法来做到这一点
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainFrame extends JFrame{
private PanelNumber1 p1;
private PanelNumber2 p2;
public MainFrame(){
super("Frame");
JPanel panel = new JPanel(new BorderLayout());
this.getContentPane().add(panel);
p1 = new PanelNumber1();
Generator g = p1.getGenerator();
p2 = new PanelNumber2(g);
panel.add(p1,BorderLayout.CENTER);
panel.add(p2,BorderLayout.EAST);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame();
}
}
class PanelNumber2 extends JPanel implements ChangeListener{
public PanelNumber2(Generator g){
super();
setLayout(new GridLayout(7,1));
g.addListener(this);
}
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
this.repaint();
}
}
class PanelNumber1 extends JPanel{
Generator g;
BufferedImage mapa;
File imageFile;
public PanelNumber1(){
super();
g = new Generator();
g.setPanel(this);
imageFile = new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_ww1.png");
try {
mapa = ImageIO.read(imageFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(mapa, 0, 0, getWidth(), getHeight(), null);
}
public Generator getGenerator(){
return g;
}
}
class Generator{
ChangeListener listener;
ArrayList<String> s;
JPanel p;
Timer generatorTimer;
public Generator(){
this.s = new ArrayList<String>();
generatorTimer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addItem();
p.repaint();
}
});
generatorTimer.start();
}
private void addItem(){
s.add(new String("string"));
listener.stateChanged(new ChangeEvent(this));
}
public void setPanel(JPanel p){
this.p = p;
}
public ArrayList<String> getArray(){
return s;
}
public void addListener(ChangeListener list){
this.listener = list;
}
}
需要帮助的人:(
答案 0 :(得分:0)
解!
天啊,天啊,几个小时后,我知道错了! :P
我需要做的更多事情就是在timer
public void actionPerformed(ActionEvent e) {
if(p != null){ //becouse after creating Generator object im doing setPanel(this) a short while after :)
addItem();
p.repaint();
}
}
现在它完美无缺。感谢MadProgrammer试图帮助我和YMY - 他发现了这个错误。话题可以关闭