我有A和B两个程序,我要实现的功能是,让B程序的代码和功能都在A程序的图形界面中显示出来。
程序Aimport java.awt.*;
import java.awt.event.*;
import java.io.*;public class LanDian {
public static void main(String args[]) {
new LDFrame(800,500);
}
}class LDFrame extends Frame {
public Tuxin tx = null;
private Panel p1=null,p2=null,p3=null; 
TextArea ta = new TextArea("alajlfj",180,100);
Button b1,b2,b3,b4,b5;
LDFrame(int w,int h) {
super("123");


b1 = new Button("a");
b1.setBounds(7,0,70,h/5);
b2 = new Button("b");
b2.setBounds(7,h/5,70,h/5);
b3 = new Button("c");
b3.setBounds(7,2*(h/5),70,h/5);
b4 = new Button("d");
b4.setBounds(7,3*(h/5),70,h/5);
b5 = new Button("e");
b5.setBounds(7,4*(h/5),70,h/5);
Monitor1 m1 = new Monitor1();
Monitor2 m2 = new Monitor2();
Monitor3 m3 = new Monitor3();
Monitor4 m4 = new Monitor4();
Monitor5 m5 = new Monitor5();
b1.addActionListener(m1);
b2.addActionListener(m2);
b3.addActionListener(m3);
b4.addActionListener(m4);
b5.addActionListener(m5);
setLayout(new BorderLayout(15,0));
setBounds(0,0,w,h);
p1 = new Panel(null);
p1.setSize(w/9,h);
p2 = new Panel(null);
p3 = new Panel(null);
p2.setBackground(Color.RED);
p2.setSize(100,h);
p2.setLayout(new BorderLayout());
p2.add(ta);
p3.setBackground(Color.blue);
p3.setSize(320,h);
p1.setBackground(Color.green);
add(p1,BorderLayout.WEST);add(p2,BorderLayout.CENTER);add(p3,BorderLayout.EAST);
    p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p1.add(b5);

setVisible(true);
addWindowListener( 
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}

public void paint(Graphics g) {
if(tx != null) tx.draw(g);              //这里,没等下面的注释执行就先执行                      
}                                              // 这样就达不到我预想的效果。。



class Monitor1 implements ActionListener {
public void actionPerformed(ActionEvent e) { 
try {
Tuxin pf = new Tuxin();
FileInputStream fis1 = new FileInputStream("D:/eclipse/LanDian/src/Tuxin.java");
byte b[] = new byte[1024];
  int len = fis1.read(b);
  ta.setText(new String(b,0,len)); } catch(FileNotFoundException f) {
  System.out.println("文件找不到");
} catch(IOException g) {
System.out.println("");
}

tx = new Tuxin();                       //这里想给tx赋值后,再让paint画出tx。
}
}class Monitor2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}class Monitor3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}class Monitor4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}class Monitor5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
}  程序Bimport java.awt.*;
import java.util.*;public class Tuxin {
public static final int R = 60;
public static void main(String args[]) {
//new PaintFrame().launchFrame();
Random r1 = new Random();  
r1.nextDouble();
Random r2 = new Random();
r2.nextDouble();
System.out.println(r1);
System.out.println(r2);
} public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.drawRect(100,100,100+2*R,100+2*R);
g.drawOval(100,100,100+R,100+R);
g.setColor(c);
}
}
问题1:程序中B中的图显示出来让其他组件(panel,textarea)遮起来了,该怎样把它显示在panel组件中?
问题2:见程序中的注释。

本人十足菜鸟,希望各位大神,能在原程序基础上帮忙改正!

解决方案 »

  1.   

    你的问题2就是要按钮触发画图效果,86行下面加一行LDFrame.this.repaint();就行了。但是图还是在下面的。
    关于问题1,因为你的paint()方法是直接在LDFrame上画的,组件都是加在Frame上面的。所以你要将画出来的图放到上面显示,要么在LDFrame上面弹出一个新窗口,把图画在里面,要么用JLayeredPane改变组件的层次关系,把画图的组件转到最上面来。(实力有限,目前我就想到这两种办法,我觉得还是弹出新窗口比较方便)
      

  2.   

    程序A部分改了下
    // public void paint(Graphics g) {
    // if(tx != null) tx.draw(g);              //这里,没等下面的注释执行就先执行                      
    // }                                              // 这样就达不到我预想的效果。。</span>


    class Monitor1 implements ActionListener {
    public void actionPerformed(ActionEvent e) { 
    try {
    FileInputStream fis1 = new FileInputStream("D:/eclipse/LanDian/src/Tuxin.java");
    byte b[] = new byte[1024];
      int len = fis1.read(b);
      ta.setText(new String(b,0,len)); } catch(FileNotFoundException f) {
      System.out.println("文件找不到");
    } catch(IOException g) {
    System.out.println("");
    }

    tx = new Tuxin((Window)LDFrame.this); //这里想给tx赋值后,再让paint画出tx。</span>
    tx.setVisible(true);
    }
    程序B
    public class Tuxin extends JDialog{
    public static final int R = 60;
    public static void main(String args[]) {
    //new PaintFrame().launchFrame();
    Random r1 = new Random();  
    r1.nextDouble();
    Random r2 = new Random();
    r2.nextDouble();
    System.out.println(r1);
    System.out.println(r2);
    Tuxin tx = new Tuxin(null);
    tx.setVisible(true);
    } public Tuxin(Window owner) {
    super(owner);
    setSize(400,500);
    }

    public void closeDialog() {
    dispose();
    }

    @Override
    public void paint(Graphics g) {
    Color c = g.getColor();
    g.setColor(Color.red);
    g.drawRect(100,100,100+2*R,100+2*R);
    g.drawOval(100,100,100+R,100+R);
    g.setColor(c);
    }
    }
    要用JLayeredPane的话,自己查查API吧
      

  3.   


    能解释下不?

    哪里不明白呢?
    弹出新窗口的方法我试着写了下,已经贴了代码了,这些API里都有的。
      

  4.   


    能解释下不?

    哪里不明白呢?
    弹出新窗口的方法我试着写了下,已经贴了代码了,这些API里都有的。LDFrame.this.repaint();你这句话是什么意思啊?我自己改了下我的代码,现在已经能在panel里面显示了(class MyPanel extends Panel {
    public MyPanel(LayoutManager layout) {
    super(layout);
    }
    public void paint(Graphics g) {
    if(tx != null) {
    tx.draw(g);              //tx开始赋值为null;
    }
    }
    }),但是运行了一下还是显示不了,必须隐藏一下界面再显示一下界面,让它调用paint方法才能显示。请问我现在这样改了怎样才能向上面一样再去调用它的repaint方法?
      

  5.   


    能解释下不?

    哪里不明白呢?
    弹出新窗口的方法我试着写了下,已经贴了代码了,这些API里都有的。LDFrame.this.repaint();你这句话是什么意思啊?我自己改了下我的代码,现在已经能在panel里面显示了(class MyPanel extends Panel {
    public MyPanel(LayoutManager layout) {
    super(layout);
    }
    public void paint(Graphics g) {
    if(tx != null) {
    tx.draw(g);              //tx开始赋值为null;
    }
    }
    }),但是运行了一下还是显示不了,必须隐藏一下界面再显示一下界面,让它调用paint方法才能显示。请问我现在这样改了怎样才能向上面一样再去调用它的repaint方法?
    paint(Graphics g)方法是在调用组件的repaint()方法里调用的,就是把组件重画一变。你看看api就清楚了。你只要在需要重画的地方调相应组件的repaint()就行了。
    关于LDFrame.this.repaint();这句话:
    repaint前的this指的是要重画的组件。
    这段代码需要调的是LDFrame的对象的repaint()方法,但是这句话却在Monitor1里面。直接repaint()或this.repaint(),此处this对象是Monitor1要报错的。解决的办法就是LDFrame.this.repaint();这么写。