0

Hey my course requires me to do some applet stuff that I'm unfamiliar with and I'm trying to position my button to a certain position on the screen. I can get the size of the button to whatever I like but not the x and y position.

public void init() {

    button1 = new Button ("HIT TARGET");

    button1.setPreferredSize(new Dimension(100, 100));

    add(button1);

    button1.setLocation(300, 300);

    button1.addActionListener(this);
}

I've tried to search for my course document and google about this but I haven't found anything.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer Mar 12 '18 at 03:14
  • 1
    *my course requires me to do some applet stuff* Your course is teaching you things you would be better off not knowing. Ask for a refund. Applets are dead. – Elliott Frisch Mar 12 '18 at 03:29
  • I know :( but it's mandatory for school so I have to Edit: I can choose to do other programs if I wish but the exams are for Java Applets – Khan Niekamp Mar 12 '18 at 03:41
  • @MadProgrammer I only had a quick glance at it but from what I could see the Containers choose the layout for me is there another way that I can position my buttons to where I want them?? – Khan Niekamp Mar 12 '18 at 03:45
  • 1
    `from what I could see the Containers choose the layout for me` - there is a default layout manager for a JPanel, but you can easily change the layout manager. The tutorial has plenty of working examples for you do download and understand. Then apply the knowledge to your project. – camickr Mar 12 '18 at 04:00
  • Ahh yeah I see, thank you – Khan Niekamp Mar 12 '18 at 04:54
  • *"..is there another way that I can position my buttons to where I want them?"* Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). *"it's mandatory for school"* Please refer the teacher to .. – Andrew Thompson Mar 12 '18 at 06:36
  • .. [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Mar 12 '18 at 06:36
  • Also .. AWT?!? Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. [Note - that's a copy/paste comment. Take it as further evidence that your instructors are incompetent & ripping you off.] – Andrew Thompson Mar 12 '18 at 06:41

1 Answers1

0

You can use there setBounds() method and also set the setLayout(null) method as null. You can also refer my code to understand how to use:

public class Ass extends Applet
{ 
    String msb="";
    Button btn;
    public void init()
    {
        btn=new Button("BUTTON");
        setLayout(null);
        btn.setBounds(30,60,100,20);
        add(btn);
    }
}
Nick
  • 3,691
  • 18
  • 36