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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:09:28+00:00 2026-05-26T00:09:28+00:00

I am new to Java Applet programming and Java in general but I am

  • 0

I am new to Java Applet programming and Java in general but I am very good in C# and C++. Anyway I am making a simple calculator with a JTextField, JButton and JLabel. But the JButton and JTextField take up the whole screen and the strangest thing is I set the size and it is not setting the size. So what am I doing wrong? Here is the code below so somebody can please help me; I will appreciate any answers.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.swing.JApplet;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
 *
 * @author Danny
 */
public class Applet extends JApplet {

    private JButton button1;
    private JTextField textBox1;
    @Override
    public void paint(Graphics g)
    {
        g.setFont(new Font("Courier", Font.PLAIN, 12));
        g.drawString("Enter a number: ", 36, 36);
        return;
    }

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    @Override
    public void init() {
          button1 = new JButton("Calculate");
          textBox1 = new JTextField("number");
          textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
          textBox1.setSize(100, 36);
          button1.setSize(100, 36);
          textBox1.setLocation(new Point(36, 76));
          button1.setLocation(new Point(36, 70));
          Container c = getContentPane();
          c.add(button1);
          c.add(textBox1);
          c.setBackground(Color.gray);
    }
    // TODO overwrite start(), stop() and destroy() methods
}
  • 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-26T00:09:29+00:00Added an answer on May 26, 2026 at 12:09 am

    You need to take layouts into consideration here. A JApplet’s contentPane uses BorderLayout by default, and if you add a component to BorderLayout without a second parameter telling it where to add it, it will be added BorderLayout.CENTER and will fill as much space as possible. The key to solving this is to learn as much as possible about layout managers and use this information to your advantage. You can find this information here: Visual Guide to the Layout Managers.

    Note that often we nest JPanels, each using its own coder-friendly layout manager, in order to achieve complex but easy to manage layouts.

    e.g.,

    import java.awt.*;
    import javax.swing.*;
    
    public class Applet extends JApplet {
    
       private JButton button1;
       private JTextField textBox1;
    
       @Override
       public void init() {
          button1 = new JButton("Calculate");
          textBox1 = new JTextField("number");
          textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
          JPanel myPanel = new JPanel(); // JPanel uses FlowLayout by default
          myPanel.add(new JLabel("Enter a number: "));
          myPanel.add(textBox1);
          myPanel.add(button1);
          myPanel.setBackground(Color.gray);
    
          getContentPane().add(myPanel, BorderLayout.CENTER);
       }
    
    }
    

    Note also that setSize is usually ignored by most layout managers. Instead preferredSize is usually honored, but setting this should be avoided as instead you’ll want the components themselves to set their preferredSize based on their contents or other properties (such as rows and columns for a JTextArea or columns for a JTextField, or String text for a JLabel).

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

Sidebar

Related Questions

Ok so I'm not Java expert but I'm just programming this java applet game
I'm trying to deploy an Applet with new Java JNLP specifications, but I always
I'm pretty new to Perl but have been programming in java for several months
I'm soon going to check in the very first commit of a new Java
I'm relatively new to java (specifically swing) and have recently been making some fairly
I'm using getAppletContext().showDocument(new URL(javascript: + command)); to call javascript from applet. But sometimes in
I am new in Java programming. My query is that I am having an
I am a programming enthusiast with a basic programming background, but I'm completely new
I'm making a game in a java applet and I'm trying to optimise my
So I am making a Text-Based RPG Applet in Java. I am using SWING

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.