File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

a curious problem about canvas

alan fisher

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();

class MainFrame extends JFrame {
public MainFrame()
Container contentPane = getContentPane();
JPanel jPanel2 = new JPanel();

jPanel2.setLayout(new BorderLayout());
Canvas nodeCanvas=new Canvas();
Canvas topoCanvas=new Canvas();
nodeCanvas.setBackground(new Color(255,0,255));
topoCanvas.setForeground(new Color(0,255,255));
Dirk Schreckmann

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.
I agree. Here's the link:
subject: a curious problem about canvas
It's not a secret anymore!