package Graph;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
Below here is my class which covers all other classes, and extends JFrame, which I have created by selecting "new > Java Frame" options in Netbeans.I created this in a package called Graph, and this filename is GraphFrame.Java.
public class GraphFrame extends javax.swing.JFrame {
public GraphFrame() {
}
The lines below is from my JFrame auto creating codes.
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
These lines below here is my main method
public static void main(String args[]) {
/* Create and display the form */
//java.awt.EventQueue.invokeLater(new Runnable() {
//public void run() {
GraphFrame Frame=new GraphFrame();
Frame.setVisible(true);
Frame.setTitle("Functional Dependency");
Frame.setSize(500, 500);
//}
// });
}
These lines is my DrawGraph class, which I created INSIDE the GraphFrame class. Besides that, it extends my JPanel which I made by dragging the JPanel icon from the Swing Containers (Sorry if that is the wrong way to add JPanel)
class DrawGraph extends JPanel{
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(10, 100, 380, 100);
g.drawLine(200, 30, 200, 190);
g.drawLine(380, 100, 370, 90);
g.drawLine(380, 100, 370, 110);
g.drawLine(200, 30, 190, 40);
g.drawLine(200, 30, 210, 40);
g.drawString("X", 360, 80);
g.drawString("Y", 220, 40);
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
My problem is, the lines of graph which I have coded, did not appear.I have googled, StackOverflowed, browsed through my lab exercises, and I can't seem to find the solution to this. Yes, this may need more reading, but to be honest, I couldn't quite understand what's wrong.
The problem is that you never add your JPanel to the frame you created.
This should do what you want.
Note that this.add(graph) can be called because
GraphFrame
extends JFrame. The.add()
method is a part of the JFrame class and can thus be called fromGraphFrame
You should also consider creating a new file for the DrawGraph class as it can get messy if you want to add more functionality to either
GraphFrame or DrawGraph
.Right now you're constructing a
GraphFrame
when you call the constructor for the objectFrame
(which should beframe
btw). However, in this constructor, you still need to "create" theJFrame
-- which is done in the constructor for theJFrame
class.All you have to do to call this super constructor for
JFrame
is put the following as the first line of your constructor inGraphFrame
:Also, as the other answerer has mentioned, in order to view your panel, you have to add it to the
JFrame
. Therefore, in your main method (or better yet in your constructor forGraphFrame
), add