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

  • SEARCH
  • Home
  • 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 6135985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:32:50+00:00 2026-05-23T17:32:50+00:00

I want to create a little game in Java using Netbeans. For now I

  • 0

I want to create a little game in Java using Netbeans. For now I have a JFrame and two JPanels.
The JFrame contains both JPanels and a button. My intent is to click on this button and resize one of the JPanels (from 0 to >0 width).
Till now I menaged to resize the frame but I can’t figure out how to resize the JPanel.
This is what I’ve done so far:

Structure
   frame
    |_ panel 1
    |_ panel 2
    |_ button

 __________________
|  _        _      |
| | |      | |    _|
| | |      | |   | |
| | |      | |   |>|  
| | |      | |   |_|
| |_|      |_|     |
|__________________|

on click should expand frame and panel
 ______________________
|  _        _____      |
| | |      |     |    _|
| | |      |     |   | |
| | |      |     |   |>|  ->
| | |      |     |   |_|
| |_|      |_____|     |
|______________________|

This is the JPanel to resize

public class ToResize extends javax.swing.JPanel {

    ...

    public void resize(int width) {
        this.setSize(new Dimension(this.getWidth() + width, this.getHeight()));
    }
}

This is the JFrame with the button

public class MyFrame extends javax.swing.JFrame {

    ...

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        if (panelToResize.getWidth() == 0) {
            panelToResize.resize(100);
        } else {
            panelToResize.resize(-100);
        }
        validate();
    }
}
  • 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-05-23T17:32:50+00:00Added an answer on May 23, 2026 at 5:32 pm

    1) if you’ll resize JFrame too, then you have to call

    a/ setPrefferedSize(getPrefferedSize()+-) for JFrame.pack();

    b/ setPrefferedSize(getPrefferedSize()+-) for panelToResize and then call JFrame.pack();

    2/ if you only change size betweens JPanels and JFrame size stays remained, then you have to call revalidate() plus repaint() to the panelToResize,

    3/ but everything depends of used LayoutManager

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

Sidebar

Related Questions

I have very little experience designing databases, and now I want to create a
i want create image animation , i have 50 images with png format now
I want to create a Java application bundle for Mac without using Mac. According
I've been using Clojure for a little while and want to create some projects
I want to create a game that will work both locally and online. My
I have written a stupid little game and want to have some kind of
I want to create a basic side-scrolling game in Java like the 1995 game
I'm new to both android and game development and have been trying to create
I want create a excel with Apache POI in java and I must insert
I'm trying to write a little CLI Hangman game, just to create something in

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.