Home > Java > javaTutorial > Why are GUI components obscured in a JFrame using BorderLayout?

Why are GUI components obscured in a JFrame using BorderLayout?

Patricia Arquette
Release: 2024-11-07 16:59:03
Original
335 people have browsed it

Why are GUI components obscured in a JFrame using BorderLayout?

GUI Component Obscured in JFrame

As one user observed, an issue in the GUI code prevents all components from being displayed correctly. Specifically, the top panel obscures the other panels.

Cause:

This problem arises due to the BorderLayout layout used in JFrame. By default, components added without specific constraints are automatically placed in the CENTER position, which allows only one component to be visible.

Solution:

To fix this layout issue, change the code as follows:

f.add(top, BorderLayout.PAGE_START); // Add top panel to PAGE_START position
f.add(mid); // Add mid panel with default BorderLayout.CENTER constraint
f.add(bot, BorderLayout.PAGE_END); // Add bot panel to PAGE_END position
Copy after login

Additional Optimizations:

  1. Remove f.setSize(500, 500); and call pack() before setVisible(true) for a better fitting window.
  2. Use f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); to properly dispose of the GUI window.
  3. Remove in.setVisible(true); and out.setVisible(true); as components are automatically visible when added to a JFrame.
  4. Change public class EncDecExample extends JFrame to public class EncDecExample for better code organization.

Complete Optimized Code:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

public class EncDecExample implements ActionListener {
    final static JPanel top = new JPanel();
    final static JPanel mid = new JPanel();
    final static JPanel bot = new JPanel();
    final static JTextField in = new JTextField(10);
    final static JTextField out = new JTextField(10);
    final static JButton enc = new JButton("Encrypt");
    final static JButton dec = new JButton("Decrypt");
    final static JFrame f = new JFrame("Encryption/decryption");

    public static void main(String[] args) {
        f.setSize(500, 500);
        f.setResizable(false);
        out.setEditable(false);
        out.setText("Hello");
        top.add(in);
        mid.add(enc);
        mid.add(dec);
        bot.add(out);
        f.add(top, BorderLayout.PAGE_START);
        f.add(mid);
        f.add(bot, BorderLayout.PAGE_END);
        f.pack();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == enc && !in.getText().equalsIgnoreCase("")) {
            out.setText(e(in.getText(), 5));
        } else if (e.getSource() == dec && !in.getText().equalsIgnoreCase("")) {
            out.setText(d(in.getText()));
        }
    }
}
Copy after login

The above is the detailed content of Why are GUI components obscured in a JFrame using BorderLayout?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template