-1

I have worked a bit with creating basic GUI's in java. Very simply, what I am trying to achieve, is a title in a given font, and below that a JPanel containing 5 different buttons equally spaced and sized, with a modifier function that allows for the resolution to be changed by a scale factor of say 0.75, the JPanel would not touch the edge of the screen, but have a border of 20 pixels away. What I would love to achieve is a way of the user being able to enter whatever resolution they want and for it to still keep the same basic design therefore making it compatible with other devices.

GUI design.

The code I have been using thus far is:

package prototype1;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GUI {


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

static public JFrame menu = new JFrame("Menu Screen");
static public double modifier = 1;
static public int width = 750;
static public int height = 1334;

static void stateManager(){

    JFrame.setDefaultLookAndFeelDecorated(true);

    AL demo = new AL();
    menu.setContentPane(demo.contentPanel1());

    menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    menu.setSize((int) (width * modifier),(int) (height * modifier));
    System.out.println((int)(width * modifier));
    menu.setVisible(true);
}


static class AL implements ActionListener{
    JLabel titleLabel;
    JButton checkStar;
    JPanel buttonScreenMenu;

    public JPanel contentPanel1(){
        JPanel totalGUI = new JPanel();
        totalGUI.setLayout(null);
        totalGUI.setBackground(Color.white);

        int x = width;
        int y = height;

        titleLabel = new JLabel("Select what it is you would like to do!");
        titleLabel.setFont(new Font("Castellar",Font.PLAIN, (int) (18 * modifier)));
        titleLabel.setLocation(0,(int) (40 * modifier));
        titleLabel.setSize((int) (x * modifier),(int) (30 * modifier));
        titleLabel.setHorizontalAlignment(0);
        totalGUI.add(titleLabel);

        buttonScreenMenu = new JPanel();
        buttonScreenMenu.setLocation((int) (20 * modifier) , (int) (100 * modifier));
        buttonScreenMenu.setSize((int) ((x - 40) * modifier),(int) ((y - 120) * modifier));
        buttonScreenMenu.setBorder(BorderFactory.createLineBorder(Color.black));
        totalGUI.add(buttonScreenMenu);

        checkStar = new JButton("Work out a Star");
        checkStar.setLocation((int)(20 * modifier),(int)(20 * modifier));
        checkStar.setSize(buttonScreenMenu.getWidth() - 40, (int) (buttonScreenMenu.getHeight() / 4) - 40);
        checkStar.setBackground(Color.white);
        buttonScreenMenu.add(checkStar);



        return totalGUI;


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}


}

However I stopped here as I was not getting anywhere near the results I wanted. The output of this code looks as follows:

output GUI

How to fix the resolution problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Off of what cricket said, using absolute position will cause and give you many problems. I suggest you to look into using a layout manager such as box layout, which looks exactly same way you want to display your nodes. All you need to do is add spacing between the nodes. Which you can see how to do here.

Thus alternatively if you still want the users to enter in the dimensions of their frames. All you have to do is set the dimensions of your jframe and every thing will automatically adjust itself.

Community
  • 1
  • 1
Javant
  • 1,009
  • 10
  • 17
  • I have used them in the past however this is for me A-Level coursework and I feel like it would make a big difference if I was able to code it myself rather than using a layout manager, but if that is the only option I will just use that. – Sam Brotherton Aug 02 '16 at 13:05
  • (1+) for using layout managers, although you should look into a GridLayout as this will provide equal sizing of each component. See the Swing tutorial on [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for working examples. – camickr Aug 02 '16 at 15:22