Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

simple interface

Posted on 2002-05-08
8
Medium Priority
?
236 Views
Last Modified: 2010-03-31
i am quite new to java programming and would like to know how i could make a simple interface, GUI  

Program title

 Label 1      textBox 1    Button 1  
 Label 2      textBox 2    Button 2  
 Label 3      textBox 3    Button 3

                           Button 4



 
0
Comment
Question by:stevenglass
7 Comments
 

Expert Comment

by:InSaNiE
ID: 6996117
class simpleGui extends JFrame{
  JLabel label1 = new JLabel("Label1");
  JLabel label2 = new JLabel("Label2");
  JLabel label3 = new JLabel("Label3");
  JTextField field1 = new JTextField();
  JTextField field2 = new JTextField();
  JTextField field3 = new JTextField();
  JButton button1 = new JButton("button1");
  JButton button2 = new JButton("button2");
  JButton button3 = new JButton("button3");
  JButton button4 = new JButton("button4");


  GridBaglayout gridbag = new GridBagLaout();
  GridBagConstraints c = new GridBagConstraints();
JPanel pane = new JPanel();

  public void constrain(Components c,int x,int y,int w,int h,int wx,int wy){
       c.gridx = x;
       c.gridy =y;
       c.gridwidth = w;
       c.gridheight = h;
       c.weightx = wx;
       c.weighty = wy;
 }

     public simpleGui(){
         super("Program title");
         this.setSize(200,100);
     
     pane.setLayout(gridBag);
     constrain(label1,0,0,1,1,33,25);
     pane.getLayout().setConstraints(label1,c);
     pane.add(label1);
     constrain(label1,0,1,1,1,33,25);
     pane.getLayout().setConstraints(label2,c);
     pane.add(label2);
     constrain(label1,0,2,1,1,33,25);
     pane.getLayout().setConstraints(label3,c);
     pane.add(label3);
     constrain(field1,1,0,1,1,33,25);
     pane.getLayout().setConstraints(field1,c);
     pane.add(label3);
     constrain(field2,1,1,1,1,33,25);
     pane.getLayout().setConstraints(field2,c);
     pane.add(label3);
     constrain(field3,1,2,1,1,33,25);
     pane.getLayout().setConstraints(field3,c);
     pane.add(label3);
     constrain(button1,2,0,1,1,33,25);
     pane.getLayout().setConstraints(button1,c);
     pane.add(button1);
     constrain(button2,2,1,1,1,33,25);
     pane.getLayout().setConstraints(button2,c);
     pane.add(button2);
     constrain(button3,2,2,1,1,33,25);
     pane.getLayout().setConstraints(button3,c);
     pane.add(button3);
     constrain(button4,3,3,1,1,100,25);
     pane.getLayout().setConstraints(button4,c);
     pane.add(button4);

    this.getContentPane().add(pane);
 
     }

public static void main(String arg[]){
       simpleGui s = new simpleGui();
       s.show();
}
}

try this - I dont' have java on this machine so i can't test it. You will also have to see what classes to import.
If you have any questions ask - I will eventually answer them.
0
 

Expert Comment

by:InSaNiE
ID: 6996129
this.getContentPane().add(pane);

    the above should be -

this.getContentPane().setContentPane(pane);

sorry about that....

0
 

Expert Comment

by:InSaNiE
ID: 6996364
public void constrain(Components c,int x,int y,int w,int h,int wx,int wy){

should be..

public void constrain(Component c,int x,int y,int w,int h,int wx,int wy){

- no s on the end of Component.
     
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:stevenglass
ID: 6997104
cannot get it to compile properly using KAWA with JDK 1.3
its full off errors!
0
 
LVL 4

Accepted Solution

by:
sanjay_thakur earned 800 total points
ID: 6997406
Hi,

This is a tested code

import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class Test   {

    protected JFrame JFrame2 ;
    protected JRootPane JRootPane4 ;
    protected JPanel glassPane6 ;
    protected JLayeredPane JLayeredPane6 ;
    protected JPanel contentPane6 ;
    protected JPanel JPanel3 ;
    protected JLabel JLabel7 ;
    protected JLabel JLabel8 ;
    protected JLabel JLabel9 ;
    protected JButton JButton7 ;
    protected JButton JButton8 ;
    protected JButton JButton9 ;
    protected JTextField JTextField7 ;
    protected JTextField JTextField8 ;
    protected JTextField JTextField9 ;
    protected JPanel JPanel4 ;
    protected JButton JButton10 ;

    public  Test(){
        super();

        JFrame2 = new JFrame();
        JRootPane4 = JFrame2.getRootPane();
        glassPane6 = (JPanel)JRootPane4.getGlassPane();
        JLayeredPane6 = JRootPane4.getLayeredPane();
        contentPane6 = (JPanel)JRootPane4.getContentPane();
        JPanel3 = new JPanel();
        JLabel7 = new JLabel();
        JLabel8 = new JLabel();
        JLabel9 = new JLabel();
        JButton7 = new JButton();
        JButton8 = new JButton();
        JButton9 = new JButton();
        JTextField7 = new JTextField();
        JTextField8 = new JTextField();
        JTextField9 = new JTextField();
        JTextField9.setText("This is just a demo");
        JPanel4 = new JPanel();
        JButton10 = new JButton();
        JFrame2.setTitle( "JFrame2" );
        contentPane6.setLayout( new java.awt.BorderLayout() );
        JPanel3.setLayout( new java.awt.GridBagLayout() );
        JLabel7.setText( "JLabel7" );
        JLabel8.setText( "JLabel8" );
        JLabel9.setText( "JLabel9" );
        JButton7.setText( "JButton7" );
        JButton8.setText( "JButton8" );
        JButton9.setText( "JButton9" );
        {
            GridBagConstraints grid = new GridBagConstraints();
            grid.insets = new Insets(5,12,5,12);
            JPanel3.add(JLabel7, grid, -1);
            grid.gridx = 0;
            grid.gridy = 1;
            JPanel3.add(JLabel8, grid, -1);
            grid.gridy = 2;
            JPanel3.add(JLabel9, grid, -1);
            grid.gridx = 2;
            grid.gridy = 0;
            JPanel3.add(JButton7, grid, -1);
            grid.gridy = 1;
            JPanel3.add(JButton8, grid, -1);
            grid.gridy = 2;
            JPanel3.add(JButton9, grid, -1);
            grid.fill = GridBagConstraints.HORIZONTAL;
            grid.gridx = 1;
            grid.gridy = 1;
            grid.weightx = 1.0;
            JPanel3.add(JTextField7, grid, -1);
            grid.gridy = 0;
            JPanel3.add(JTextField8, grid, -1);
            grid.gridy = 2;
            JPanel3.add(JTextField9, grid, -1);
        }
        JPanel4.setLayout( new java.awt.GridBagLayout() );
        JButton10.setText( "JButton10" );
        {
            GridBagConstraints grid = new GridBagConstraints();
            grid.anchor = GridBagConstraints.SOUTHEAST;
            JPanel4.add(JButton10, grid, -1);
        }
        {
            String strConstraint;
            strConstraint = "Center";
            contentPane6.add(JPanel3, strConstraint, -1);
            strConstraint = "South";
            contentPane6.add(JPanel4, strConstraint, -1);
        }
        JFrame2.pack();
        JFrame2.show();

    }


    public static void main(String args[])
    {
      Test obj = new Test();
      obj.JFrame2.setSize(400,200);

    }


}

Also I think you should refer to various layout managers
used in Java

As in the example above the GridBagLayout
may look bit complex initially but it is the most flexible
one once you know how to use it


hope this helps
0
 
LVL 1

Expert Comment

by:raid999
ID: 6997686
Well these codes are really nice, but i remamber when i was new in JAVA i wanted something fast an easy if that what you are looking for you can try Forte from Sun it is a program were you can drag and drop items and learn wile doing that.

------------------------
Cheers
0
 
LVL 35

Expert Comment

by:girionis
ID: 8740738
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to sanjay_thakur

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question