0

I'm almost new to create GUI in Java. I used Jigloo a long time ago however, but I did a very simple GUI.

I need some guidance with creating a toolbar (e.g. File, options, help, etc) and a slider bar with an editbox showing its value? I'm almost familiar with labels, editbox.

In general, I want to build a GUI something like this:

enter image description here

And my simple code using jFrame is just as follows:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToolBar;

public class ControlPanel {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ControlPanel window = new ControlPanel();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ControlPanel() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
//      frame.sets
        frame.setBounds(100, 100, 1000, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JToolBar toolBar = new JToolBar();
        toolBar.setBounds(10, 11, 87, 16);
        frame.getContentPane().add(toolBar);

        JPanel panel = new JPanel();
        panel.setBounds(10, 38, 400, 100);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(10, 149, 400, 100);
        frame.getContentPane().add(panel_1);
        panel_1.setLayout(null);

        JSlider slider = new JSlider();
        slider.setBounds(0, 11, 400, 26);
        panel_1.add(slider);

        JPanel panel_2 = new JPanel();
        panel_2.setBounds(10, 260, 400, 100);
        frame.getContentPane().add(panel_2);

        JPanel panel_3 = new JPanel();
        panel_3.setBounds(10, 371, 400, 100);
        frame.getContentPane().add(panel_3);
    }

}
Tina J
  • 4,983
  • 13
  • 59
  • 125
  • 1
    You would do better by building the GUI yourself. A builder might show you what to look at but in the long run it's an inferior option. – user1803551 Oct 10 '14 at 23:13
  • Perhaps you want a [`JMenuBar`](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#create) rather than a `JToolBar` – Paul Samsotha Oct 10 '14 at 23:20
  • 2
    Here is an approach: for the menu ("File, Options, Help") [use a `JMenuBar`](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html). Create 4 panels with [titled borders](http://docs.oracle.com/javase/tutorial/uiswing/components/border.html) and stack them on top of each other with a [`GridLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html) (or a [`BoxLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html)). Inside the panels [`GridbagLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) will do it best. – user1803551 Oct 10 '14 at 23:22
  • Thanks. One of my main problems in general is with layouts. Grid or Box just stretches all of them towards the whole width...I'm using Absolue at this point which is also not good. – Tina J Oct 10 '14 at 23:58
  • `frame.getContentPane().setLayout(null);` 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). *"One of my main problems in general is with layouts"* Design a nested layout from 'the inside out'. For example, the top 3 titled border areas are essentially the same, so figure out how to layout one, then find ... – Andrew Thompson Oct 11 '14 at 00:43
  • .. another layout that allows us to stack a number of panels in a column (`GridLayout` springs to mind), then figure how to put that column of 3 with another component below them. For that, I'm thinking `BorderLayout` with the `NIBP` titled area at `PAGE_END`.. – Andrew Thompson Oct 11 '14 at 00:46

0 Answers0