I want to achieve this look and trying to set the JPanel components accordingly but when I run the app, I see a different placement. This is the look I am looking for:
First of all, the order isn't correct and secondly, there are no spaces between the items so, in general, this is not what I want. How can I fix it to look similar to the image I first added? My code:
package Lab5;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class FontDesigner extends JFrame {
public FontDesigner() {
// JFrame
setSize(400, 400);
setTitle("Font Changer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// String
JLabel text = new JLabel("Change Me");
text.setFont(new Font("Arial", 1, 28));
// Radio Button
JRadioButton button1 = new JRadioButton("Small");
JRadioButton button2 = new JRadioButton("Medium");
JRadioButton button3 = new JRadioButton("Large");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(button1);
radioGroup.add(button2);
radioGroup.add(button3);
/*
* if (button1.isSelected()) {
* text.setSize(1);
* }
*/
// Combo Box
JComboBox combo = new JComboBox<>();
combo.addItem("Serif");
combo.addItem("Sans-Serif");
// String selectedFont = (String) combo.getSelectedItem();
// Check Box
JCheckBox checkBox1 = new JCheckBox("Italic");
JCheckBox checkBox2 = new JCheckBox("Bold");
ButtonGroup checkBoxGroup = new ButtonGroup();
checkBoxGroup.add(checkBox1);
checkBoxGroup.add(checkBox2);
// JPanel
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
panel1.setLayout(new BorderLayout());
panel2.setLayout(new BorderLayout());
panel3.setLayout(new BorderLayout());
panel4.setLayout(new BorderLayout());
panel1.add(text, BorderLayout.CENTER);
panel2.add(combo, BorderLayout.CENTER);
panel3.add(button1, BorderLayout.WEST);
panel3.add(button2, BorderLayout.CENTER);
panel3.add(button3, BorderLayout.EAST);
panel4.add(checkBox1);
panel4.add(checkBox2);
// Frame Configuration
// setLayout(new BorderLayout());
setLayout(new FlowLayout());
add(panel1);
add(panel2);
add(panel3);
add(panel4);
// Set Visibility
setVisible(true);
}
public static void main(String[] args) {
FontDesigner newFontDesigner = new FontDesigner();
}
}

Introduction
I rearranged your code to create the following GUI.
Explanation
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
All Swing applications must start with a call to the
SwingUtilitiesinvokeLatermethod. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.The first thing I noticed was that your GUI could be divided into four separate
JPanels. One to hold the text, one to hold the font selection, one to hold the style, and one to hold the size.So, I rearranged your code into four methods, with each method producing one of the
JPanels.Then, I decided the text
JPanelprobably should be in the center of theJFrame'sBorderLayout. So, I created a controlJPanelto display the bottom threeJPanelsusing anotherBorderLayout.Creating each
JPanelin a separate method allows me to try different Swing layout managers and see which ones I like the best. It also makes the code much easier for other people to read and understand.I modified the
JComboBoxto hold anAppFontinstance. This way, I can display the font family name in the combo box. When the user selects one of the options, you have aFontinstance to move to thecurrentFontfield.Code
Here's the complete runnable code. I made the additional class an inner class so I could post the code as one block.
Edited to add 1:
I went ahead and finished the GUI by adding an application model and all of the
ActionListeners. I made all theActionListenerslambda expressions, as I wasn't planning to provide the complete answer.To the OP, go ahead and finish the GUI on your own.
For everyone else:
I scanned the
GraphicsEnvironmentfor all the possible font families that would correctly display "Change Me". My computer has 218 font families that qualify. Your computer will probably have a different set of font families. The Dialog font should be present on most computers, so I made that the default.I modified the GUI somewhat. Here's the revised GUI.
Code 1
Here's the complete runnable code. I made the additional classes inner classes so I could post the code as one block.