• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

a curious problem about canvas

 
Greenhorn
Posts: 5
  • 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);
}
}
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Ranch Hand
Posts: 1535
  • 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.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic