Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:44:35+00:00 2026-06-01T16:44:35+00:00

When I run this program, the window blocks out the buttons in panel2 when

  • 0

When I run this program, the window blocks out the buttons in panel2 when I use setSize to determine window size.

In addition, if I use frame.pack() instead of setSize(), all components are on one horizontal line but I’m trying to get them so that panel1 components are on one line and panel2 components are on a line below them.

Could someone explain in detail the answers to both of these problems?

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

public class Exercise16_4 extends JFrame{
    // FlowLayout components of top portion of calculator
    private JLabel jlbNum1 = new JLabel("Number 1");
    private JTextField jtfNum1 = new JTextField(4);
    private JLabel jlNum2 = new JLabel("Number 2");
    private JTextField jtfNum2 = new JTextField(4);
    private JLabel jlbResult = new JLabel("Result");
    private JTextField jtfResult = new JTextField(8);

    // FlowLayout Components of bottom portion of calculator
        private JButton jbtAdd = new JButton("Add");
    private JButton jbtSubtract = new JButton("Subtract");
    private JButton jbtMultiply = new JButton("Multiply");
    private JButton jbtDivide = new JButton("Divide");


    public Exercise16_4(){
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
        panel1.add(jlbNum1);
        panel1.add(jtfNum1);
        panel1.add(jlNum2);
        panel1.add(jtfNum2);
        panel1.add(jlbResult);
        panel1.add(jtfResult);

        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 10));
        panel1.add(jbtAdd);
        panel1.add(jbtSubtract);
        panel1.add(jbtMultiply);
        panel1.add(jbtDivide);

        add(panel1, BorderLayout.NORTH);
        add(panel2, BorderLayout.CENTER);
    }

    public static void main(String[] args){
        Exercise16_4 frame = new Exercise16_4();

        frame.setTitle("Caculator");
        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setResizable(false);
        frame.setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:44:37+00:00Added an answer on June 1, 2026 at 4:44 pm

    You’re problem is likely a typographical error in that you’re adding all components to panel1 and none to panel2:

      // you create panel2 just fine
      JPanel panel2 = new JPanel();
      panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 10));
    
      // but you don't use it! Change below to panel2.
      panel1.add(jbtAdd);
      panel1.add(jbtSubtract);
      panel1.add(jbtMultiply);
      panel1.add(jbtDivide);
    

    Add the buttons to panel2, and then call pack() before setVisible(true). Do not set the size of the GUI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program I use has it's own variables to set when you run it,
I am trying to use networkx with Python. When I run this program it
when I run this program, it print NameError global name 'viewAll' is not defined
I have run this program before and it worked fine. Then I added the
When I run this program, I enter the username as dcole and the password
I need to run this line from my c++ program: java -jar test.jar text1
Is there a program which I can run like this: py2py.py < orig.py >
I've run into this while writing a Traveling Salesman program. For an inner loop,
To run a program with custom env settings, we do this on Linux $
Everytime I run the program, this mysterious error pops up saying that I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.