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 8764027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:55:47+00:00 2026-06-13T15:55:47+00:00

Why does the first code not put a border around the JPanel while the

  • 0

Why does the first code not put a border around the JPanel while the second one does? I cannot see any differences in the code for the border. In the first one i am working on snake. The second one is a example which i wrte to post here but it turned out it worked.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ts {


public static void main(String[] args) {

        MyJFrame frame1 = new MyJFrame();

    }
    static class MyJFrame extends JFrame{

        public MyJFrame (){

            setTitle("Snake Build: Dev 0.2");
            setBounds(200,100,600,500);
            setResizable(false);
            setLayout(null);

            Container cp = getContentPane();

            JPanel settings = new JPanel();
            settings.setBounds(200, 100, 200, 200);
            settings.setBorder(BorderFactory.createRaisedBevelBorder());
            //exsists but cant be seen border working?
            settings.addMouseListener(new MouseListener(){
                @Override
                public void mouseClicked(MouseEvent arg0) {
                }

                @Override
                public void mouseEntered(MouseEvent arg0) {
                    System.out.println("entered");
                }

                @Override
                public void mouseExited(MouseEvent arg0) {
                    System.out.println("exited");
                }

                @Override
                public void mousePressed(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void mouseReleased(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }
            });

            cp.add(settings);
            settings.setVisible(true);

            JButton test = new JButton("test");
            test.setBounds(10, 20, 30, 30);
            settings.add(test);
            test.setVisible(true);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }

        public void paint(Graphics g)
        {

        }
    }
}

Second piece which does give a border

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ts {
    public static void main(String[] args) {

        MyJF frame1 = new MyJF();

    }
    static class MyJF extends JFrame{
        public MyJF(){
            setBounds(50,50,500,200);
            setLayout(null);
            setResizable(false);

            Container cp = getContentPane();

            JPanel settings = new JPanel();
            settings.setBounds(20,20,100,100);
            settings.setBorder(BorderFactory.createRaisedBevelBorder());

            JButton JB = new JButton("hey");
            JB.setBounds(10, 10, 50, 50);
            settings.add(JB);

            add(settings);

            settings.setVisible(true);

            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }

}
  • 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-13T15:55:49+00:00Added an answer on June 13, 2026 at 3:55 pm

    Without wanting to state the obvious but you are overriding JFrame.paint(Graphics) with an empty code-block, hence you see problems. If you remove that part it will work as expected.

    Consider the following recommendations:

    • Do not override JFrame.paint(Graphics) but instead override JComponent.paintComponent(Graphics) on a component contained in your JFrame for custom painting. In both case, you should usually also call the super method
    • Use appropriate LayoutManager‘s and forget about setBounds/setLocation/setSize (and don’t try to replace them with set-Min/Max/Pref-Size)
    • Follow Java naming conventions (classes start with an Upper case letter)
    • Do not extend JFrame if it is not needed. You should extend JFrame only if you add frame-behaviour
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the first code print DCBA and the second ABCD? Is 0x44434241 not
Does the following code contain a memory leak of the first three characters in
So I have some code that does not seem to be working. This is
EDIT3: To wit, DO NOT put migration code in your model.rb files!!! EDIT2: THE
I have a snippet of code where I first put some values in to
I have the following code which does (the first part of) what I want
The code below runs the exact same calculation 3 times (it does not do
Why does the first if statement evaluate to true? I know if I use
Two related questions. First: does this repro for you or is it something local
What the title says. Are they the same? I've noticed that the first does

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.