Ok so this thread is going to be used a lot because I have. A programming class and it wants us to do things before it introduces is to the code. The first command I need to know, how to command where an object is placed. Say you have a fram with a container and a panel. You have several buttons you want to place on this panel. You want to put them in specific spots however. A command like panel.add(button) will put it in the order but wont allow you to decide when to wrap the buttons to the next line. The code I need can also be explained like ti basic, I need the java equivalent of an output, where it controls where the object is placed on the "home screen".
I am afraid to say the only way I can explain this is in C#, this is how I would do it, assuming I only have notepad available (which is how I do it anyway)


Code:
class blah :Form  {   // class that inherits from Forms

Button btn = new Button()  // creates a button object

public blah {           // class blah's constructor method

Size = new Size(250, 250);         // makes the form size 250 by 250 pixels

btn.Location = new Point(30,30);   // changes the button location to 30 pixels in and 30 pixels down

Controls.Add(btn);  // adds button to form

     }     //end to method
}     // end to class


this is how I would do it in C#, note it is missing some things like using statements and stuff, but it is just the basic idea of how to make a button, change the point on the form, and then add it to the form, I just posted this in hopes it will help as I know C# and java are slightly similar


note: anyone who is more experienced then me, just tell me if I am correct, I am new at this helping people with programming thing `-`[/code]
I'll try the .location and see if I get any errors. I can get the idea of what you are saying. I also feel like I should sy that I'm programming java from bluej, just incase that changes anything.
JPFowl, are you using AWT or Swing? I hope Swing, because I'm pretty sure that AWT is deprecated.
I'm taking an introductory course in programming so I don't really know what the difference is between the different things like swing and awt. This is my code.

Code:

/**
 * This program is for Computer Programming 1.
 * This will create a GUI window that promts the user for two points in (x,y) format.
 * When the user clicks calculate (button) the program will display
 * the distance, midpoint, slope, and the equation of a line in slope-
 * intercept form.
 *
 * @author JPfowl
 * @version 1
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Two_Point_Solver implements ActionListener
{
    JPanel M;
    JFrame F;
    Container T;
    JLabel DESC,L1S1,L1S2,L2S1,L2S2,XL,YL,AL,BL,DAL,MAL,MP1L,MP2L,SAL,EAL,YEL,XPL,DDL,MD1L,MD2L,SDL,YDL,BDL,L3S2,L4S2,L5S2,L6S2;
    JButton RUN;
    JTextField XT,YT,AT,BT;
    Double DD,SD,BD;
    int X,Y,A,B;
    Double T2,MP1D,MP2D;
    String XS,YS,AS,BS;
   
    public Two_Point_Solver()
    {
        F = new JFrame("Quadratic Formula Calculator");
        F.setBounds(200,300,400,500);
        T = F.getContentPane();
        M = new JPanel();
        DESC = new JLabel("Pt. 1: (x,y);Pt. 2: (a.b)");
        XL = new JLabel("X=?");
        YL = new JLabel("Y=?");
        AL = new JLabel("A=?");
        BL = new JLabel("B=?");
        DAL = new JLabel("Distance--> ");
        DDL = new JLabel("");
        MAL = new JLabel("Mid Point--> (");
        MD1L = new JLabel("");
        MP1L = new JLabel(";");
        MD2L = new JLabel("");
        MP2L = new JLabel(")");
        SAL = new JLabel("Slope--> ");
        SDL = new JLabel("");
        EAL = new JLabel("Equation of a line--> ");
        YEL = new JLabel("Y=");
        YDL = new JLabel("");
        XPL = new JLabel("X+");
        BDL = new JLabel("");
        XT = new JTextField(4);
        YT = new JTextField(4);
        AT = new JTextField(4);
        BT = new JTextField(4);
        RUN = new JButton("Calculate");
        RUN.addActionListener(this);
        T2 = 2.0;
        T.add(M);
        M.add(DESC);
        M.add(XL);
        M.add(XT);
        M.add(YL);
        M.add(YT);
        M.add(AL);
        M.add(AT);
        M.add(BL);
        M.add(BT);
        M.add(RUN);
        M.add(DAL);
        M.add(DDL);
        M.add(MAL);
        M.add(MD1L);
        M.add(MP1L);
        M.add(MD2L);
        M.add(MP2L);
        M.add(SAL);
        M.add(SDL);
        M.add(EAL);
        M.add(YEL);
        M.add(YDL);
        M.add(XPL);
        M.add(BDL);
        F.show();
    }
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == RUN)
        {
            XS = XT.getText();
            YS = YT.getText();
            X = Integer.parseInt(XS);
            Y = Integer.parseInt(YS);
            AS = AT.getText();
            BS = BT.getText();
            A = Integer.parseInt(AS);
            B = Integer.parseInt(BS);
            DD = Math.sqrt((A-X)*(A-X)+(B-Y)*(B-Y));
            DDL.setText(""+DD);
            DDL.setForeground(new Color(0,120,0));
            MP1D = ((X+A)/(T2));
            MP2D = ((Y+B)/(T2));
            MP1L.setText(""+MP1D+":");
            MP2L.setText(""+MP2D+")");
        }
    }
}


By the way, the location command did not work. My teacher taught me to try a BorderLayout command but I can't figure out how it works. It is supposed to divide your panel into sections in which you add more panels, effectively setting where you output it.
Not sure how to solve your problem, but you should probably learn how to name your variables a little bit better.
They make sense to me and that's fine for now. I don't have a lot of time to write these programs so I need to keep things short. I will make the names better in the future if I program more but now I don't have time. That is not the point right now though.
JPfowl wrote:
...

By the way, the location command did not work. My teacher taught me to try a BorderLayout command but I can't figure out how it works. It is supposed to divide your panel into sections in which you add more panels, effectively setting where you output it.


http://download.oracle.com/javase/tutorial/uiswing/layout/border.html
KermMartian wrote:
JPFowl, are you using AWT or Swing? I hope Swing, because I'm pretty sure that AWT is deprecated.


Last time I checked, isn't Swing about to be deprecated as well?
lafferjm wrote:
JPfowl wrote:
...

By the way, the location command did not work. My teacher taught me to try a BorderLayout command but I can't figure out how it works. It is supposed to divide your panel into sections in which you add more panels, effectively setting where you output it.


http://download.oracle.com/javase/tutorial/uiswing/layout/border.html

That isn't how the border layout works according to my teacher. He said that I couture use it to divide my panel into sections vertically and horizontally based on numbers specified by the programmer. (2,3) for example, would split it into two collumns and 3 rows (or vice-versatile, that's what I Ned help with).
Then I think what you want is the gridlayout. http://download.oracle.com/javase/tutorial/uiswing/layout/grid.html
1.) You are using swing (stuff in the javax.swing package, which is basically all that stuff starting with J).

2.) Could you sketch a picture (or two) of what you application should look like? That would help a lot.

3.) There are a few ways to go about what you want to do:

The first is to try to interact with a layout manager and gui components each time something needs to be modified (Java layout managers always frustrate me, but you usually have to learn the quirks of one and use IT'S stuff to manage things, because they often flat-out ignore stuff like location and size and preferredSize of components).

The second is to have a method that, given values, will setup the components with a new layout manager according to what the user wants; and then when you need to change it, call the method with different values. It should first remove all the components, and then add them back in. Make sure to NOT add your class as an actionlistener each time (just once), or you might get duplicate command-requrests!

The third way to do it is to set your layoutManager to null - This allows you to place and size all your components MANUALLY (which would be nice if you can code it exactly how you want; though you might have to take into account the sizes, positions, and boundaries of all components).

The fourth way is to add just the necessary components (input fields and buttons), and then add a Canvas and get the Graphics object from it, and just DRAW everything else (rather than using labels) -- this way is probably not what your class is asking for though.

@Ashbad: Swing is going to be deprecated!? What is going to be the new setup, and what's the difference? ... (oy, java certainly needs to have ANOTHER layer of gui stuff added Smile )
[img]
http://www.flickr.com/photos/jpfowl/6201657672/
[/img]
the text are JLabels, the black boxes are textfields, the run thing is a button and the red boxes are hidden answer fields.
Ashbad wrote:
KermMartian wrote:
JPFowl, are you using AWT or Swing? I hope Swing, because I'm pretty sure that AWT is deprecated.


Last time I checked, isn't Swing about to be deprecated as well?
Oh really? What are they replacing it with, then? I definitely agree about variable names. Unlike TI-BASIC, you don't get (much of) a speed/size penalty for using descriptive variable names, and using less descriptive names is frowned upon.
Actually, never mind about the Swing being Deprecated thing. I heard it from a Java profession at NAIC (from my robotics thing) last year, but obviously it was just a rumor since that was a year ago, and I can't find any info about it online after quite a few (minutes) of researching. Perhaps he was just making a logical guess, since each Java GUI framework made before Swing seemed to have a very short half-life.

Also, I didn't know longer names in Java even had anything to do with speed/size -- I assumed it was like C, where names have nothing to with with access/operation speed on variables, and all things having to do with names are thrown out after compilation. How much of a speed difference does it make? Not that I'm really concerned (I just want to learn something more about the subject), since I'll continue using long names in Java for APCS -- It's not like I'm ever going to use Java for a speed-intensive project anyways.
KermMartian wrote:


Unlike TI-BASIC, you don't get (much of) a speed/size penalty for using descriptive variable names, and using less descriptive names is frowned upon.



I mean I usually have 40 minutes to write a program. Can you check my code for now; I decided not to worry about the border for now (but I still want to know how to use it to divide my panel) but the program isn't working mathematically. The distance and midpoint work fine regardless, but the last tow functions only work if it is withing in 3 points in X and Y direction.


Code:


/**
 * This program is for Computer Programming 1.
 * This will create a GUI window that promts the user for two points in (x,y) format.
 * When the user clicks calculate (button) the program will display
 * the distance, midpoint, slope, and the equation of a line in slope-
 * intercept form.
 *
 * @author JPfowl
 * @version 1
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Two_Point_Solver implements ActionListener
{
    JPanel M;
    JFrame F;
    Container T;
    JLabel DESC,XL,YL,AL,BL,DAL,MAL,MP1L,MP2L,SAL,EAL,YEL,XPL,DDL,MD1L,MD2L,SDL,YDL,BDL,LB11A,LB11B;
    JButton RUN;
    JTextField XT,YT,AT,BT;
    Double DD;
    Double SD;
    Double BD;
    int X,Y,A,B;
    Double T2,MP1D,MP2D,o1;
    int SDA,SDB;
    Double O2;
    String XS,YS,AS,BS;
   
    public Two_Point_Solver()
    {
        F = new JFrame("Quadratic Formula Calculator");
        F.setBounds(200,300,400,500);
        T = F.getContentPane();
        M = new JPanel();
        DESC = new JLabel("                                        Pt. 1: (x,y);Pt. 2: (a.b)                                        ");
        XL = new JLabel("X=?");
        YL = new JLabel("Y=?");
        AL = new JLabel("A=?");
        BL = new JLabel("B=?");
        LB11A = new JLabel("                                          ");
        LB11B = new JLabel("                                          ");
        DAL = new JLabel("Distance--> ");
        DDL = new JLabel("                                                                                  ");
        MAL = new JLabel("Mid Point--> (");
        MD1L = new JLabel("                                        ");
        MP1L = new JLabel(";");
        MD2L = new JLabel("                                        ");
        MP2L = new JLabel(")");
        SAL = new JLabel("Slope--> ");
        SDL = new JLabel("                                                                        ");
        EAL = new JLabel("Equation of a line--> ");
        YEL = new JLabel("Y=");
        YDL = new JLabel("                          ");
        XPL = new JLabel("X+");
        BDL = new JLabel("                          ");
        XT = new JTextField(4);
        YT = new JTextField(4);
        AT = new JTextField(4);
        BT = new JTextField(4);
        RUN = new JButton("Calculate");
        RUN.addActionListener(this);
        T2 = 2.0000;
        O2 = -1.0;
        o1 = 1.0;
        T.add(M);
        M.add(DESC);
        M.add(XL);
        M.add(XT);
        M.add(YL);
        M.add(YT);
        M.add(AL);
        M.add(AT);
        M.add(BL);
        M.add(BT);
        M.add(LB11A);
        M.add(RUN);
        M.add(LB11B);
        M.add(DAL);
        M.add(DDL);
        M.add(MAL);
        M.add(MD1L);
        M.add(MP1L);
        M.add(MD2L);
        M.add(MP2L);
        M.add(SAL);
        M.add(SDL);
        M.add(EAL);
        M.add(YEL);
        M.add(YDL);
        M.add(XPL);
        M.add(BDL);
        F.show();
    }
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == RUN)
        {
            XS = XT.getText();
            YS = YT.getText();
            X = Integer.parseInt(XS);
            Y = Integer.parseInt(YS);
            AS = AT.getText();
            BS = BT.getText();
            A = Integer.parseInt(AS);
            B = Integer.parseInt(BS);
            DD = Math.sqrt((A-X)*(A-X)+(B-Y)*(B-Y));
            DDL.setText(""+DD+"                                               ");
            DDL.setForeground(new Color(0,120,0));
            MP1D = ((X+A)/(T2));
            MP2D = ((Y+B)/(T2));
            MP1L.setText(""+MP1D+":");
            MP1L.setForeground(new Color(0,120,0));
            MP2L.setText(""+MP2D+")");
            MP2L.setForeground(new Color(0,120,0));
            SDA = (B-Y);
            SDB = (A-X);
            SD = ((SDA/SDB)*o1);
            SDL.setText(""+SD);
            SDL.setForeground(new Color(0,120,0));
            YDL.setText(""+SD);
            YDL.setForeground(new Color(0,120,0));
            BD = (((SD*X)-Y)*O2);
            BDL.setText(""+BD);
            BDL.setForeground(new Color(0,120,0));
            YEL.setForeground(new Color(0,120,0));
            XPL.setForeground(new Color(0,120,0));
        }
    }
}

  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement