Jump to content

Java Applet, how to make a text box after clicking a button

erty176

Hello all,

 

I was wondering, I have the following code.

How do I get a textbox to show when a button is clicked? I know I need actionperformed and then in that specify what I want done, its just specifying what I want done that's the problem.
package game;import java.applet.Applet;import java.awt.Button;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Polygon;import java.awt.event.ActionListener;import java.awt.event.ItemListener;public class Test extends Applet implements ActionListener, ItemListener{	private final int WIDTH = 1200, HEIGHT = 650; // sets the size of the applet	private Font fnt25p = new Font("Impact", Font.PLAIN, 25); // sets a font to 25 points	private Font fnt48p = new Font("Sans Serif", Font.PLAIN, 48); // sets another font to 48 points	private Font fnt36p = new Font("Times New Roman", Font.PLAIN, 36); // sets another font to 36 points	private Polygon poly; // used for triangle	private Button b_computer;//evaluates according to choices made	private Button b_gpu;	private Button b_psu;	private Button b_cpu;	private Button b_ram;	private Button b_mother;	private Color black = new Color(0, 0, 0);//makes the color black	private Color red = new Color(255, 0, 0);//makes the color red	private Color orange = new Color(255, 120, 25);//makes the color orange	private Color white = new Color(255, 255, 255);//makes the color white	private Color green = new Color(0, 255, 0);//makes the color green	private Color blue = new Color(0, 0, 255);//makes the color blue		//fonts	private Font fnt12P = new Font("Times New Roman", Font.PLAIN, 12);//sets the font Times New Roman, Plain, size 12	private Font fnt24B = new Font("Times New Roman", Font.BOLD, 24);//sets the font Times New Roman, Bold, size 24		public void init()	{		setSize(WIDTH, HEIGHT);		setLayout(null);		setBackground(Color.BLACK);				//Button setup		b_computer = new Button("COMPUTER");//makes the button		b_computer.setSize(80, 20);//sets the size of the button		b_computer.setLocation(WIDTH - 700, HEIGHT - 630);//sets the location of the button		b_computer.setFont(fnt12P);//sets the font of the button		b_computer.setBackground(white);//sets the color of the button		b_computer.setForeground(black);//sets the text color		b_computer.setVisible(true);//makes button visible		b_computer.addActionListener(this);//makes the button respond to clicks		add(b_computer);//adds the button to the applet				b_gpu = new Button("GPU");//makes the button		b_gpu.setSize(80, 20);//sets the size of the button		b_gpu.setLocation(WIDTH - 400, HEIGHT - 510);//sets the location of the button		b_gpu.setFont(fnt12P);//sets the font of the button		b_gpu.setBackground(white);//sets the color of the button		b_gpu.setForeground(black);//sets the text color		b_gpu.setVisible(true);//makes button visible		b_gpu.addActionListener(this);//makes the button respond to clicks		add(b_gpu);//adds the button to the applet				b_psu = new Button("PSU");//makes the button		b_psu.setSize(80, 20);//sets the size of the button		b_psu.setLocation(WIDTH - 1100, HEIGHT - 260);//sets the location of the button		b_psu.setFont(fnt12P);//sets the font of the button		b_psu.setBackground(white);//sets the color of the button		b_psu.setForeground(black);//sets the text color		b_psu.setVisible(true);//makes button visible		b_psu.addActionListener(this);//makes the button respond to clicks		add(b_psu);//adds the button to the applet				b_cpu = new Button("CPU");//makes the button		b_cpu.setSize(80, 20);//sets the size of the button		b_cpu.setLocation(WIDTH - 1100, HEIGHT - 510);//sets the location of the button		b_cpu.setFont(fnt12P);//sets the font of the button		b_cpu.setBackground(white);//sets the color of the button		b_cpu.setForeground(black);//sets the text color		b_cpu.setVisible(true);//makes button visible		b_cpu.addActionListener(this);//makes the button respond to clicks		add(b_cpu);//adds the button to the applet				b_ram = new Button("RAM");//makes the button		b_ram.setSize(80, 20);//sets the size of the button		b_ram.setLocation(WIDTH - 700, HEIGHT - 410);//sets the location of the button		b_ram.setFont(fnt12P);//sets the font of the button		b_ram.setBackground(white);//sets the color of the button		b_ram.setForeground(black);//sets the text color		b_ram.setVisible(true);//makes button visible		b_ram.addActionListener(this);//makes the button respond to clicks		add(b_ram);//adds the button to the applet				b_mother = new Button("MOTHERBOARD");//makes the button		b_mother.setSize(80, 20);//sets the size of the button		b_mother.setLocation(WIDTH - 400, HEIGHT - 260);//sets the location of the button		b_mother.setFont(fnt12P);//sets the font of the button		b_mother.setBackground(white);//sets the color of the button		b_mother.setForeground(black);//sets the text color		b_mother.setVisible(true);//makes button visible		b_mother.addActionListener(this);//makes the button respond to clicks		add(b_mother);//adds the button to the applet							}	public void paint(Graphics g)	{	String image = "computer.jpg"; // IMAGE IMPORT OF COMPUTER	Image imageProblem1 = getImage(getCodeBase(), image); // FINDS LOCATION OF IMAGE	g.drawImage(imageProblem1, WIDTH-700, HEIGHT-625, this); // puts in the image		String image2 = "download.jpg"; // image import of cpu	Image imageProblem2 = getImage(getCodeBase(), image2);	g.drawImage(imageProblem2, WIDTH -1100, HEIGHT - 500, this);		String ram= "computer-ram.jpg"; // image import of ram	Image imageProblem3 = getImage(getCodeBase(), ram);	g.drawImage(imageProblem3,WIDTH - 700, HEIGHT - 400, this);		String gcard = "nvidia-quadro-graphics-card.png"; // image import of graphics card	Image imageProblem4 = getImage(getCodeBase(), gcard);	g.drawImage(imageProblem4,WIDTH -400, HEIGHT - 500, this);		String psu = "psu2.jpg"; // image import of power supply unit	Image imageProblem5 = getImage(getCodeBase(), psu);	g.drawImage(imageProblem5,WIDTH - 1100, HEIGHT - 250, this);		String mboard ="motherboard.jpg"; // image import of motherboard	Image imageProblem6 = getImage(getCodeBase(), mboard);	g.drawImage(imageProblem6, WIDTH - 400, HEIGHT - 250, this);		g.setColor(Color.orange);	g.drawLine(WIDTH -860, HEIGHT -500, WIDTH - 700, HEIGHT-625); //pc to cpu	g.drawLine(WIDTH - 700, HEIGHT - 400, WIDTH - 700, HEIGHT -625); // pc to ram	g.drawLine(WIDTH - 400, HEIGHT - 500, WIDTH -500, HEIGHT -625); // pc to graphics card	g.drawLine(WIDTH - 900, HEIGHT - 250, WIDTH -700, HEIGHT -425); // pc to power supply unit	g.drawLine(WIDTH - 400, HEIGHT -250, WIDTH-500, HEIGHT-425); // pc to motherboard	g.drawLine(WIDTH -860, HEIGHT -500, WIDTH-700, HEIGHT -625);		}			}

 

Does not having a second parenthesis around something bother anyone else as much as it does me? (Like if this statement was missing a second side)

Link to comment
Share on other sites

Link to post
Share on other sites

There are different ways of doing this

 

1 way would be to have the textbox always there and set it hidden by default and visible on button click like so:

// on initsomeTextbox.setVisible(false);// on button action peforomedsomeTextbox.setVisible(true);

another way would be to add() the textbox not on init but instead on button action performed which I believe you should know how to do

 

I'd use add()only once, so lets say if you were to make the textbox appear only on button click and you won't be making it disappear ever again.

If you were to make the textbox disappear after you make it appear then I'd recommend using someComponent.setVisible() instead.

 

So preferably you'd only use add() once otherwise if you were to make the component appear/disappear multiple times so like[onclick, appear. again? disappear. again? appear. again? disappear...] then you would change the visibility instead.

 

Just in case for the action performed part you'd do as following

someButton.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {//		set textbox visible	}});
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×