Java Swing - GridBagConstraints insets color - java

I'm having some troubles working with GridBagConstraints, specifically with the insets property.
I have two components, and I want some custom margin between theme. Now, what I did was using constraints.insets in this way:
polinomioConstraints.insets = new Insets(5, 0, 15 ,0);
checkboxesPanel.add(infoAggiunte, polinomioConstraints);
checkBoxesPanel.add(anotherString, constraintWithoutInsets);
The problem is that for some reason a white block appears in the insets area, so between the two components, and I found no way to change that white color to the actual panel's background

Here's a GUI I just did for someone else. I added the yellow background.
I don't see white in between the Swing components.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MainRegistrationPage {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainRegistrationPage();
}
});
}
private JTextField firstname;
private JTextField lastname;
private JTextField email;
private JPasswordField password;
private JFrame frame;
public MainRegistrationPage() {
// create user interface
frame = new JFrame("Registration Page");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createRegistrationPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createRegistrationPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
mainPanel.setBackground(Color.YELLOW);
Font font = new Font("Georgia", Font.PLAIN, 14);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("First Name:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
firstname = new JTextField(30);
firstname.setFont(font);
mainPanel.add(firstname, gbc);
gbc.gridx = 0;
gbc.gridy++;
label = new JLabel("Last Name:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
lastname = new JTextField(30);
lastname.setFont(font);
mainPanel.add(lastname, gbc);
gbc.gridx = 0;
gbc.gridy++;
label = new JLabel("Email:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
email = new JTextField(30);
email.setFont(font);
mainPanel.add(email, gbc);
gbc.gridx = 0;
gbc.gridy++;
label = new JLabel("Password:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
password = new JPasswordField(30);
password.setFont(font);
mainPanel.add(password, gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy++;
JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
}
});
mainPanel.add(submit, gbc);
return mainPanel;
}
}

Related

how can i center Label in frame Swing?

I want to make the copyright in the image below in the center of the frame.
I tried to add SwingConstants.CENTER and Component.CENTER_ALIGNMENT but neither worked.
Can someone please help me with this one? Thanks in advance.
enter image description here
This is my code:
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JTextField text1;
JTextField text2;
JLabel label1;
JLabel label2;
JButton encrypt;
JButton decrypt;
JLabel copyright;
//constructor
public MyFrame(){
super("Encryption and Decryption");
setLayout(new FlowLayout()); //add first text field
label1 = new JLabel("enter a text: ");
add(label1);
text1= new JTextField(10);
add(text1);
//add second text field
label2 = new JLabel("result: ");
add(label2);
text2= new JTextField(10);
add(text2);
encrypt = new JButton("encrypt!");
add(encrypt);
decrypt = new JButton("decrypt!");
add(decrypt);
copyright = new JLabel(" © Project realized by Ayoub Touti ");
add(copyright);
//organizing elements in a panel
JPanel panel = new JPanel(new GridLayout(4,2,12,6));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(encrypt);
panel.add(decrypt);
panel.add(copyright);
add(panel);
}
}
//////////////////////////////
import javax.swing.*;
public class main {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (500, 200);
frame.setResizable(true);
frame.setVisible (true);
frame.setLocationRelativeTo(null); // to open the window in the main screen
}
}
Use a GridBagLayout (or a combination of layouts through compound components, slightly demonstrated below)
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(32, 32, 32, 32));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = gbc.LINE_END;
gbc.insets = new Insets(0, 8, 0, 8);
add(new JLabel("enter a text:"), gbc);
gbc.gridy++;
add(new JLabel("result:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = gbc.LINE_START;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
JPanel actionPane = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = gbc.HORIZONTAL;
gbc.insets = new Insets(0, 0, 0, 0);
actionPane.add(new JButton("encyprt!"), gbc);
gbc.gridx++;
actionPane.add(new JButton("decyprt!"), gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.gridy = 2;
add(actionPane, gbc);
gbc.gridy++;
add(new JLabel("© Project realized by Ayoub Touti"), gbc);
}
}
}
See How to Use GridBagLayout for more details

Align label and combo box in vertical instead of horizontal in java

How do I get my label and combo box to appear vertical instead of horizontal in java? I tried setting the layout to null but it hides the label.
public PetrolApplication()
{
setLayout(new FlowLayout());
add(label);
add(label2);
Combo1 = new JComboBox(Arraycities);
Combo1.setBounds(50, 50, 100, 20);
Use a different layout manager - see A Visual Guide to Layout Managers
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(16, 16, 16, 16));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add( new JLabel("I'm assuming you want something"), gbc);
gbc.anchor = GridBagConstraints.LINE_END;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(new JLabel("Please make a selection"), gbc);
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
model.addElement("This is not what you are looking for");
model.addElement("This is something else");
model.addElement("Not what you think");
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx++;
add(new JComboBox<String>(model));
}
}
}

GridBagLayout Not Working Properly?

I am using a GridBagLayout for my components in a JFrame. I just started using it, and I keep confusing myself. What I want is; Patch notes in the top left, buttons (vertical) on the right, play button on the bottom. I'm not sure what the issue really is, but can you please help me organize this?
Here's my code:
package counter.main;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class HomeFrame {
private static JPanel panel;
private static JButton play = new JButton("Play");
private static JPanel p;
File patch = new File(Main.class.getResource("/counter/res/ResourceCounterPatchNotes.txt").getFile());
//private static JLabel text;
public static JLabel greet = new JLabel("", SwingConstants.CENTER);
static JFrame frame = new JFrame("Resource Counter - Home"); {
frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.repaint();
frame.revalidate();
createView();
}
private void createView() {
setIcon();
panel = new JPanel();
frame.getContentPane().add(panel);
p = new JPanel();
frame.getContentPane().add(p);
p.setLayout(new FlowLayout(FlowLayout.CENTER, 400, 360));
play.setPreferredSize(new Dimension(200, 70));
p.add(play);
JPanel p2 = new JPanel();
frame.getContentPane().add(p2);
p2.setLayout(new GridBagLayout());
JButton button = new JButton(" Button ");
play.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
Thread.sleep(500);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
SelectionFrame.frame1.setVisible(true);
frame.setVisible(false);
}
});
JTextArea ta = new JTextArea();
p2.setBackground(Color.BLACK);
ta.setForeground(Color.WHITE);
ta.setFont(new Font("Lucida Sans", Font.PLAIN, 12));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(-150, 5, 5, 30);
gbc.gridx = 0;
gbc.gridy = 0;
p2.add(ta, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
p2.add(button, gbc);
/*gbc.anchor = GridBagConstraints.SOUTH;
gbc.gridx = 0;
gbc.gridy = 2;
p2.add(play, gbc);*/
try {
ta.read(new FileReader(patch), null);
ta.setEditable(false);
//p2.add(ta, BorderLayout.WEST);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
greet.setFont(new Font( "Dialog", Font.BOLD, 20));
frame.getContentPane().add(greet, BorderLayout.NORTH);
}
public void setIcon() {
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/counter/res/Iron-Pickaxe-icon.png")));
}
}
Here is what I get:
Your code is a mess (sorry), I could spent a lot of time trying to unravel your compound layouts, but it would be easier to just start again. Not saying that you might not consider using a compound layout concept, but I think that's what's got you into such a mess to start with...
So I've basically create a simplified example of what I "guess" your description is asking for...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = gbc.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
JTextArea patch = new JTextArea(10, 20);
add(new JScrollPane(patch), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
for (int index = 0; index < 6; index++) {
add(new JButton("Button #" + index), gbc);
gbc.gridy++;
}
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JButton("Play >"), gbc);
}
}
}
With layouts, it's best to start with pen and paper, group the elements you need to together (like the buttons down the right side for example) and devise a plan for how you might lay them out and prototype of view ideas...
Updated
1) How can I have it so that there is space between the TextArea and the buttons, and vertical space on the buttons? I tried using the Insets but I haven't arranged the numbers in a correct way yet.
insets is the correct way to go...
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(2, 8, 2, 4);
gbc.anchor = GridBagConstraints.NORTH;
for (int index = 0; index < 6; index++) {
core.add(new JButton("Button #" + index), gbc);
gbc.gridy++;
}
2) I would like the "Play" button at the center-bottom of the screen, and have it be larger
You "could" do this with GridBagLayout, but I decided not to, as it can cause some issues if you're not careful with how your setup the constraints for the other components, so instead, I used a combination of BorderLayout and GridBagLayout.
To make the "play" button larger, you could modify the font or adjust the button's margins...
playButton.setMargin(new Insets(12, 12, 12, 12));
depending on the effect you're after
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout(0, 4));
JPanel core = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JTextArea patch = new JTextArea(10, 20);
core.add(new JScrollPane(patch), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(2, 8, 2, 4);
gbc.anchor = GridBagConstraints.NORTH;
for (int index = 0; index < 6; index++) {
core.add(new JButton("Button #" + index), gbc);
gbc.gridy++;
}
add(core);
JButton playButton = new JButton("Play >");
playButton.setMargin(new Insets(12, 12, 12, 12));
add(playButton, BorderLayout.SOUTH);
}
}

Prevent components from spreading apart when resizing using GridBagLayout

Edit: Each time I try adding the gui tag, it switches to user-interface. Someone mind explaining/fixing that?
I want the client to be resizable. I want the JSeparator to fill the frame's width when resizing, but I want the JLabels to stay next to the fields.
It starts out like this, which the JLabels are too far apart from the fields as it is:
When I resize it horizontally, this is the result:
Which is obviously way too far apart. The code that I use to set up these components are:
public class LoginPanel extends JPanel {
private JTextField userfield = new JTextField(10);
private JPasswordField passfield = new JPasswordField(10);
private JButton login = new JButton("Login");
private JButton create = new JButton("Create Account");
public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.gridx = 2;
JLabel label = new JLabel("Username: ");
add(label, gbc);
gbc.gridx = 3;
gbc.gridwidth = 2;
add(userfield, gbc);
gbc.gridy = 1;
add(passfield, gbc);
gbc.gridx = 2;
label = new JLabel("Password: ");
add(label, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 2;
gbc.gridx = 1;
gbc.gridwidth = 5;
add(new JSeparator(JSeparator.HORIZONTAL), gbc);
}
}
(Had to cut out a few things, please tell me if I'm missing anything)
I've tried anchoring, but I'm still not 100% familiar with GridBagLayout (and the constraints) yet, so I'm not sure if I'm my attempts are in the right direction.
How would I prevent the Username: and Password: labels from moving away from my fields, with still being able to resize?
Also, I want to use GridBagLayout. There is still a lot of things I need to add, and I do not want to use a simple layout due to the fact that I'm going to need flexibility.
Make use of GridBagConstraints#anchor
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LogInTest {
public static void main(String[] args) {
new LogInTest();
}
public LogInTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new LoginPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LoginPanel extends JPanel {
private JTextField userfield = new JTextField(10);
private JPasswordField passfield = new JPasswordField(10);
private JButton login = new JButton("Login");
private JButton create = new JButton("Create Account");
public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.gridx = 2;
gbc.anchor = GridBagConstraints.EAST;
JLabel label = new JLabel("Username: ");
add(label, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 3;
gbc.gridwidth = 2;
add(userfield, gbc);
gbc.gridy = 1;
add(passfield, gbc);
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 2;
label = new JLabel("Password: ");
add(label, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 2;
gbc.gridx = 1;
gbc.gridwidth = 5;
add(new JSeparator(JSeparator.HORIZONTAL), gbc);
}
}
}
You may also want to consider making use of compound layouts, that is, separate each area into it's own container and focus on the individual layout needs for each section and then build them all up into a single layout

Java GUI, trying to position radio buttons and check boxes

So I'm trying to create a series of radio buttons and check boxes that are displayed as follows:
Radio Button
Check Box
Radio Button
Check Box
Radio Button
However, I'm still in the learning process for java and I was wondering if anyone could solve this problem. At the moment the buttons and boxes are being displayed in the correct location, however the first radio button ("Times") is not being displayed for some reason. If you could perhaps describe the reason and a possible solution that'd be great.
Thanks
Updated Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Question2 {
public static void main(String[] args) {
MyFrame f = new MyFrame("Font Chooser");
f.init();
}
}
class MyFrame extends JFrame {
MyFrame(String title) {
super(title);
}
private JPanel mainPanel;
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbLayout = new GridBagLayout();
void init() {
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
this.setContentPane(mainPanel);
gbc.gridx = 0;
gbc.gridy = 1;
JCheckBox cb = new JCheckBox("Bold");
gbLayout.setConstraints(cb, gbc);
mainPanel.add(cb);
gbc.gridy = 3;
gbLayout.setConstraints(cb, gbc);
cb = new JCheckBox("Italic");
mainPanel.add(cb);
gbc.gridx = 1;
gbc.gridy = 0;
JRadioButton rb = new JRadioButton("Times");
gbLayout.setConstraints(rb, gbc);
mainPanel.add(rb, gbc);
gbc.gridy = 2;
gbLayout.setConstraints(rb, gbc);
rb = new JRadioButton("Helvatica");
mainPanel.add(rb, gbc);
gbc.gridy = 4;
gbLayout.setConstraints(rb, gbc);
rb = new JRadioButton("Courier");
mainPanel.add(rb, gbc);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
Here's the problem, you are saying each height is 3 high, but really each cell is 1.
cRadioButton.gridheight = 3; // change this to 1
Here's the full source, and I did make some of the suggested changes from the other answer because at some point you will want to do something different (different action listener implementation for each type of button).
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class MyFrame1 extends JFrame {
MyFrame1(String title) {
super(title);
}
private JPanel mainPanel;
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbLayout = new GridBagLayout();
void init() {
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
this.setContentPane(mainPanel);
gbc.gridx = 0;
gbc.gridy = 1;
JCheckBox italic = new JCheckBox("Italic");
gbLayout.setConstraints(italic, gbc);
mainPanel.add(italic);
JCheckBox bold = new JCheckBox("Bold");
gbc.gridy = 3;
gbLayout.setConstraints(bold, gbc);
mainPanel.add(bold);
gbc.gridx = 1;
gbc.gridy = 0;
JRadioButton times = new JRadioButton("Times");
gbLayout.setConstraints(times, gbc);
mainPanel.add(times, gbc);
gbc.gridy = 2;
JRadioButton helv = new JRadioButton("Helvatica");
gbLayout.setConstraints(helv, gbc);
mainPanel.add(helv, gbc);
gbc.gridy = 4;
JRadioButton courier = new JRadioButton("Courier");
gbLayout.setConstraints(courier, gbc);
mainPanel.add(courier, gbc);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
MyFrame1 f = new MyFrame1("Font Chooser");
f.init();
}
}
It seems like you keep reassigning the same object, which may be leading to your overlapping. Instead of
JRadioButton rb = new JRadioButton("Times");
//...
newPanel.add(rb);
rb = new JRadioButton("Helvatica");
//...
newPanel.add(rb);
//and so on
try something like
JRadioButton times = new JRadioButton("Times");
JRadioButton helva = new JRadioButton("Helvatica");
//...
newPanel.add(times);
newPanel.add(helva);

Categories

Resources