File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a curious problem about canvas

 
alan fisher
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Craig Wood
Ranch Hand
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic