Ask coding questions

← Back to all posts
swing Java tutorial - doesn't work, how to resolve?
GregBeutler

https://repl.it/@GregBeutler/brokentutorialJavaSWing

I copied the jswing Java tutorial and it doesn't work!

pls advise.
Greg

Voters
GregBeutler
Comments
hotnewtop
ParthaSarathi6

import java.awt.;
import java.awt.event.
;
class Main extends Frame implements ActionListener {
TextField t;
Main() {
super("Event Handling - ActionEvent");
t = new TextField();
t.setSize(30,30);
t.setBackground(Color.green);
t.setForeground(Color.red);
Button b = new Button("click Me");
b.addActionListener(this);
setSize(300,250);
setLayout(new GridLayout(2,1));
add(t); add(b);
setVisible(true);
}
public void actionPerformed (ActionEvent ae) {
t.setText("Keep Learning");

{
new Main();
}
}

EpicGamer007

Hey sorry for my bad tutorial. You need some imports that I forgot to mention. Really sorry

here are the imports:

add this to your code and hopefully this works, really sorry about making that mistake. Hope this helps :)

GregBeutler

@AbhayBhat
thanks that works.

any more tutorials?
I'm trying to take the input of the button
and make the gui display an image.
just like the FB game 'Which disney princess are you?"

EpicGamer007

@GregBeutler, , yes i do have more tutorials coming up :)

For what you are planning on doing, you need graphics. I think you should first do Image toDraw = new BufferedImage("res/image.jpg");(Research more about buffered images), then i suggest is creating a button, then adding an action listener which gets the graphics for you using this method: Graphics g = frame.getGraphics(); Note, this is a very bad way of doing graphics. I will show a better way in my upcoming tutorials.. ok, with the g object, you can do g.drawImage(0, 0, toDraw, null); which draws the image at 0, 0 on the screen.

Yeah...
That is how i suggest doing it, if you have any other questions, feel free to contact me :).

Please mark my answer as correct if it helps :)

PattanAhmed

@GregBeutler Hi,
Check this website to learn JavaSwing
Click here
Thanks!

Hope this helps