aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes a curious problem about canvas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "a curious problem about canvas " Watch "a curious problem about canvas " New topic
Author

a curious problem about canvas

alan fisher
Greenhorn

Joined: Sep 27, 2003
Posts: 5
I add two canvas into a panel, as for these tow canvas, I set different background,but when the program runs,it shows only one canvas,why?
The test source code as follows:


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;


public class Test
{
public static void main(String[] args)
{
JFrame frame = new MainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class MainFrame extends JFrame {
public MainFrame()
{
setSize(300,300);
setTitle("BounceThread");
Container contentPane = getContentPane();
JPanel jPanel2 = new JPanel();
contentPane.add(jPanel2);

jPanel2.setLayout(new BorderLayout());
Canvas nodeCanvas=new Canvas();
Canvas topoCanvas=new Canvas();
nodeCanvas.setSize(jPanel2.getWidth(),(int)0.6*jPanel2.getHeight());
nodeCanvas.setBackground(new Color(255,0,255));
topoCanvas.setSize(jPanel2.getWidth(),(int)0.4*jPanel2.getHeight());
topoCanvas.setForeground(new Color(0,255,255));
jPanel2.add(nodeCanvas,BorderLayout.CENTER);
jPanel2.add(topoCanvas,BorderLayout.SOUTH);
}
}
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Swing likes the setPreferredSize method. Also, in Swing we use either JComponent or JPanel instead of Canvas. It is recommended not to mix heavyweight AWT components with lightweight Swing components.
 
 
subject: a curious problem about canvas