473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with java layout and panels and containers

OK Guyz I'm very new to JAVA I have two programs here the one is my Case Study where you put coordinates width and height and you choose from a list of shapes and its draw it... My main problem is I cannot arrange the JButton JRadioButton and JTextField and JLabel to be in the right direction I wanted it to have two separate panels kinda like this program i grab in the net the problem is I cannot incorporate the two programs together Any help will be much appreciated

my program
Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. public class CaseStudy extends JApplet
  6. {
  7.  
  8.     public void init()
  9.     {
  10.         Container content=getContentPane();
  11.         Mypanel panel=new Mypanel();
  12.         content.add(panel);
  13.         resize(800,400);
  14.         show();
  15.     }
  16. }
  17. class Mypanel extends JPanel implements ActionListener
  18.  
  19.     {
  20.         JRadioButton line,circle,rectangle,triangle,pie,trapezoid,octagon;
  21.         ButtonGroup  group;
  22.         JTextField text1,text2,text3,text4,text5,text6,text7,text8;
  23.         JLabel label1,label2,label3,label4,label5,label6,label7,label8;
  24.         JButton draw,showcoor;
  25.         int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
  26.         int shapes = 0;
  27.         public Mypanel()
  28.  
  29.         {
  30.  
  31.  
  32.  
  33.  
  34.  
  35.             JPanel panel1=new JPanel();
  36.             panel1.setLayout(new GridLayout(1,2));
  37.             panel1.setBackground(Color.red);
  38.  
  39.             JPanel panel2=new JPanel();
  40.             panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
  41.             panel2.setBackground(Color.green);
  42.  
  43.             JPanel panel3=new JPanel();
  44.             panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
  45.             panel3.setBackground(Color.blue);
  46.  
  47.  
  48.             JPanel panel4=new JPanel();
  49.             panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
  50.             panel4.setBackground(Color.pink);
  51.  
  52.  
  53.  
  54.  
  55.             line = new JRadioButton("LINE" , true);
  56.             line.addActionListener(this);
  57.             circle = new JRadioButton("CIRCLE" , false);
  58.             circle.addActionListener(this);
  59.             rectangle = new JRadioButton("RECTANGLE" , false);
  60.             rectangle.addActionListener(this);
  61.             triangle = new JRadioButton("TRIANGLE" , false);
  62.             triangle.addActionListener(this);
  63.             pie = new JRadioButton("PIE"  , false);
  64.             pie.addActionListener(this);
  65.             trapezoid = new JRadioButton("TRAPEZOID"  , false);
  66.             trapezoid .addActionListener(this);
  67.             octagon = new JRadioButton("OCTAGON"  , false);
  68.             octagon .addActionListener(this);
  69.  
  70.  
  71.             text1=new JTextField(4);
  72.             text2=new JTextField(4);
  73.             text3=new JTextField(4);
  74.             text4=new JTextField(4);
  75.             text5=new JTextField(4);
  76.             text6=new JTextField(4);
  77.             text7=new JTextField(4);
  78.             text8=new JTextField(4);
  79.             label1=new JLabel ("X Coor");
  80.             label2=new JLabel ("Y Coor");
  81.             label3=new JLabel ("Width");
  82.             label4=new JLabel ("Length");
  83.             label5=new JLabel ("Start Angle");
  84.             label6=new JLabel ("Arc Angle");
  85.             label7=new JLabel ("X 2 Coor");
  86.             label8=new JLabel ("Y 2 Coor");
  87.  
  88.  
  89.             draw=new JButton("DRAW");
  90.             showcoor=new JButton("Show Coordinates");
  91.             panel2.add(line);
  92.             panel2.add(circle);
  93.             panel2.add(rectangle);
  94.             panel2.add(triangle);
  95.             panel2.add(pie);
  96.             panel2.add(trapezoid);
  97.             panel2.add(octagon);
  98.             panel3.add(text1);
  99.             panel3.add(text2);
  100.             panel3.add(text3);
  101.             panel3.add(text4);
  102.             panel3.add(text5);
  103.             panel3.add(text6);
  104.             panel3.add(text7);
  105.             panel3.add(text8);
  106.             panel4.add(label1);
  107.             panel4.add(label2);
  108.             panel4.add(label3);
  109.             panel4.add(label4);
  110.             panel4.add(label5);
  111.             panel4.add(label6);
  112.             panel4.add(label7);
  113.             panel4.add(label8);
  114.             panel2.add(draw);
  115.             panel2.add(showcoor);
  116.             draw.addActionListener(this);
  117.  
  118.  
  119.             group=new ButtonGroup();
  120.             group.add(line);
  121.             group.add(circle);
  122.             group.add(rectangle);
  123.             group.add(triangle);
  124.             group.add(pie);
  125.             group.add(trapezoid);
  126.             group.add(octagon);
  127.  
  128.  
  129.  
  130.             add(panel1);
  131.             add(panel2);
  132.             add(panel3);
  133.             add(panel4);
  134.             }
  135.             public void actionPerformed(ActionEvent ev)
  136.             {
  137.             String pindot = ev.getActionCommand();
  138.  
  139.  
  140.         if(pindot=="LINE"){
  141.         shapes =1;
  142.             repaint();
  143.         }else if (pindot== "CIRCLE"){
  144.         shapes =2;
  145.             repaint();
  146.         }else if (pindot== "RECTANGLE"){
  147.         shapes =3;
  148.             repaint();
  149.         }else if (pindot== "TRIANGLE"){
  150.         shapes =4;
  151.             repaint();
  152.         }else if (pindot== "PIE"){
  153.                 shapes =5;
  154.             repaint();
  155.         }else if (pindot== "TRAPEZOID"){
  156.                 shapes =6;
  157.             repaint();
  158.         }else if (pindot== "OCTAGON"){
  159.                 shapes =7;
  160.             repaint();
  161.  
  162.             x1=Integer.parseInt(text1.getText());
  163.             x2=Integer.parseInt(text2.getText());
  164.             x3=Integer.parseInt(text3.getText());
  165.             x4=Integer.parseInt(text4.getText());
  166.             y1=Integer.parseInt(text5.getText());
  167.             y2=Integer.parseInt(text6.getText());
  168.             y3=Integer.parseInt(text7.getText());
  169.             y4=Integer.parseInt(text8.getText());
  170.  
  171.  
  172.  
  173.         }
  174.  
  175.         }
  176.         public void paintComponent(Graphics a)
  177.             {
  178.                 a.setColor(new Color(162,251,160));
  179.                 a.fillRect(0,0,400,400);
  180.  
  181.                 a.setColor(Color.black);
  182.                         for(int i=10;i<400;i=i+10){
  183.                             a.drawLine(0,i,400,i);
  184.                             a.drawLine(i,0,i,400);
  185.                         }
  186.  
  187.  
  188.                     if (shapes==1){
  189.                     a.setColor(new Color(116,19,215));
  190.                     a.drawLine(x1,x2,x3,x4);
  191.  
  192.                     }else if (shapes==2){
  193.                     a.setColor(new Color(116,19,215));
  194.                     a.fillOval(x1,x2,x3,x4);
  195.  
  196.                     }else if (shapes==3){
  197.                     a.setColor(new Color(116,19,215));
  198.                     a.fillRect(x1,x2,x3,x4);
  199.  
  200.  
  201.                     }else if (shapes==4){
  202.                     a.setColor(new Color(116,19,215));
  203.                     int x []={x1,x2,x3};
  204.                     int y []={x4,y1,y2};
  205.                     a.fillPolygon(x,y,3);
  206.  
  207.                     }else if (shapes==5){
  208.                     a.setColor(new Color(116,19,215));
  209.                     a.fillArc(x1,x2,x3,x4,y1,y2);
  210.  
  211.  
  212.                     }else if (shapes==6){
  213.  
  214.                     a.setColor(new Color(116,19,215));
  215.                     a.fillRect(x1,x2,x3,x4);
  216.                     a.setColor(new Color(162,251,160));
  217.                     int x12 []={x1,x1,x1+x3/4};
  218.                     int y12 []={x2,x2+x4,x2};
  219.                     a.fillPolygon(x12,y12,3);
  220.                     a.setColor(new Color(162,251,160));
  221.                     int x13 []={x1+x3,x1+x3,x1+x3/4*3};
  222.                     int y13 []={x2,x2+x4,x2};
  223.                     a.fillPolygon(x13,y13,3);
  224.  
  225.  
  226.                     }if (shapes==7){
  227.  
  228.                     a.setColor(new Color(116,19,215));
  229.                     int x []={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
  230.                     int y []={x2,x2-x4/2,x2-x4/2,x2};
  231.                     a.fillPolygon(x,y,4);
  232.                     a.setColor(new Color(116,19,215));
  233.                     int x11[]={x1,x1+(x3/4),x1+(x3/4*3),x1+x3};
  234.                     int y11 []={x2+x4,x2+x4+x4/2,x2+x4+x4/2,x2+x4};
  235.                     a.fillPolygon(x11,y11,4);
  236.                     a.setColor(new Color(116,19,215));
  237.                     a.fillRect(x1,x2,x3,x4);
  238.  
  239.  
  240.  
  241. }
  242.  
  243.  
  244.             }
  245.  
  246.     }
  247.  

and this is the layout out i wanted the grid should be in the left panel and all the components are in the right panel so when i draw i specific shape the shape will be in the left panel

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class JPanels extends JFrame {
  5. public static void main(String[] args) {
  6. new JPanels();
  7. }
  8.  
  9. JRadioButton line,circle,rectangle,triangle,pie,trapezoid,octagon;
  10. ButtonGroup  group;
  11. JTextField text1,text2,text3,text4,text5,text6,text7,text8;
  12. JLabel label1,label2,label3,label4,label5,label6;
  13. JButton draw,showcoor;
  14. int x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
  15. int shapes = 0;
  16.  
  17. public JPanels () {
  18. super("Test Window");
  19. Container content = getContentPane();
  20. content.setBackground(Color.lightGray);
  21.  
  22. JPanel bothsides = new JPanel(new GridLayout(1, 2));
  23. bothsides.setBackground(Color.DARK_GRAY);
  24.  
  25.  
  26. JPanel leftside = new JPanel();
  27. leftside.setBackground(Color.DARK_GRAY);
  28. leftside.setPreferredSize(new Dimension(400, 200));
  29.  
  30.  
  31. JPanel rightside = new JPanel();
  32. rightside.setBackground(Color.PINK);
  33. rightside.setPreferredSize(new Dimension(400, 200));
  34.  
  35.  
  36. text1=new JTextField(4);
  37.             text2=new JTextField(4);
  38.             text3=new JTextField(4);
  39.             text4=new JTextField(4);
  40.             text5=new JTextField(4);
  41.             text6=new JTextField(4);
  42.             text7=new JTextField(4);
  43.             text8=new JTextField(4);
  44.             label1=new JLabel ("X Coor");
  45.             label2=new JLabel ("Y Coor");
  46.             label3=new JLabel ("Width");
  47.             label4=new JLabel ("Length");
  48.             label5=new JLabel ("Width");
  49.             label6=new JLabel ("Length");
  50.  
  51.  
  52.         rightside.add(text1);
  53.         rightside.add(text2);
  54.         rightside.add(text3);
  55.         rightside.add(text4);
  56.         rightside.add(text5);
  57.         rightside.add(text6);
  58.         rightside.add(text7);
  59.         rightside.add(text8);
  60.  
  61.  
  62.  
  63. bothsides.add(leftside);
  64. bothsides.add(rightside);
  65.  
  66. content.add(bothsides);
  67.  
  68. pack();
  69. setVisible(true);
  70. }
  71. }
  72.  
  73.  
Mar 19 '11 #1
0 1852

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: knoak | last post by:
hi there, A few days ago i posted a question how i could divide a query's result over 2 collumns. I got the following code thanks to Pedro: > echo '<table class="search_result"...
2
by: Avin Patel | last post by:
Hi I have converted the code from Java to C#, using .net tool The Base Class implementes ISerializable interface. Which makes me implement this interface in all inherited class, In order to get...
12
by: Mahesh Devjibhai Dhola [MVP] | last post by:
Hi, I have added few of the events in some control, example code is: btnControl.GotFocus +=new EventHandler(EventHandlingMethod); btnControl.Click +=new EventHandler(EventHandlingMethod);...
1
by: Anoop | last post by:
Hi, Requirement: Create Edit Forms dynamically i.e. at runtime in C#.net. The child contol(s) information e.g. no.of control, their type(lable/textbox), enabled/disabled etc. is coming from the...
0
by: Nathan Laff | last post by:
I have a user control which has a custom label. when the labels width changes, it adjusts it's height to fit it all. Then when I adjust the width of the user control if the height of the label...
2
by: desperateboy | last post by:
hi I am trying to create a two-column layout using XHTML and CSS. I'm having a problem having the text in the second column start at the top of the column. How do I make sure that the text will go...
10
by: ellie2905 | last post by:
Hello, I am new to this forum and I am glad I found it because it seems that it will help me with my problem.I have creates a site using jsf components like grid panels and buttons.In the mozilla...
5
by: creative1 | last post by:
hi everyone, I am struck in layout of my site. Im using 1024 fixed width layout. I have a site layout designed in psd files. Bisically I have to stick with that design tightly and I am asked not to...
3
by: Andrea Gasperi | last post by:
Hi there not sure if this is the right place but i'm proud to use for the first time the "Managed Newsgroups" feature of my brand new MSDN subscription :-) So here is the problem. I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.