Solved

Java Swing is going everywhere but where its supposed to

Posted on 2004-05-01
2
354 Views
Last Modified: 2013-11-23
I can seem to get my GUI to look the way I want it to, here is a pic of the GUI Im getting
http://www.81x.com/Authors/roadragedave/badgui.jpg

and here is a pic of what I want it to look like
http://www.81x.com/Authors/roadragedave/wanted%20gui.jpg

Here is the code::

***************************************************************
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import java.beans.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;

//import com.college.attempt.cpp;

//import com.emc.college.ProgramActionListener;

public class ProgramStartup extends JFrame
{
    private Window_actionAdapter my_actionListener = null;

    JPanel jPanel1             = new JPanel();
      JPanel buttonPanel       = new JPanel();
      JPanel checkPanel       = new JPanel();
      JPanel textPanel       = new JPanel();

    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();

    JTextField jTextField1             = new JTextField();
    JTextField jTextField2             = new JTextField();
    JTextField jPasswordField1       = new JPasswordField();

    JLabel jTextPane1 = new JLabel();
    JCheckBox jCheckBox1 = new JCheckBox();
      JCheckBox jCheckBox2 = new JCheckBox();
      JCheckBox jCheckBox3 = new JCheckBox();
      JButton jButton1        = new JButton();
      JButton jButton2        = new JButton();

      public ProgramStartup ()
    {
        my_actionListener = new Window_actionAdapter();
        createGui();
    }

    private void createGui()
    {

        jPanel1.setBounds(new Rectangle());
        this.getContentPane().setLayout(new BorderLayout());
        this.setTitle("Guineyd WMI Application");
        this.setBounds(100, 100, 600, 400);
        this.setResizable(false);

        jLabel1.setText("Machine Name");
        jLabel1.setBounds(new Rectangle(144, 137, 89, 25));

        jLabel2.setText("Username");
        jLabel2.setBounds(new Rectangle(144, 196, 63, 23));
 
        jLabel3.setText("Password");
        jLabel3.setBounds(new Rectangle(425, 196, 66, 23));

        jTextField1.setPreferredSize(new Dimension(80, 21));
        jTextField1.setBounds(new Rectangle(144, 161, 382, 23));

        jTextField2.setPreferredSize(new Dimension(80, 21));
        jTextField2.setBounds(new Rectangle(144, 218, 112, 21));

        jPasswordField1.setPreferredSize(new Dimension(70, 21));
        jPasswordField1.setBounds(new Rectangle(425, 216, 98, 21));

        jTextPane1.setText("Please tick the values you wish to display");
        jTextPane1.setBounds(new Rectangle(44, 160, 238, 24));

        jCheckBox1.setText("Available Memory");
        jCheckBox1.setBounds(new Rectangle(44, 185, 118, 24));

        jCheckBox2.setText("Load Percentage");
        jCheckBox2.setBounds(new Rectangle(44, 210, 118, 24));

      jCheckBox3.setText("Available Disk Space");
      jCheckBox3.setBounds(new Rectangle(44, 235, 118, 24));

      jButton1.setBounds(new Rectangle(94, 277, 128, 30));
      jButton1.setText("Connect");
      jButton1.addActionListener(my_actionListener);
      jButton1.setActionCommand("CONNECT_CLICK");

      jButton2.setBounds(new Rectangle(255, 277, 128, 30));
      jButton2.setText("Cancel");
      jButton2.addActionListener(my_actionListener);
      jButton2.setActionCommand("CANCEL_CLICK");

      buttonPanel.add(jButton1, null);
      buttonPanel.add(jButton2, null);

      checkPanel.setLayout(new BorderLayout());
      checkPanel.setBounds(new Rectangle(100, 300, 250, 120));
      checkPanel.add(jTextPane1, BorderLayout.NORTH);
      checkPanel.add(jCheckBox1, BorderLayout.WEST);
      checkPanel.add(jCheckBox2, BorderLayout.WEST);
      checkPanel.add(jCheckBox3, BorderLayout.WEST);

      textPanel.setBounds(new Rectangle(100, 100, 471, 180));
      textPanel.add(jLabel1,                      null);
      textPanel.add(jTextField1,             null);
      textPanel.add(jLabel2,                      null);
      textPanel.add(jTextField2,             null);
      textPanel.add(jLabel3,                 null);
      textPanel.add(jPasswordField1,       null);

      this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
      this.getContentPane().add(checkPanel,  BorderLayout.CENTER);
      this.getContentPane().add(textPanel,   BorderLayout.NORTH);
    }

    public static void main (String [] args)
    {
        ProgramStartup thisWindow = new ProgramStartup();
        thisWindow.setVisible(true);

        thisWindow.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }

    // ----------------- ACTION LISTENER --------------------

    class Window_actionAdapter implements ActionListener
    {
        Window_actionAdapter()
        {
        }

        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand().equals("CANCEL_CLICK"))
            {
                // do the button stuff
                System.out.println("David says go home");
                System.exit(0);
            }
            else if(e.getActionCommand().equals("CONNECT_CLICK"))
            {
      JNIConnect jniConn = new JNIConnect();
      System.out.println("David is Cool");
      Login();

            }
        }
    }

    //------------------ CLASS OF VARIABLES -----------------

   class MachineDetails {
      String BSTRMachineName;
      String UserName;
      String PassWd;
      boolean AvailMemory;
      boolean LoadPercentage;
      boolean DiskSpaceFree;

   public MachineDetails(){
      String BSTRMachineName = null;
      String UserName = null;
      String PassWd   = null;
      boolean AvailMemory = false;
      boolean LoadPercentage = false;
      boolean DiskSpaceFree = false;
      }
   }

   public void Login(){
      MachineDetails dClass = new MachineDetails();
      //JNIConnect jniConn = new JNIConnect();

      dClass.BSTRMachineName = jTextField1.getText();
      dClass.UserName          = jTextField2.getText();
      dClass.PassWd                = jPasswordField1.getText();

      dClass.AvailMemory     = jCheckBox1.isSelected();
      dClass.LoadPercentage  = jCheckBox2.isSelected();
      dClass.DiskSpaceFree   = jCheckBox3.isSelected();

      driver(dClass);
    }

    public class JNIConnect{
      public JNIConnect()
      {
      try{
                                System.loadLibrary("guineyd");
      }
            catch(java.lang.UnsatisfiedLinkError e){
            System.out.println(e);
      }
      }

      }
       public native void driver(MachineDetails dClass) throws UnsatisfiedLinkError;;
}

*******************************************************************

Im looking at the co-ordinates for each of the checkBoxes and I can understand why its ending up that way

0
Comment
Question by:vassim
2 Comments
 
LVL 9

Accepted Solution

by:
keteracel earned 500 total points
Comment Utility
Ok, since you're not allowing the window to be resized we can position the elements absolutely. This is not the best way to do it (especially if resizing is an option) but is certainly the quickest for this simple dialog.....


********************************************************
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

//import com.college.attempt.cpp;

//import com.emc.college.ProgramActionListener;

public class ProgramStartup extends JFrame {
      private Window_actionAdapter my_actionListener = null;

      JLabel jLabel4 = new JLabel();

      JLabel jLabel1 = new JLabel();
      JLabel jLabel2 = new JLabel();
      JLabel jLabel3 = new JLabel();

      JTextField jTextField1 = new JTextField();
      JTextField jTextField2 = new JTextField();
      JTextField jPasswordField1 = new JPasswordField();

      JLabel jTextPane1 = new JLabel();
      JCheckBox jCheckBox1 = new JCheckBox();
      JCheckBox jCheckBox2 = new JCheckBox();
      JCheckBox jCheckBox3 = new JCheckBox();
      JButton jButton1 = new JButton();
      JButton jButton2 = new JButton();

      public ProgramStartup() {
            my_actionListener = new Window_actionAdapter();
            createGui();
      }

      private void createGui() {
            this.setTitle("Guineyd WMI Application");
            this.setSize(590, 340);
            this.setResizable(false);
            this.getContentPane().setLayout(null);

            jLabel1.setText("Machine Name");
            jLabel1.setLocation(30, 20);
            jLabel1.setSize(500, 20);
            
            jTextField1.setLocation(30, 41);
            jTextField1.setSize(500, 20);
            
            jLabel2.setText("Username");
            jLabel2.setLocation(30, 80);
            jLabel2.setSize(150, 20);

            jTextField2.setLocation(30, 101);
            jTextField2.setSize(150, 20);
            
            jLabel3.setText("Password");
            jLabel3.setLocation(380, 80);
            jLabel3.setSize(150, 20);

            jPasswordField1.setLocation(380, 101);
            jPasswordField1.setSize(150, 20);
            
            jLabel4.setText("Please tick the values you wish to display");
            jLabel4.setLocation(30, 170);
            jLabel4.setSize(500, 20);
            
            jCheckBox1.setText("Available Memory");
            jCheckBox1.setLocation(30, 191);
            jCheckBox1.setSize(500, 20);
            
            jCheckBox2.setText("Load Percentage");
            jCheckBox2.setLocation(30, 212);
            jCheckBox2.setSize(500, 20);
            
            jCheckBox3.setText("Available Disk Space");
            jCheckBox3.setLocation(30, 233);
            jCheckBox3.setSize(500, 20);

            jButton1.setText("Connect");
            jButton1.addActionListener(my_actionListener);
            jButton1.setActionCommand("CONNECT_CLICK");
            jButton1.setLocation(170, 273);
            jButton1.setSize(100, 20);

            jButton2.setText("Cancel");
            jButton2.addActionListener(my_actionListener);
            jButton2.setActionCommand("CANCEL_CLICK");
            jButton2.setLocation(290, 273);
            jButton2.setSize(100, 20);
            
            this.getContentPane().add(jLabel1);
            this.getContentPane().add(jTextField1);
            this.getContentPane().add(jLabel2);
            this.getContentPane().add(jTextField2);
            this.getContentPane().add(jLabel3);
            this.getContentPane().add(jPasswordField1);
            this.getContentPane().add(jLabel4);
            this.getContentPane().add(jCheckBox1);
            this.getContentPane().add(jCheckBox2);
            this.getContentPane().add(jCheckBox3);
            this.getContentPane().add(jButton1);
            this.getContentPane().add(jButton2);
      }

      public static void main(String[] args) {
            ProgramStartup thisWindow = new ProgramStartup();
            thisWindow.setVisible(true);

            thisWindow.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                        System.exit(0);
                  }
            });
      }

      // ----------------- ACTION LISTENER --------------------

      class Window_actionAdapter implements ActionListener {
            Window_actionAdapter() {
            }

            public void actionPerformed(ActionEvent e) {
                  if (e.getActionCommand().equals("CANCEL_CLICK")) {
                        // do the button stuff
                        System.out.println("David says go home");
                        System.exit(0);
                  } else if (e.getActionCommand().equals("CONNECT_CLICK")) {
                        JNIConnect jniConn = new JNIConnect();
                        System.out.println("David is Cool");
                        Login();

                  }
            }
      }

      //------------------ CLASS OF VARIABLES -----------------

      class MachineDetails {
            String BSTRMachineName;
            String UserName;
            String PassWd;
            boolean AvailMemory;
            boolean LoadPercentage;
            boolean DiskSpaceFree;

            public MachineDetails() {
                  String BSTRMachineName = null;
                  String UserName = null;
                  String PassWd = null;
                  boolean AvailMemory = false;
                  boolean LoadPercentage = false;
                  boolean DiskSpaceFree = false;
            }
      }

      public void Login() {
            MachineDetails dClass = new MachineDetails();
            //JNIConnect jniConn = new JNIConnect();

            dClass.BSTRMachineName = jTextField1.getText();
            dClass.UserName = jTextField2.getText();
            dClass.PassWd = jPasswordField1.getText();

            dClass.AvailMemory = jCheckBox1.isSelected();
            dClass.LoadPercentage = jCheckBox2.isSelected();
            dClass.DiskSpaceFree = jCheckBox3.isSelected();

            driver(dClass);
      }

      public class JNIConnect {
            public JNIConnect() {
                  try {
                        System.loadLibrary("guineyd");
                  } catch (java.lang.UnsatisfiedLinkError e) {
                        System.out.println(e);
                  }
            }

      }
      public native void driver(MachineDetails dClass)
            throws UnsatisfiedLinkError;
      ;
}

*************************************************************

keteracel...
0
 

Author Comment

by:vassim
Comment Utility
All I have to say is...............


I think I love you!!!!!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now