0

I've got a school project I'm working on. I'm trying to get my Swing application to match the picture they gave me. The problem is that I have no clue on how to move the components. All the ways I've tried have either not worked or made things worse.

package guiprjct;

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {

    JButton button1 = new JButton("Button");
    JTextArea area = new JTextArea(10,19);
    JCheckBox box = new JCheckBox("Checkbox");
    JTextField field = new JTextField("TextField");
    JScrollBar bar = new JScrollBar();
    JFrame frame = new JFrame();

    public static void main(String[]args) {
        new GUI();
    }

    public GUI() {
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setTitle("Frame example");

        JPanel pnl = new JPanel();

        pnl.add(button1);
        pnl.add(area);
        pnl.add(box);
        pnl.add(field);
        pnl.add(bar);

        frame.add(pnl);
        frame.setVisible(true);
    }
}

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MrGamma195
  • 35
  • 1
  • 2
  • 9
  • Please indent the code properly before asking a question. It saves time for the one(s) that can answer it. – Emz Dec 06 '14 at 09:01
  • It is also a bit unclear of what you exactly want to do. Could you clarfiy? – Emz Dec 06 '14 at 09:05
  • Maybe a look at the [swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) will help you. – Paco Abato Dec 06 '14 at 09:11
  • @Emz yeah sorry i forgot to add the picture but basically i need this applet to look like the picture and i cant get the components like the button and the rest to line up like the picture at all – MrGamma195 Dec 06 '14 at 09:11
  • @PacoAbato sorry no it didnt thats what i was given to try and figure this out but it didnt help me – MrGamma195 Dec 06 '14 at 09:12
  • @PacoAbato so you want to change the x and y positions of the GUI elements? – Emz Dec 06 '14 at 09:20
  • @Emz yes thats what i need to do – MrGamma195 Dec 06 '14 at 09:21
  • Look at the tutorials on Swing LayoutManager. This is the correct and easy way to do such thing. By all means, stay away from `setLocation/setSize/setBounds`. – Guillaume Polet Dec 06 '14 at 09:44
  • 1
    *"I'm trying to get my Swing application to match the picture they gave me."* Whoever gave you that image, smack them upside the head for me. A `JTextField` is a single line entry component that should **look** like a single line component. The `JScrollBar` does not seem to provide any useful purpose and is not aligned with the text area. The `JButton` is unnecessarily square. In short, it is a confusing and horrid user interface, and forcing people to try and reproduce it is an exercise in learning bad habits. – Andrew Thompson Dec 06 '14 at 10:22
  • @AndrewThompson that would be my teacher I've gotten such a migraine try to figure this thing out – MrGamma195 Dec 06 '14 at 10:45

2 Answers2

1

I dare your teacher to make that layout work on MacOS, Linux, Solaris, Windows 7 and Windows 8 without it look (even) more crap

Layout this

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class TestLayout40 {

    public static void main(String[] args) {
        new TestLayout40();
    }

    public TestLayout40() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(8, 8, 8, 8));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 2;
            gbc.ipadx = 30;
            gbc.ipady = 30;
            gbc.anchor = GridBagConstraints.LINE_START;
            add(new JButton("Button"), gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.gridwidth = 2;
            gbc.ipady = 30;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = new Insets(0, 15, 0, 0);
            add(new JTextField("TextField", 15), gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 1;
            add(new JScrollBar(), gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 2;
            gbc.ipadx = 30;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(15, 0, 0, 0);
            add(new JScrollPane(new JTextArea(5, 10)), gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridwidth = 2;
            gbc.weighty = 1.0;
            add(new JLabel("label"), gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 3;
            gbc.gridy = 1;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.weightx = 1.0;
            add(new JCheckBox("Checkbox"), gbc);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

<Component>.setBounds(_x_, _y_, _width_, _height_);

Will change the x, y position of a GUI element. It is also possible to resize it.

More can be read here

https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

Emz
  • 1,280
  • 1
  • 14
  • 29
  • i was actually just reading on this and getting ready to try it – MrGamma195 Dec 06 '14 at 09:54
  • your explanation helped me a lot better than the websites thanks – MrGamma195 Dec 06 '14 at 10:16
  • 2
    Java GUIs have to work on different OS', screen size, screen resolution etc. 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). – Andrew Thompson Dec 06 '14 at 10:18
  • @AndrewThompson this I understand but this method will have to do until i can get an understanding of the other layout managers its just a school assignment so its nothing major – MrGamma195 Dec 06 '14 at 10:34
  • 1
    @MrGamma195 *"its just a school assignment so its nothing major"* - I can't believe you just said that, seriously. This is why its impossible to be fine decent developers these days. Perfect practice makes perfect – MadProgrammer Dec 06 '14 at 10:44
  • @MadProgrammer I see why you can disagree with me but I've not followed one thing they've wanted me to do I've been taking this class for over a year now they taught no ethics how to properly debug when i took the entry class to html it was more informed than this all in all this has to be the worst programming class I've taken. – MrGamma195 Dec 06 '14 at 10:50
  • @AndrewThompson "Doing Without a Layout Manager (Absolute Positioning)", then I see no alternative than to manually set the x and y coordinates. I do however agree on MadProgrammer as well. A task should be taken seriously no matter, there are no shortcuts. – Emz Dec 06 '14 at 10:55
  • *"..then I see no alternative.."* I linked to two answers that **gave an alternative.** But if you are that unimaginative, and unable to follow links and understand what you read at the other end, I suspect you'll never be a competent programmer. – Andrew Thompson Dec 06 '14 at 11:20