package test;import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;class PicPanel extends JPanel{ private JButton bStart;
private JButton bOk;
private JTextField text;

PicPanel(){ //setLayout(new BorderLayout());

bStart = new JButton("start");
bStart.addActionListener(new Controller());

bOk = new JButton("ok");
bOk.addActionListener(new Controller());

        text = new JTextField(10);
text.setText("wo");

    add(bStart);
add(text);
add(bOk);
}
JTextField getText(){
return text;
}
}
package test;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;class Controller implements ActionListener{

public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根

PicPanel p = new PicPanel();

if(e.getActionCommand().equals("start"))
p.getText().setText("shijian");

else if(e.getActionCommand().equals("ok"))
p.getText().setText("end");
}
}import javax.swing.JFrame;public class MainTest {
private static JFrame frame;

public static void main(String[] args){

frame = new JFrame();
PicPanel pic = new PicPanel();

frame.add(pic);

frame.setVisible(true);
frame.setDefaultCloseOperation(1);
}
}
为什按键之后,text中的内容不改变啊?我是想实现按键之后text中的内容改变!
那位高手帮忙啊!!!

解决方案 »

  1.   

    class Controller implements ActionListener {
    PicPanel p;
    public Controller(PicPanel p){//这里注意
    this.p=p;
    }
    public void actionPerformed(ActionEvent e) {
    // TODO 自动生成方法存根
    if (e.getActionCommand().equals("start"))
    p.getText().setText("shijian"); else if (e.getActionCommand().equals("ok"))
    p.getText().setText("end");
    }
    }------------------
    class PicPanel extends JPanel { private JButton bStart; private JButton bOk; private JTextField text; PicPanel() {
    bStart = new JButton("start");
    bStart.addActionListener(new Controller(this)); bOk = new JButton("ok");
    bOk.addActionListener(new Controller(this));
    text = new JTextField(10);
    text.setText("wo"); add(bStart);
    add(text);
    add(bOk);
    } JTextField getText() {
    return text;
    }
    } 已经可以了
      

  2.   

    哦,明白了  我在Controller里生成PicPanel的对象和在MainTest里生成PicPanel的对象没有任何的关联,
    在两个类里是独立的,是独立的两个对象,所以实现不了........是这样吗,huayiluo ?
    谢了啊!!!!
      

  3.   

    package Cai24;import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;import contorller.Contorller;public class GamePanel extends JPanel {

    JButton bStart;
    JButton bOk;
    JTextField tText;
    JLabel lPicture;

    GamePanel(){
    setLayout(new BorderLayout());

    bStart = new JButton("start");
    bStart.addActionListener(new Contorller(this));
    bOk = new JButton("ok");
    bOk.addActionListener(new Contorller(this));

    tText = new JTextField(10);

    JPanel p = new JPanel();
    p.add(bStart);
    p.add(tText);
    p.add(bOk);
    add(p,BorderLayout.SOUTH);

    } public void displayPic(){

         lPicture = new JLabel(new ImageIcon(this.getClass().getResource("wo.jpg")));
         lPicture.setSize(500, 500);
    this.add(lPicture,BorderLayout.CENTER);
    }
    }
    package contorller;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import Cai24.GamePanel;public class Contorller implements ActionListener {

    private GamePanel g; public Contorller(GamePanel g) {
    this.g = g;
    } public void actionPerformed(ActionEvent e) {
    // TODO 自动生成方法存根
    if(e.getActionCommand().equals("start")){
    g.displayPic();
    System.out.println("shi");
    }
    else if(e.getActionCommand().equals("ok")){
    System.out.println("shijian");
    }
    }}
    package Cai24;import javax.swing.JFrame;public class GameMain{

    public static void main(String[] args){

    GamePanel gPanel = new GamePanel();
    JFrame fGame = new JFrame("wo");

    fGame.add(gPanel);

    fGame.setSize(gPanel.getWidth()+5, gPanel.getHeight()+10);
    fGame.setDefaultCloseOperation(1);
    fGame.setVisible(true);
    }}
    很奇怪的事呀,图片在我按键之后不会显示,点击Frame的最小化之后,再最大化,图片就又显示在那里!是什么原因啊?huqyiluo你再帮我看看啊!谢了啊大家帮忙啊!!!!!!!!!谢了
      

  4.   


    public void displayPic(){      lPicture = new JLabel(new ImageIcon(this.getClass().getResource("wo.jpg"))); 
         lPicture.setSize(500, 500); 
    this.repaint();
    this.add(lPicture,BorderLayout.CENTER);