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.
.
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:
How to fix the resolution problem?