0
CODING CLUB: Challenge 1
Okay so for this challenge you need to print "hello world" Comment how you did it below... (Plus I want this to be organised by everyone not just me, so feel free to post new challenges etc.) Everything to do with the coding club must be tagged on codingclub GOOD LUCK!!! 💕
18 odpowiedzi
+ 2
Just to be helpful though, I'll simplify your program.
import javax.swing.*;
import java.awt.event.*;
public class MyFirst extends JFrame implements ActionListener{
JButton button;
public static void main(String[] args){
MyFirst gui = new MyFirst();
gui.go();
}
public void go(){
button = new JButton("Click here");
button.addActionListener(this);
getContentPane().add(button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
button.setText("Hello World");
}
}
+ 1
public class Program
{
public static void main(String[] args) {
System.out.println("Hello World!");
// Prints "Hello World!"
}
}
:D
+ 1
import javax.swing.*;
import java.awt.event;
public class HelloWord extends JFrane implements ActionListener{
public HelloWord(){
super("Hello word");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton cb = new JButton("Button");
JTextArea ta = new JTextArea(10, 11);
JPane pane = JPane();
pane.add(cb);
pane.add(ta);
add(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if(source.equals(cb)){
ta.setText("Hello World");
}
public static void main(String [] args){
HelloWorld h = new HelloWorld();
}
}
0
I'm going to answer this sith a simple "hello world" program. but after I'm going to try and make a box with button, and when pressed it pops up "hello world".
//start of code
public class Myfirst {
public static void main(String[] args) {
System.out.println("hello world");
}
}
//code ends
very simple. my second attempt should look a little better.
0
Barra, one thing with your code to pick at. You should use MyFirst when naming your class, in order to follow java standard naming convention.Other then that, good job.
0
James, all tips are welcome, thanks :) I might take a while with this pub up box with Jframe and JButton. back to the drawing board.
0
ok so getting the button was the easy part. using ActionListener I had a bit of a struggle. I'll need to work on it. I've posted it to paste bin - http://pastebin.com/B3Twu2h4
0
Barra, if I may make a recommendation based on reading your code. Go back to basics. I could be wrong, but it looks like you're jumping from hello world to a JFrame and ActionListener program. That's a huge leap, you're probably copying and pasting your code and not entirely sure what it's doing? If that's the case, don't jump so far ahead. Keep learning the basics, eventually you'll stumble on Inheritance and Interface implementation, at that point your code will make sense and you won't get so hung up on it.
0
Yeah I think your right as I had to keep looking up how to do things. my code was a complete mess and I can't believe how easy you made it look!!
0
I've been there man. I've stopped and started programming several times throughout the years. I've done the same things other people do, so I generally know what's going on. Jumping too far ahead has led to a lot of frustrations that, looking back, could have been solved by just taking it slower and making sure I knew what I was doing. Rushing to learn as fast as possible is generally a setup for failure.
0
James, a small correction
In the import statement, it's import javax.swing.*; not javac.swing.*;
0
And Barra, I learnt Java from thenewboston and I did the exact same thing...it's completely correct
0
Fixed. Lotta code to write on a phone, was bound to make a mistake somewhere
0
Yeah I learnt from TheNewBoston also but I'm a bit too far ahead on the videos as I couldn't write most of that code on my own. I'm going to put up a second challenge. I prefer James code as it's a lot simpler.
0
public class printHello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
0
Keep in mind, my code was simplified for a teaching example. The code you were writing was going towards a much more efficient program with the frame and handler being separated into classes. But, if you're not sure what's going on, writing a bit of less optimized code can help get a better understanding.
0
Can't deny that James!
0
class Shyam
{
class public void main(String s[] )
{
System.out.println("hello world");
}
}