显示JFrame时获取另一个JFrame的数据

时间:2016-10-08 16:47:16

标签: java jframe drawing

我有两个扩展JFrame的类。首先,我从One Jframe获取输入并在另一个JFrame上显示图形但在第二个JFrame中它还显示第一个Jframe的内容。这是代码

class upgma extends JFrame
{
static int width;
static int height;
JPanel c;
int titem;
volatile boolean called;
static String finalans;
upgma()
{
    c = new JPanel();
    called = false;
    JTextField items;
    JButton init;
    c.setLayout(null);
    items=new JTextField();
    JLabel ni = new JLabel("Number of items");
    ni.setBounds(width/2-95,height/2-50,100,20);

    items.setBounds(width/2-100,height/2-20,100,20);
    init = new JButton("Next");
    init.setBounds(width/2+15,height/2-20,80,20);
    init.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if(items.getText().isEmpty() || !items.getText().matches("[1-9]?[0-9]*"))
            {
                JLabel e=new JLabel("Please Enter VALID Data !! ");
                e.setBounds(width/2-85,height/2+20,200,20);
                c.add(e);
                validate();
                repaint();
            }
            else
                enterData(items);
        }
    });
    c.add(ni);
    c.add(items);
    c.add(init);

    JScrollPane jScrollPane = new JScrollPane(c);
    jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    getContentPane().add(jScrollPane);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

    public static void main(String[] args)
    {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    width = (int)screenSize.getWidth();
    height = (int)screenSize.getHeight()-40;
    System.out.println(width+""+height);
    upgma u = new upgma();
    u.setSize(width,height);
    u.setVisible(true);
    u.setTitle("Unweighted Pair Group Method with Arithmetic Mean");

    while(!u.called);

    u.setVisible(false);
    u = null;
    drawing  d= new drawing(finalans);
    d.getRootPane().setOpaque(false);
    ((JComponent) d.getContentPane()).setOpaque(false);
    d.setSize(width,height);
    d.setVisible(true);
    d.setTitle("Unweighted Pair Group Method with Arithmetic Mean demo");
    }

}

另一个课程如下:

class drawing extends JFrame
{
Container c1;
static int width;
static int height;
String data;

drawing(String temp)
{
    data = temp;
    c1=getContentPane();
    c1.setLayout(null);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    width = (int)screenSize.getWidth();
    height = (int)screenSize.getHeight()-40;
}

public void paint(Graphics g) 
{
    super.paint(g);
    Font font = g.getFont().deriveFont( 20.0f );
    g.setFont( font );
    if(data.isEmpty())
    {   
        JLabel er = new JLabel("Nothing to Show !!");
        er.setBounds(width/2-100,height/2,150,40);
        c1.add(er);
    }
    else
    {
        int length = data.length();         
        draw(g,data,width/2-length*7,40);
    }
}
}

它向我显示如下:树打印在这里。我在我的绘图类中打印它。它显示的文本字段是扩展JFrame的第一类upgma。enter image description here

我没有到达我做错的地方..请帮助..

1 个答案:

答案 0 :(得分:1)

可能是因为你让它变得透明。 ((JComponent)d.getContentPane())。setOpaque(false);