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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:17:24+00:00 2026-06-13T05:17:24+00:00

I can’t make the string show up when the Enter message button is clicked,

  • 0

I can’t make the string show up when the “Enter message” button is clicked, and I can’t center the box for some reason. When I click the Draw square it will draw the square and if I click “Draw it in color” it will draw it in color. However no matter what I do, the text will not appear. Here’s my code:

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


public class Lab9 extends JApplet implements ActionListener, ItemListener {

 private JRadioButton square;
 private JRadioButton message;
 private JTextField messageField;
 private String messageEntered = "";
 private JComboBox location;
 private JPanel top;
 private JPanel bottom;
 private JCheckBox drawColor;
 private JButton draw;
 private int newWidth;
 private int newHeight;
 private static final String locations[] = {"Centered", "Random location"};
 private boolean drawSquare;
 private Color color;

@Override
public void init() {
    setLayout(new FlowLayout());
    top = new JPanel();
    bottom = new JPanel();

    square = new JRadioButton("Draw square");
    square.addItemListener(this);
    top.add(square);

    message = new JRadioButton("Enter your message:");
    message.addItemListener(this);
    top.add(message);

    messageField = new JTextField(20);
    messageField.addActionListener(this);
    top.add(messageField);

    //bottom panel
    bottom.add(new JLabel("Select where to draw:"));
    location = new JComboBox(locations);
    bottom.add(location);

    drawColor = new JCheckBox("Draw in color");
    drawColor.addItemListener(this);
    bottom.add(drawColor);

    draw = new JButton("Draw it!");
    draw.addActionListener(this);
    bottom.add(draw);

    add(top);
    add(bottom);

}

 @Override 
 public void paint(Graphics g) {
     super.paint(g); 

     g.setColor(color);

     if(drawSquare){

         g.fillRect(newWidth, newHeight, 200, 200);
     }

     else {
         g.setColor(color);
         g.drawString(messageEntered, newWidth, newHeight); // print out entered name 
     }
 }

@Override
public void actionPerformed(ActionEvent e) {

    repaint();
}

@Override
public void itemStateChanged(ItemEvent e) {
    int width = getWidth();
    int height = getHeight();
    Random rand = new Random();

    if(e.getSource() == location && location.getSelectedIndex() == 1) {

         newWidth = rand.nextInt(width) + 20;
         newHeight = rand.nextInt(height) + 20;
    }

    if (e.getSource() == location && location.getSelectedIndex() == 0) {
        newWidth = width/2-100;
        newHeight = height/2-100;
    }

    if(e.getSource() == square && e.getStateChange() == ItemEvent.SELECTED) {
        drawSquare = true;
    }

    if(e.getSource() == message && e.getStateChange() == ItemEvent.SELECTED) {
        drawSquare = false;
        messageEntered = messageField.getText();
    }

    if (e.getSource() == drawColor && e.getStateChange() == ItemEvent.SELECTED) {
        color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    }

    if(e.getSource() == drawColor && e.getStateChange() == ItemEvent.DESELECTED) {
        color = Color.BLACK;
    }


}

Suggestions?

  • 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-13T05:17:26+00:00Added an answer on June 13, 2026 at 5:17 am

    As shown here, the drawString() method requires baseline relative coordinates. This example may be helpful in understanding the geometry.

    FontMetrics fm = g.getFontMetrics();
    g.drawString(messageEntered, newWidth, newHeight + fm.getAscent());
    

    As an aside, consider overriding paintComponent() in a JPanel and adding that to your applet.

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

Sidebar

Related Questions

Can anyone give me some tips on adding a custom button for each row
Can we change the default action of the edit selected row button? Here is
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can someone help me with this scenario? *There is a button, which when tapped,
Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Does anyone know how can I replace this 2 symbol below from the string
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can anyone tell me how I can display a status message like 12 seconds

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.