• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot manage layout in the frame

 
suraya ya'acob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm drawing a line using a class with extends JComponent. So when i want to have many lines, i have to import many classes. The problem is, i've tried using gridLayout and borderLayout to manage the layout in JFrame but the lines didnt appear... below is my coding..
please help me
My main coding...............
import java.awt.*;
import java.awt.Container;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.border.*;
import java.awt.image.*;
import java.sql.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;
class carian22 extends JFrame{

public carian22()
{

Container pane=getContentPane();
//pane.setLayout (new BorderLayout());
JPanel grafik = new JPanel();
grafik.setLayout(new GridLayout(2,1));

JPanel timeline = new JPanel();
timeline.setLayout(new GridLayout(1,2));
OneLine o = new OneLine(20,200,100,200);
timeline.add(o);
TwoLine t = new TwoLine(110,200,200,200);
timeline.add(t);

JPanel panelberat = new JPanel();

Berat b = new Berat(50, 300,100,300);
panelberat.add(b);
grafik.add(timeline);
grafik.add(panelberat);
pane.add(grafik);
/*
Berat b = new Berat(50, 300,400,300);
pane.add(b);
*/
setBounds(0,0,1024,730);
setVisible(true);
}

public static void main(String args[])
{
carian22 aya = new carian22();
aya.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

class with extends JComponent coding(example just for OneLine.java, same goes to the rest..)
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import java.awt.*;
public class OneLine extends JComponent {
private int x,y,x2,y2,tebal, warna;
int tinggi, kaler;
public OneLine(int x, int y, int x2, int y2) {
this.x = x;
this.y = y;
this.x2 = x2;
this.y2 = y2;

}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d=(Graphics2D) g;

g2d.drawLine(x,y,x2,y2);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
}
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use a JSeperator instead of reinventing the wheel? It's not just for Menu and Tool Bars. You can use it just like any other component in SWING.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic