问题描述
编辑:下面是我对这个例子给ü机会编译这个在自己的SSCCE尝试。我需要:类通知类 PanelNumber2
的对象发电机G
PanelNumber1
已被更改。
EDITED: below is my try of SSCCE for this example to give u an opportunity to compile this on your own. I need to: inform class PanelNumber2
that object Generator g
in class PanelNumber1
have been changed.
我试着用 ChangeListner
和的actionPerformed
然而,在命令:
I tried with ChangeListner
and ActionPerformed
however on command:
listener.stateChanged(新的ChangeEvent(本));
我有空指针异常。纠正我,如果u已经得到这个问题的任何解决方案。或者更好的方式来做到这一点。
i have got nullpointer exception. correct me if u have got any solution of this problem. or any better way to do this
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;
}
}
需要帮助的家伙:(
Need help guys :(
推荐答案
解决方案!
哦,我的上帝,经过几个小时figuiring出什么错我得到它! :P
Oh my God, after few hours figuiring out whats wrong i got it! :P
一切什么我需要做的更多的是在加入定时
everything what i need to do more was adding a condition in 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 - 他发现的错误。话题可以关闭
now it works perfectly. Thanks going for MadProgrammer for trying to help me and for YMY - he find that mistake. topic can be closed
这篇关于改变监听器在其他类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!