-1

Please look below for Edits.

So I've looking over numerous "solutions" to fix my problem, but I just can't seem to get it working.

This is what my application looks like with the code below:

enter image description here

Basically, I want to set the location of a button, but I can't manage to do so. Here is my code:

package me.cervinakuy.application;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ControlPanel3 extends JFrame {

    JPanel panel = new JPanel();

    JButton startRobo = new JButton();
    JButton stopRobo = new JButton();
    JButton restartRobo = new JButton();

    public ControlPanel3() {

//      setLayout(null);
        setSize(1000, 700);
        setResizable(false);
        setLocation(450, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(45, 48, 55));
        setTitle("Espin Software | Control Panel");
        setVisible(true);

        startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png")));
        stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png")));
        restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png")));

        startRobo.setBorder(null);
        stopRobo.setBorder(null);
        restartRobo.setBorder(null);

        startRobo.setLocation(100, 100);

        panel.add(startRobo);
        panel.add(stopRobo);
        panel.add(restartRobo);
        panel.setOpaque(false);

        add(panel);

        validate();

    }

}

EDIT: I have now managed to create a GUI of what I was initially looking for, however, I have a new problem. Buttons are now pressable from different parts of the GUI, rather than only on the image. For those interested, here is what I have been able to accomplish:

New GUI look.

Updated Code:

package me.cervinakuy.application;

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ControlPanel3 extends JFrame {

    JPanel panel = new JPanel();

    JButton startRobo = new JButton();
    JButton stopRobo = new JButton();
    JButton restartRobo = new JButton();

    public ControlPanel3() {

//      setLayout(null);
        setSize(1000, 700);
        setResizable(false);
        setLocation(450, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(45, 48, 55));
        setTitle("Espin Software | Control Panel");
        setVisible(true);

        startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png")));
        stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png")));
        restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png")));

        startRobo.setBorder(null);
        stopRobo.setBorder(null);
        restartRobo.setBorder(null);

        panel.setLayout(null);
        startRobo.setLocation(200, 200);
        startRobo.setBounds(5, -95, 300, 300);
        stopRobo.setBounds(5, 0, 300, 300);
        restartRobo.setBounds(5, 95, 300, 300);

        panel.add(startRobo);
        panel.add(stopRobo);
        panel.add(restartRobo);
        panel.setOpaque(false);

        add(panel);

        validate();

    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Espin
  • 135
  • 1
  • 1
  • 10
  • 3
    *"I've looking over numerous "solutions" to fix my problem"* Link to the top 5 & explain why they a) failed to help, & b) failed to convince you that setting the explicit positions of components was counter productive. – Andrew Thompson Sep 13 '16 at 01:05
  • 1
    1) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Sep 13 '16 at 01:06
  • I already tried the first 5 that appeared when I titled my question, however the first few were not related to what I'm trying to achieve, and others simply made my application screen blank. When I say I've looked over numerous solutions, I have. The intended layout is fairly simple, just aligned to the left. However, for some reason they’re all centered and put to the very top. I am trying to figure out how to set the location so they’re no longer where they are, hence, the purpose of this question. – Espin Sep 13 '16 at 01:16
  • So where is the ASCII art/drawing? A picture paints a thousand words, whereas your words don't even tell me if the buttons should be in a row or column. – Andrew Thompson Sep 13 '16 at 02:05
  • E.G. 'aligned left' might be interpreted like [this](http://i.stack.imgur.com/zU6qh.png) - where they are aligned left in a single row at the top of the GUI. – Andrew Thompson Sep 13 '16 at 02:19
  • 1
    ***"Buttons are now pressable from different parts of the GUI, rather than only on the image."*** Gee what a surprise. This is the type of crap that happens when using `null` layouts. Or at least, the tip of the iceberg. – Andrew Thompson Sep 13 '16 at 02:33

1 Answers1

3

enter image description here

There are typically a number of ways to layout components that end with the same effect. In this example, we use a panel to contain the buttons in a column (buttonContainer using a GridLayout) then a panel to restrict that container to the top (buttonConstrainPanel using a BorderLayout) then a container to put that panel on the left (ui with BorderLayout).

It could also be achieved using a single GridBagLayout or a GroupLayout, though the logic of achieving it might not be as simple.

The focus border seen on the blue button indicates the limits of where a mouse click would activate the button.

import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ThreeButtonAlignedLeft {

    private JComponent ui = null;
    private String prefix = "https://i.stack.imgur.com/";
    private String[] suffix = {"gJmeJ.png","T5uTa.png","wCF8S.png"};

    ThreeButtonAlignedLeft() {
        try {
            initUI();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    public void initUI() throws MalformedURLException {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JPanel buttonContainer = new JPanel(new GridLayout(0, 1, 5, 5));
        for (int ii=0; ii<suffix.length; ii++) {
            JButton b = new JButton(new ImageIcon(new URL(prefix + suffix[ii])));
            b.setBorderPainted(false);
            b.setMargin(new Insets(0,0,0,0));
            b.setContentAreaFilled(false);
            buttonContainer.add(b);
        }
        JPanel buttonConstrainPanel = new JPanel(new BorderLayout(0, 0));
        buttonConstrainPanel.add(buttonContainer, BorderLayout.PAGE_START);

        ui.add(buttonConstrainPanel, BorderLayout.LINE_START);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ThreeButtonAlignedLeft o = new ThreeButtonAlignedLeft();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433