Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help!! Strange

 
Aleksandar Mitrev
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made java code to draw lane on a JPanel. When I COMPILE and RUN the code there are no mistake but I can't see the lane on the JPanel the strang is thet if I run the program several times I'm able to see the lane on the JPanel example I run the code 20 times i cen see the lane once
Can someone tell me what is the problem . Sorry about my English
Here is the code:

import javax.swing.*;
import java.awt.*;
import java.lang.Object.*;


public class Proba extends JPanel {

public Proba(Container c) {
setBackground(Color.GRAY);
setSize(200, 200);
c.add(this);
drawing();

}

public void drawing() {
int W = getWidth();
int H = getHeight();
Graphics g = getGraphics();
g.setColor(Color.BLACK);
g.drawLine(1, H-1, 20, H-8);
}

public static void main(String args[]) {

JFrame win = new JFrame();
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = win.getContentPane();
win.setSize(400,400);
win.setVisible(true);
Proba panel = new Proba(cp);

}

}
 
Brian Cole
Author
Ranch Hand
Posts: 924
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aleksandar Mitrev:
I made java code to draw lane on a JPanel. When I COMPILE and RUN the code there are no mistake but I can't see the lane on the JPanel the strang is thet if I run the program several times I'm able to see the lane on the JPanel example I run the code 20 times i cen see the lane once


This code draws a line once (or at least tries to) but that's not
good enough. If you want the user to see the line you must ensure
that it gets drawn whenever it is needed. For example, if the user
places a foreign window on top of your application, the line must
be drawn again when your application becomes visible again.

The way you do this is to override the paintComponent() method
that is inherited from JPanel. You should replace your drawing()
method with something like this:



Other things I would consider:

A) It's probably better to add your Proba instance to a container before calling setVisible(true).

B) Don't do c.add(this) in Proba's constructor. Instead do cp.add(new Proba()) in main(). It's cleaner that way, don't you think?

C) Don't call setSize() in Proba's constructor. Calling setPreferredSize() would be more appropriate. The size will be ignored, however, because cp uses BorderLayout by default. Try calling cp.setLayout(new FlowLayout()) if you want to change that.
 
Aleksandar Mitrev
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks man you helpt me a lot I'm new at java. I want to ask you another if I make a JPanel and I add JLabel example
JPanel panel = new JPanel();
panel.add(new JLabel("1"));
is there a method that I can get the JLebol and the string, that in the case up is 1
something like panel.getLabel();
is there method like that I have search the sun documentation and I did not find
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aleksandar Mitrev:
...is there a method that I can get the JLebol and the string, that in the case up is 1...

To get the text from a JLabel, I think you want the getText method.

To get that label from a JPanel its been added to, look at the overloaded getComponent methods in java.awt.Container (which is a superclass of JPanel).
 
Yes, my master! Here is the tiny ad you asked for:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!