Solved

simple interface

Posted on 2002-05-08
8
211 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
8 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 82
null output 3 42
Delete Android all or certain applications data programmatically 9 107
going to wrong jsp page 2 46
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

713 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