I'm fairly new to Java and programming in general and I've run into a problem that I can't seem to solve. In my program I have two JPanels, containing a JLabel and three JButtons respectively, and the panels are arranged in a GridLayout within the frame.
My problem is that I can't get the text in the JLabel to sit centrally to the north of the first JPanel. Originally I was using a FlowLayout that arranged it in the orientation I wanted, but when the contained String became longer than a certain length, it just went off-screen. With the current setup, the text wraps appropriately, but always starts off aligned to the left.
Example screenshot of UI
I've searched around the site and looked at multiple answers - indeed I thought my issue would be solved by using HTML (This does solve the wrapping issue and aligns the second line centrally, as seen below) - but I still can't figure out how to align the first line centrally.
Example using longer String
Here's my current code:
import java.awt.*;
import javax.swing.*;
public class UITests extends JFrame {
private JLabel txtWindow = new JLabel();
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private JButton jb3 = new JButton();
private final Font font = new Font("Serif", Font.PLAIN, 12);
public UITests() {
//Creating the top panel for the JLabel
JPanel panelA = new JPanel(new BorderLayout());
txtWindow.setForeground(Color.white);
txtWindow.setFont(new Font("", Font.PLAIN, 15));
txtWindow.setText("<html><div style='text-align: center;'>" + "Mellon"
+ "</div></html>");
panelA.setBackground(Color.BLACK);
panelA.add(txtWindow, BorderLayout.NORTH);
//Creating the bottom panel for the JButtons
JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));
//setting up button properties
jb1.setBackground(Color.BLACK);
jb2.setBackground(Color.BLACK);
jb3.setBackground(Color.BLACK);
jb1.setForeground(Color.white);
jb2.setForeground(Color.white);
jb3.setForeground(Color.white);
jb1.setFocusPainted(false); //Stopping highlighting of button
jb2.setFocusPainted(false);
jb3.setFocusPainted(false);
jb1.setFont(font);
jb2.setFont(font);
jb3.setFont(font);
panelB.setBackground(Color.BLACK);
panelB.add(jb1);
panelB.add(jb2);
panelB.add(jb3);
setLayout(new GridLayout(2, 1, 0, 0));
add(panelA);
add(panelB);
setTitle("Screen");
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new UITests();
}
}
Any help with this would be greatly appreciated, and apologies if there exists a very similar question; I did my best to find it!
Just use txtWindow.setHorizontalAlignment(JLabel.CENTER);, like so:
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel txtWindow = new JLabel();
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private JButton jb3 = new JButton();
private final Font font = new Font("Serif", Font.PLAIN, 12);
public Main() {
//Creating the top panel for the JLabel
JPanel panelA = new JPanel(new BorderLayout());
txtWindow.setForeground(Color.white);
txtWindow.setFont(new Font("", Font.PLAIN, 15));
txtWindow.setText("Mellon");
txtWindow.setHorizontalAlignment(JLabel.CENTER);
panelA.setBackground(Color.BLACK);
panelA.add(txtWindow, BorderLayout.NORTH);
//Creating the bottom panel for the JButtons
JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));
//setting up button properties
jb1.setBackground(Color.BLACK);
jb2.setBackground(Color.BLACK);
jb3.setBackground(Color.BLACK);
jb1.setForeground(Color.white);
jb2.setForeground(Color.white);
jb3.setForeground(Color.white);
jb1.setFocusPainted(false); //Stopping highlighting of button
jb2.setFocusPainted(false);
jb3.setFocusPainted(false);
jb1.setFont(font);
jb2.setFont(font);
jb3.setFont(font);
panelB.setBackground(Color.BLACK);
panelB.add(jb1);
panelB.add(jb2);
panelB.add(jb3);
setLayout(new GridLayout(2, 1, 0, 0));
add(panelA);
add(panelB);
setTitle("Screen");
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
This will center the text in the label.
Then you won't need to add HTML, so you can also setText("Melon"); directly, instead of using HTML.
Edit 1
For both short and long strings though try combining your centered HTML div tag and the setHorizontalAlignment(JLabel.CENTER); call, like so:
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel txtWindow = new JLabel();
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private JButton jb3 = new JButton();
private final Font font = new Font("Serif", Font.PLAIN, 12);
public Main() {
//Creating the top panel for the JLabel
JPanel panelA = new JPanel(new BorderLayout());
txtWindow.setForeground(Color.white);
txtWindow.setFont(new Font("", Font.PLAIN, 15));
final String text = "Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon";
txtWindow.setText("<html><div style='text-align: center;'>" + text + "</div></html>");
txtWindow.setHorizontalAlignment(JLabel.CENTER);
panelA.setBackground(Color.BLACK);
panelA.add(txtWindow, BorderLayout.NORTH);
//Creating the bottom panel for the JButtons
JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));
//setting up button properties
jb1.setBackground(Color.BLACK);
jb2.setBackground(Color.BLACK);
jb3.setBackground(Color.BLACK);
jb1.setForeground(Color.white);
jb2.setForeground(Color.white);
jb3.setForeground(Color.white);
jb1.setFocusPainted(false); //Stopping highlighting of button
jb2.setFocusPainted(false);
jb3.setFocusPainted(false);
jb1.setFont(font);
jb2.setFont(font);
jb3.setFont(font);
panelB.setBackground(Color.BLACK);
panelB.add(jb1);
panelB.add(jb2);
panelB.add(jb3);
setLayout(new GridLayout(2, 1, 0, 0));
add(panelA);
add(panelB);
setTitle("Screen");
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
Hope this is what you are looking for.
Related
This is the picture I am trying to replicate
This is what I have (didn't add icon images yet)
I can't seem to find a solution, been staring at it for quite some time.
I am trying to replicate the following picture, using GridLayout for the buttons and the figure out the rest on my own using Java Swing. Furthermore, I've added my buttons into a JPanel and now I'm trying to add spacing between the panel and the pane.
This is what I have, how can I go about it?
super(title);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
Container pane = this.getContentPane();
JButton b1 = new JButton();
b1.setBackground(Color.white);
JButton b2 = new JButton();
b2.setBackground(Color.white);
JButton b3 = new JButton();
b3.setBackground(Color.white);
JButton b4 = new JButton();
b4.setBackground(Color.white);
JButton b5 = new JButton();
b5.setBackground(Color.white);
JButton b6 = new JButton();
b6.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,10,10));
panel.setBackground(Color.black);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
pane.add(panel);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
The easiest way to do it would be to add an empty border to your JPanel (see this post on empty borders):
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// ...
panel.setBorder(new EmptyBorder(50, 50, 50, 50));
Another good approach (depending always on your application needs), if you have the JButton preferred size set, would be to have the main JPanel's grid layout set to have two columns and one row, with another JPanel inside each column. Adding to the interior JPanels a BoxLayout in Y_AXIS mode and aligning the buttons with setAlignmentX() would work great too (note this approach wouldn't center the JButtons vertically) (see How to use BoxLayout):
public class MyFrame extends JFrame {
private String title = "Title";
public MyFrame(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,2,10,10));
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
JButton b1 = new JButton();
b1.setBackground(Color.white);
//b1.setIcon(new ImageIcon(img1));
b1.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b1);
JButton b2 = new JButton();
b2.setBackground(Color.white);
//b2.setIcon(new ImageIcon(img2));
b2.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b2);
JButton b3 = new JButton();
b3.setBackground(Color.white);
//b3.setIcon(new ImageIcon(img3));
b3.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b3);
JButton b4 = new JButton();
b4.setBackground(Color.white);
//b4.setIcon(new ImageIcon(img4));
b4.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b4);
JButton b5 = new JButton();
b5.setBackground(Color.white);
//b5.setIcon(new ImageIcon(img5));
b5.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b5);
JButton b6 = new JButton();
b6.setBackground(Color.white);
//b6.setIcon(new ImageIcon(img6));
b6.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b6);
add(mainPanel); //Adding our mainPanel to the contentPane of the JFrame
this.setSize(500,500); //or pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
}
}
Here's a little demonstration I whipped up.
All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
You don't set the size of the JFrame and try and make the Swing components fit. You let the JFrame pack with all the Swing components.
You create a GridLayout JPanel inside of a FlowLayout JPanel. The FlowLayout JPanel uses an empty border of the appropriate size.
I used the image the OP provided to get the icons.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class EmptySpaceDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new EmptySpaceDemo());
}
private Image[] images;
public EmptySpaceDemo() {
this.images = createImages();
}
private Image[] createImages() {
BufferedImage image = readImage();
Image[] images = new Image[6];
images[0] = image.getSubimage(155, 113, 110, 90);
images[1] = image.getSubimage(276, 113, 110, 90);
images[2] = image.getSubimage(155, 217, 110, 90);
images[3] = image.getSubimage(276, 217, 110, 90);
images[4] = image.getSubimage(155, 321, 110, 90);
images[5] = image.getSubimage(276, 321, 110, 90);
return images;
}
#Override
public void run() {
JFrame frame = new JFrame("Empty Space Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.BLACK);
panel.setBorder(BorderFactory.createEmptyBorder(40, 100, 40, 100));
JPanel innerPanel = new JPanel(new GridLayout(0, 2, 10, 10));
innerPanel.setBackground(Color.BLACK);
for (int i = 0; i < images.length; i++) {
JButton button = new JButton(new ImageIcon(images[i]));
innerPanel.add(button);
}
panel.add(innerPanel);
return panel;
}
private BufferedImage readImage() {
try {
return ImageIO.read(getClass().getResourceAsStream("/icons.png"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
I'm stuck with my application. Every time I run my code, my fields e.g. fromTextField once have width 180 other time my width private static final Integer widthField = 232.
Code:
package gui;
import javax.swing.*;
import java.awt.*;
public class MailGui extends JFrame {
private static final Integer widthField = 232;
private MailGui() {
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(5, 5));
JLabel fromLabel = new JLabel("From: ", SwingConstants.LEFT);
JLabel passwdLabel = new JLabel("Password: ", SwingConstants.LEFT);
JLabel toLabel = new JLabel("To: ", SwingConstants.LEFT);
JLabel subjectLabel = new JLabel("Subject: ", SwingConstants.LEFT);
JLabel textLabel = new JLabel("Content: ", SwingConstants.LEFT);
JTextField fromTextField = new JTextField();
JTextField toTextField = new JTextField();
JPasswordField passwdPasswordField = new JPasswordField();
JTextField subjectTextField = new JTextField();
JTextArea textArea = new JTextArea(8, 30);
JButton sendButton = new JButton("Send");
textArea.setLineWrap(true);
northPanel.add(fromLabel);
northPanel.add(fromTextField);
northPanel.add(passwdLabel);
northPanel.add(passwdPasswordField);
northPanel.add(toLabel);
northPanel.add(toTextField);
northPanel.add(subjectLabel);
northPanel.add(subjectTextField);
northPanel.add(textLabel);
northPanel.add(textArea);
this.add(northPanel, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(textArea);
this.add(scrollPane, BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.add(sendButton);
add(southPanel, BorderLayout.SOUTH);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
fromTextField.setBounds(textArea.getX() + 100, 0, widthField, 19);
passwdPasswordField.setBounds(textArea.getX() + 100, 19, widthField, 19);
toTextField.setBounds(textArea.getX() + 100, 38, widthField, 19);
subjectTextField.setBounds(textArea.getX() + 100, 57, widthField, 19);
}
public static void main(String[] args) {
new MailGui();
}
}
What I have:
Or
What I except:
Thanks for every help. Q.
Invoking the setBounds() method does nothing. The layout manager will override the size/location based on the rules of the layout manager. In the case of the GridLayout all components will be sized to the preferred size of the largest component or as the frame size is changes each cell will adjust to fill the space available in the frame.
When you create a JTextField the code should be something like:
JTextField textField = new JTextField(15);
This will allow the text field to determine its own preferred size to display 15 "W" characters based on the font of the text field.
Then the layout manager can do a better job at determining the size of each component
If you want the widths of the labels and the text fields to be different, then you need to use a different layout manager. Probably a GridBagLayout. Read the Swing tutorial on How to Use GridBagLayout for more information and examples.
Note the tutorial examples show you how to create the GUI on the Event Dispatch Thread (EDT).
Call pack() at the end of the GUI composition in order to layout it in a definite way once.
The dependency of the geometry to textArea is unfortunate. Put it in the layout too.
There are nice GUI editors with more complex layout managers.
So Im trying to make a little program to calculate the area of a specific shape.
The user should be able to make a input via a textfield (Like the height and stuff of the shapes). The he should press a button and the price should get printed.
But it doesnt show up.
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rechner extends JFrame implements ActionListener{
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JButton button4;
private static JTextField numberField;
private JPanel jpanel;
public Rechner(String titel){
super(titel);
jpanel = new JPanel();
numberField = new JTextField(1500);
add(numberField, BorderLayout.CENTER);
button1 = new JButton("Rechteck");
button1.setBounds(10, 10, 150, 30);
button1.addActionListener(this);
add(button1);
button2 = new JButton("Dreieck");
button2.setBounds(170, 10, 150, 30);
button2.addActionListener(this);
add(button2);
button3 = new JButton("Trapez");
button3.setBounds(330, 10, 150, 30);
button3.addActionListener(this);
add(button3);
button4 = new JButton("Parallelogramm");
button4.setBounds(490, 10, 150, 30);
button4.addActionListener(this);
add(button4);
setResizable(false);
}
public static void main(String[] args) {
Rechner frame = new Rechner("Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(660, 400);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
System.out.println("fff");
}
String numberStr = numberField.getText();
}
}
The default layout manager of a JFrame (well, of its content pane in fact) is BorderLayout.
Without specified constraints , the component is added to BorderLayout.CENTER, so
add(component);
is the same as
add(component, BorderLayout.CENTER);
and each component added this way will replace the last component added to the center.
Also note that setBounds will have no effect if there is a layout manager, and that you create a JPanel that you never use.
Finally, you may want to have a look at this guide : A Visual Guide to Layout Managers
This line is mainly the problem:
add(numberField, BorderLayout.CENTER);
Is causing the TextField to fill the entire space. Then, the next time you add a component to the JFrame with BorderLayout.CENTER, the JTextField gets replaced. To fix this:
super(titel);
jpanel = new JPanel();
add(jpanel, BorderLayout.NORTH); //adding the jpanel
button1 = new JButton("Rechteck");
jpanel.add(button1);
button1.setBounds(10, 10, 150, 30);
//adding the other buttons to the JPanel...
//...
//...
button4.addActionListener(this);
button3.addActionListener(this);
button2.addActionListener(this);
button1.addActionListener(this);
numberField = new JTextField(1500);
add(numberField);//this will cause it to fill the remaining space
setResizable(false);
Explanation:
The buttons should go into the JPanel you created, and the JPanel should go into the JFrame's NORTH. That way they don't cover the JFrame
when I compile and run my code everything seems to work fine except the JButton does not appear. I'm adding it to a JPanel that is on the frame. I'm new so I might not know what to lookout for here. Thanks!
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class TemperatureConverter extends JFrame{
//declarations
private JLabel celJLabel, farJLabel;
private JTextField celJTextField, farJTextField;
private JSlider sliderJSlider;
private JButton closeButton;
private TitledBorder border;
private JPanel topPanel, bottomPanel;
double celsiusDegrees, farenheitDegrees, sliderValue;
DecimalFormat decimalFormat = new DecimalFormat("#.0");
public TemperatureConverter()
{
createUserInterface();
}
public void createUserInterface()
{
//create the JFrame
Container frame = getContentPane();
frame.setBackground(Color.white);
frame.setLayout(null);
border = new TitledBorder("Convert between C & F");
border.setTitleColor(Color.black);
border.setTitleFont(new Font("Default", Font.ITALIC, 12));
border.setTitleJustification(TitledBorder.LEFT);
border.setTitlePosition(TitledBorder.TOP);
topPanel = new JPanel();
topPanel.setBounds(20,10,360,300);
topPanel.setForeground(Color.black);
topPanel.setBackground(Color.white);
topPanel.setLayout(null);
topPanel.setBorder(border);
frame.add(topPanel);
bottomPanel = new JPanel();
bottomPanel.setBounds(20,310,360,50);
bottomPanel.setForeground(Color.black);
bottomPanel.setBackground(Color.white);
bottomPanel.setLayout(null);
frame.add(bottomPanel);
celJLabel = new JLabel();
celJLabel.setBounds(120, 200, 60, 20);
celJLabel.setBackground(Color.white);
celJLabel.setFont(new Font("Default", Font.PLAIN, 12));
celJLabel.setText("Celcius");
celJLabel.setHorizontalAlignment(JLabel.LEFT);
topPanel.add(celJLabel);
farJLabel = new JLabel();
farJLabel.setBounds(120, 220, 60, 20);
farJLabel.setBackground(Color.white);
farJLabel.setFont(new Font("Default", Font.PLAIN, 12));
farJLabel.setText("Faranheit");
farJLabel.setHorizontalAlignment(JLabel.LEFT);
topPanel.add(farJLabel);
celJTextField = new JTextField();
celJTextField.setBounds(195,200, 50, 15);
celJTextField.setFont(new Font("Default", Font.PLAIN, 12));
celJTextField.setHorizontalAlignment(JTextField.CENTER);
celJTextField.setForeground(Color.black);
celJTextField.setBackground(Color.white);
topPanel.add(celJTextField);
farJTextField = new JTextField();
farJTextField.setBounds(195,225, 50, 15);
farJTextField.setFont(new Font("Default", Font.PLAIN, 12));
farJTextField.setHorizontalAlignment(JTextField.CENTER);
farJTextField.setForeground(Color.black);
farJTextField.setBackground(Color.white);
topPanel.add(farJTextField);
sliderJSlider = new JSlider(JSlider.HORIZONTAL, 0,100,0);
sliderJSlider.setBounds(20, 20, 310, 120);
sliderJSlider.setMajorTickSpacing(10);
sliderJSlider.setMinorTickSpacing(5);
sliderJSlider.setPaintTicks(true);
sliderJSlider.setPaintLabels(true);
sliderJSlider.setForeground(Color.black);
sliderJSlider.setBackground(Color.white);
topPanel.add(sliderJSlider);
sliderJSlider.addChangeListener(
new ChangeListener()
{
public void stateChanged(ChangeEvent event)
{
sliderStateChanged(event);
}
}
);
closeButton = new JButton();
closeButton.setBounds(140, 250, 75, 20);
closeButton.setFont(new Font("Default", Font.PLAIN,12));
closeButton.setText("Close");
closeButton.setForeground(Color.black);
closeButton.setBackground(Color.white);
bottomPanel.add(closeButton);
closeButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeActionPerformed(event);
}
}
);
setTitle("Temperature Converter");
setSize(400,400);
setVisible(true);
}
public static void main(String[] args)
{
TemperatureConverter application = new TemperatureConverter();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void sliderStateChanged(ChangeEvent event)
{
farenheitDegrees = sliderJSlider.getValue();
calculateCelsiusTemperature();
}
public void calculateCelsiusTemperature()
{
celsiusDegrees = (farenheitDegrees - 32)*5.0/9.0;
outputTemps();
}
public void outputTemps()
{
celJTextField.setText(decimalFormat.format(celsiusDegrees));
farJTextField.setText(decimalFormat.format(farenheitDegrees));
}
public void closeActionPerformed(ActionEvent event)
{
TemperatureConverter.this.dispose();
}
}
I'd follow the advice from the comments, use a proper layout manager.
The actual fault, is the placement of the close button within the bottom panel.
closeButton.setBounds(140, 250, 75, 20);
This might be a typo or a misunderstanding of the coordinate system, each new panel has its own private system where (0,0) is the top left of at component. The button is at (140, 250), however bottomPanel is only 360 x 50, so it is outside the visible bounds..
Try changing to
closeButton.setBounds(0, 0, 75, 20);
Your first and major mistake is this: topPanel.setLayout(null);. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
The solution is simple: learn about and how to use the layout managers, and then use them. You can find links to the Swing tutorials including those for the layout managers and other Swing resources here: Swing Info
e.g.,
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class TempConverter extends JPanel {
private static final int PREF_W = 400;
private static final int GAP = 5;
private JTextField celJTextField = new JTextField(10);
private JTextField farJTextField = new JTextField(10);
private JSlider sliderJSlider = new JSlider(0, 100, 0);
private JButton closeButton = new JButton("Close");
public TempConverter() {
sliderJSlider.setMajorTickSpacing(10);
sliderJSlider.setMinorTickSpacing(5);
sliderJSlider.setPaintTicks(true);
sliderJSlider.setPaintLabels(true);
JPanel textFieldPanel = new JPanel(new GridBagLayout());
textFieldPanel.add(new JLabel("Celcius:"), createGbc(0, 0));
textFieldPanel.add(celJTextField, createGbc(1, 0));
textFieldPanel.add(new JLabel("Faranheit:"), createGbc(0, 1));
textFieldPanel.add(farJTextField, createGbc(1, 1));
JPanel textFieldWrapperPanel = new JPanel(new GridBagLayout());
textFieldWrapperPanel.add(textFieldPanel);
JPanel conversionPanel = new JPanel(new BorderLayout());
conversionPanel.setBorder(BorderFactory.createTitledBorder("Foo"));
conversionPanel.setLayout(new BorderLayout());
conversionPanel.add(sliderJSlider, BorderLayout.PAGE_START);
conversionPanel.add(textFieldWrapperPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
bottomPanel.add(closeButton);
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout());
add(conversionPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(GAP, GAP, GAP, GAP);
return gbc;
}
#Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
;
if (isPreferredSizeSet()) {
super.getPreferredSize();
}
int prefW = Math.max(PREF_W, superSize.width);
return new Dimension(prefW, superSize.height);
}
private static void createAndShowGui() {
TempConverter mainPanel = new TempConverter();
JFrame frame = new JFrame("TempConverter");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Now you might say that this looks more complicated, and perhaps it is, but what happens when you want to add another JTextField and JLabel to represent the Kelvin temperature scale? For your GUI, you'll need to resize the GUI and recalculate the position of any component that may be effected by adding the new components. For my GUI, you just need to add a few new lines, and the chance of the changes causing a bug in my code is much smaller than that of your changes. e.g. please note the changes below just require 3 lines of code. Everything else remains the same:
public class TempConverter extends JPanel {
private static final int PREF_W = 400;
private static final int GAP = 5;
private JTextField celJTextField = new JTextField(10);
private JTextField farJTextField = new JTextField(10);
private JTextField KelvinJTextField = new JTextField(10); // !!! Added
private JSlider sliderJSlider = new JSlider(0, 100, 0);
private JButton closeButton = new JButton("Close");
public TempConverter() {
sliderJSlider.setMajorTickSpacing(10);
sliderJSlider.setMinorTickSpacing(5);
sliderJSlider.setPaintTicks(true);
sliderJSlider.setPaintLabels(true);
JPanel textFieldPanel = new JPanel(new GridBagLayout());
textFieldPanel.add(new JLabel("Celcius:"), createGbc(0, 0));
textFieldPanel.add(celJTextField, createGbc(1, 0));
textFieldPanel.add(new JLabel("Faranheit:"), createGbc(0, 1));
textFieldPanel.add(farJTextField, createGbc(1, 1));
// !!! added
textFieldPanel.add(new JLabel("Kelvin:"), createGbc(0, 2));
textFieldPanel.add(KelvinJTextField, createGbc(1, 2));
public class Gridlayout extends JFrame {
public Gridlayout()
{
setTitle("xxx");
setSize(540,635);
this.setResizable(false);
this.setLocation(500,50);
initComponents();
setDefaultCloseOperation(3);
}
JLabel etykieta = new JLabel("Poziom trudności: ");
JPanel top = new JPanel(new GridLayout(2, 2));
JPanel mid = new JPanel(new GridLayout(0, 1));
JPanel bot = new JPanel(new GridLayout(1, 0));
JButton start= new JButton("Start");
JButton zakoncz = new JButton("Zakoncz");
JRadioButton latwy = new JRadioButton("Łatwy");
JRadioButton sredni = new JRadioButton("Średni");
JRadioButton trudny = new JRadioButton("Trudny");
ButtonGroup poziomTrudnosci= new ButtonGroup();
public void initComponents() {
this.getContentPane().add(top, BorderLayout.NORTH);
this.getContentPane().add(mid, BorderLayout.CENTER);
this.getContentPane().add(bot, BorderLayout.SOUTH);
start.setPreferredSize(new Dimension(1, 40));
zakoncz.setPreferredSize(new Dimension(1, 40));
mid.add(etykieta);
mid.add(latwy);
mid.add(sredni);
mid.add(trudny);
bot.add(start);
poziomTrudnosci.add(latwy);
poziomTrudnosci.add(sredni);
poziomTrudnosci.add(trudny);
}
public static void main(String[] args) {
new Gridlayout().setVisible(true);
}
}
How can I move this button from the left to the center of the window? They should stay close. I was trying everything but they still are on the left side.
If I understand your question correctly, you want to horizontally center the JRadioButtons in the JFrame, yes? Try the setHorizontalAlignment() method. Add the following lines to your initComponents() function:
latwy.setHorizontalAlignment(JRadioButton.CENTER);
sredni.setHorizontalAlignment(JRadioButton.CENTER);
trudny.setHorizontalAlignment(JRadioButton.CENTER);