请各位高手帮下忙 为何下列代码执行后 点击开始按钮后 在文本框不会输出内容 代码没有报错
import java.awt.EventQueue;
import java.util.concurrent.Semaphore;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.concurrent.*;
import java.lang.Thread;
public class FruitUI {
public Signal dish = new Signal(1);
public Signal Sapple = new Signal(0);
public Signal Sorange = new Signal(0);
public JFrame frame;
public JTextField textField;
public JTextField textField_1;
public JTextField textField_2;
public JTextField textField_3;/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FruitUI window = new FruitUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}/**
* Create the application.
*/
public FruitUI() {
initialize();
}/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 566, 521);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);JLabel label_1 = new JLabel("\u6C34\u679C\u95EE\u9898");
label_1.setForeground(new Color(0, 153, 255));
label_1.setFont(new Font("微软雅黑", Font.BOLD, 34));
label_1.setBounds(218, 0, 153, 56);
frame.getContentPane().add(label_1);JLabel label_2 = new JLabel("");
label_2.setIcon(new ImageIcon("Z:\\Users\\superman303\\Pictures\\\u7D20\u6750
2.0btn
2-0btn_02.png"));
label_2.setBounds(157, 0, 256, 73);
frame.getContentPane().add(label_2);JLabel label_3 = new JLabel("\u5F00\u59CB");
label_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
Father father = new Father();
Mother mother = new Mother();
Son son = new Son();
Daughter daughter = new Daughter();
Thread f = new Thread(father);
Thread m = new Thread(mother);
Thread s = new Thread(son);
Thread d = new Thread(daughter);f.start();
m.start();
s.start();
d.start();}
}
);
label_3.setFont(new Font("黑体", Font.BOLD, 26));
label_3.setIcon(new ImageIcon("Z:\\Users\\superman303\\Pictures\\\u7D20\u6750
twg_retina_icon
video_play_64.png"));
label_3.setBounds(218, 386, 133, 73);
frame.getContentPane().add(label_3);JLabel label_4 = new JLabel("");
label_4.setIcon(new ImageIcon("Z:\\Users\\superman303\\Pictures\\\u7D20\u6750
twg_retina_icon
arrow_right_64.png"));
label_4.setBounds(127, 175, 70, 79);
frame.getContentPane().add(label_4);textField = new JTextField();
textField.setBounds(312, 137, 158, 39);
frame.getContentPane().add(textField);
textField.setColumns(10);textField_1 = new JTextField();
textField_1.setBounds(312, 175, 158, 39);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);textField_2 = new JTextField();
textField_2.setBounds(312, 224, 158, 39);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);textField_3 = new JTextField();
textField_3.setBounds(312, 262, 158, 39);
frame.getContentPane().add(textField_3);
textField_3.setColumns(10);}
}
class Signal extends Semaphore{
int values;
public Signal(int permits) {
super(permits);
this.value = permits;
// TODO Auto-generated constructor stub
}
int value;
public synchronized void P(){
value--;
if(value<0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void V(){
value++;
if(value<=0){
this.notify();
}
}
}class Mother extends FruitUI implements Runnable{
public void run() {
// TODO Auto-generated method stubdish.P();textField.setText("妈妈放橘子");Sorange.V();
}
}class Father extends FruitUI implements Runnable{
public void run() {
// TODO Auto-generated method stub
dish.P(); 
textField_2.setText("爸爸放苹果");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Sapple.V();
}}
class Son extends FruitUI implements Runnable{
public void run() {
// TODO Auto-generated method stubSorange.P();
textField_1.setText("儿子取橘子");dish.V();
}
}class Daughter extends FruitUI implements Runnable{
public void run() {
// TODO Auto-generated method stubSapple.P();
String s = "女儿取苹果";
textField_3.setText(s);
dish.V();
}
}
请各位大大帮忙看看 感激不尽 临码涕零

解决方案 »

  1.   

    楼主,你这样写是不对的,你让Father,Mother,Daughter,Son都继承于FruitUI,他们操作的数据实际上是他们内部的数据而不是FruitUI的数据,如果你要让它们修改FruitUI的数据,你应该不要让Father,Mother,Daughter,Son四个类继承FruitUI,而是通过传递参数的方法把需要的参数传到相应的类里面去,下面是我改的代码,关于图片的代码我删了,你自己重新补上去吧import java.awt.EventQueue;
    import java.util.concurrent.Semaphore;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import java.awt.Font;
    import java.awt.Color;
    import javax.swing.JTextField;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.concurrent.*;
    import java.lang.Thread;public class FruitUI {
    public Signal dish = new Signal(1);
    public Signal Sapple = new Signal(0);
    public Signal Sorange = new Signal(0);
    public JFrame frame;
    public JTextField textField;
    public JTextField textField_1;
    public JTextField textField_2;
    public JTextField textField_3;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    FruitUI window = new FruitUI();
    window.frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the application.
    */
    public FruitUI() {
    initialize();
    }

    /**
    * Initialize the contents of the frame.
    */
    public void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 566, 521);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel label_1 = new JLabel("\u6C34\u679C\u95EE\u9898");
    label_1.setForeground(new Color(0, 153, 255));
    label_1.setFont(new Font("微软雅黑", Font.BOLD, 34));
    label_1.setBounds(218, 0, 153, 56);
    frame.getContentPane().add(label_1);

    JLabel label_2 = new JLabel("");
    //我这里没有图片,就把图片都注释掉了,你自己补上去吧
    //label_2.setIcon(new ImageIcon("Z:\\Users\\superman303\\Pictures\\\u7D20\u67502.0btn2-0btn_02.png"));
    label_2.setBounds(157, 0, 256, 73);
    frame.getContentPane().add(label_2);

    JLabel label_3 = new JLabel("\u5F00\u59CB");
    label_3.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
    Father father = new Father(dish, Sapple, textField_2);
    Mother mother = new Mother(dish, Sorange, textField);
    Son son = new Son(dish, Sorange, textField_1);
    Daughter daughter = new Daughter(dish, Sapple, textField_3);
    Thread f = new Thread(father);
    Thread m = new Thread(mother);
    Thread s = new Thread(son);
    Thread d = new Thread(daughter);

    f.start();
    m.start();
    s.start();
    d.start();

    }
    }
    );
    label_3.setFont(new Font("黑体", Font.BOLD, 26));
    //label_3.setIcon(new ImageIcon("Z:\\Users\\superman303\\Pictures\\\u7D20\u6750twg_retina_iconvideo_play_64.png"));
    label_3.setBounds(218, 386, 133, 73);
    frame.getContentPane().add(label_3);

    JLabel label_4 = new JLabel("");
    //label_4.setIcon(new ImageIcon("Z:\\Users\\superman303\\Pictures\\\u7D20\u6750twg_retina_iconarrow_right_64.png"));
    label_4.setBounds(127, 175, 70, 79);
    frame.getContentPane().add(label_4);

    textField = new JTextField();
    textField.setBounds(312, 137, 158, 39);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(312, 175, 158, 39);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

    textField_2 = new JTextField();
    textField_2.setBounds(312, 224, 158, 39);
    frame.getContentPane().add(textField_2);
    textField_2.setColumns(10);

    textField_3 = new JTextField();
    textField_3.setBounds(312, 262, 158, 39);
    frame.getContentPane().add(textField_3);
    textField_3.setColumns(10);

    }
    }
    class Signal extends Semaphore{
    int values;
    public Signal(int permits) {
    super(permits);
    this.value = permits;
    // TODO Auto-generated constructor stub
    }
    int value;

    public synchronized void P(){
    value--;
    if(value<0){
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    public synchronized void V(){
    value++;
    if(value<=0){
    this.notify();
    }
    }
    }class Mother implements Runnable{ private Signal dish;
    private Signal Sorange;
    private JTextField textField;

    public Mother(Signal dish, Signal Sorange, JTextField textField)
    {
    this.dish = dish;
    this.Sorange = Sorange;
    this.textField = textField;
    } public void run() {
    // TODO Auto-generated method stub

    dish.P();

    textField.setText("妈妈放橘子");

    Sorange.V();
    }
    }class Father implements Runnable{ private Signal dish;
    private Signal Sapple;
    private JTextField textField_2;

    public Father(Signal dish, Signal Sapple, JTextField textField_2)
    {
    this.dish = dish;
    this.Sapple = Sapple;
    this.textField_2 = textField_2;
    } public void run() {
    // TODO Auto-generated method stub
    dish.P(); 
    textField_2.setText("爸爸放苹果");
    try {
    Thread.sleep(200);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Sapple.V();
    }}
    class Son implements Runnable{ private Signal dish;
    private Signal Sorange;
    private JTextField textField_1;

    public Son(Signal dish, Signal Sorange, JTextField textField_1)
    {
    this.dish = dish;
    this.Sorange = Sorange;
    this.textField_1 = textField_1;
    } public void run() {
    // TODO Auto-generated method stub

    Sorange.P();
    textField_1.setText("儿子取橘子");

    dish.V();
    }
    }class Daughter implements Runnable{

    private Signal dish;
    private Signal Sapple;
    private JTextField textField_3;

    public Daughter(Signal dish, Signal Sapple, JTextField textField_3)
    {
    this.dish = dish;
    this.Sapple = Sapple;
    this.textField_3 = textField_3;
    }

    public void run() {
    // TODO Auto-generated method stub

    Sapple.P();
    String s = "女儿取苹果";
    textField_3.setText(s);
    dish.V();
    }
    }