SOS: JPanel ... Help MEEEE!

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class jd extends JPanel implements ActionListener

{

int R = 120;

int T = 120;

public void paintComponent(Graphics g)

{

g.setColor(Color.RED);

g.drawLine(50,50,R,T);

g.drawLine(R,T,200,200);

}

public void SetupPage()

{

JFrame frame = new JFrame("Grrrr");

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,300);

frame.getContentPane().setLayout(new BorderLayout());

JButton j = new JButton("abcd");

frame.add(j);

j.addActionListener(this);

frame.getContentPane().add(j, BorderLayout.NORTH);

JPanel Graph = new jd();

frame.getContentPane().add(Graph);

//frame.getContentPane().add(Graph, BorderLayout.SOUTH);

frame.setVisible(true);

}

public static void main(String args[])

{

jd J = new jd();

J.SetupPage();

}

public void actionPerformed(ActionEvent e)

{

R = 500;

T = 500;

repaint();

}

}

I dont know why... the repaint never works.... and the initial line wont even show if i add the graph to the BorderLayout.SOUTH .. I have to set it to the contentPage ... I am confused.. Help me... Thanks in advance!

[1585 byte] By [CMaila] at [2007-11-27 4:09:34]
# 1

You should do the basic tutorials: http://java.sun.com/docs/books/tutorial/uiswing/

Try this code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Jd extends JPanel implements ActionListener{

int r = 120;

int t = 120;

public void paintComponent(Graphics g){

g.setColor(Color.RED);

g.drawLine(50, 50, r, t);

g.drawLine(r, t, 200, 200);

}

public static void setupPage(){

JFrame frame = new JFrame("Grrrr");

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton j = new JButton("abcd");

frame.getContentPane().add(j, BorderLayout.NORTH);

Jd graph = new Jd();

frame.getContentPane().add(graph, BorderLayout.CENTER);

j.addActionListener(graph);

frame.setSize(500, 500);

frame.setVisible(true);

}

public static void main(String args[]){

setupPage();

}

public void actionPerformed(ActionEvent e){

r = 500;

t = 500;

repaint();

}

}

hiwaa at 2007-7-12 9:15:01 > top of Java-index,Java Essentials,New To Java...
# 2
thanks a lot. it worked. but there is a small problem now.The first click shows the new line. If I click again, a new button is added to the page. How can I stop that?
CMaila at 2007-7-12 9:15:01 > top of Java-index,Java Essentials,New To Java...
# 3

> thanks a lot. it worked. but there is a small problem

> now.

>

> The first click shows the new line. If I click again,

> a new button is added to the page. How can I stop

> that?

When I use HIWA's code, no new button appears.

If you are talking about new effects that have happened after changing your code, then you have to show your code. There are many clever coders who read this forum, but none smart enough to read minds.

Good luck.

/Pete

petes1234a at 2007-7-12 9:15:01 > top of Java-index,Java Essentials,New To Java...