Jump to content

Java HW help

- - - - -

  • Please log in to reply
13 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
Hello forum vaironl here,
I just started IB computer science and the programming language is java which I had only use a few times to do very simple variable programs. I knew c++ fairly all of the basics I needed to make a program , I did an allegro game with basic movement. (Just some background)

First I don't want my homework done for me not even near done, but I do need some references besides the ones I get in school well here is what I need to do.

A panel which greets the user with a message like "typer your name and then press enter"
and after that make the previous prompt be replsce with something like hi how are you + name.
Does anyone know how can I center the items aswell?

here is what I got some far....
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;



public class HelloWorldPanel extends JPanel

{

    JLabel helloMsg;

    

    public HelloWorldPanel(){

    setLayout(new FlowLayout());

    JLabel helloMsg = new JLabel("Hello World");

    add(helloMsg);

    TextField enterName = new TextField("Enter name here");

    add(enterName);

    Button Enter = new Button("Enter");

    add(Enter);

    

    }

        

    public static void main(String[] args)

    {

    JFrame frame = new JFrame("Assignment1");

    frame.setSize(250, 100);

    frame.setLocation(200, 100);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setContentPane(new HelloWorldPanel());

    frame.setVisible(true);

    

    }

    

}



#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I can see this being done easily with JDialogs in java.
How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

More specifically, showing an input dialog, then a message dialog. With these there is also no need for your jpanel/jframe. They'll show up without them.

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

I can see this being done easily with JDialogs in java.
How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

More specifically, showing an input dialog, then a message dialog. With these there is also no need for your jpanel/jframe. They'll show up without them.

So it would be the same process ?
Actionlisterner and all of that ?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
No, the optionpanes don't require actionlisteners.
You use static methods on the class: JOptionPane.showX(...)

If you have a dialog popup where the user can type stuff in, it will return the string:
String input = JOptionPane.showX(...);


#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

wim DC said:

No, the optionpanes don't require actionlisteners.
You use static methods on the class: JOptionPane.showX(...)

If you have a dialog popup where the user can type stuff in, it will return the string:
String input = JOptionPane.showX(...);

This is correct. And for what you're doing, the JOptionPane class makes your task trivial.
Inside of your main(...) I can see your problem being solved in 3 lines.

#6
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

wim DC said:

No, the optionpanes don't require actionlisteners.
You use static methods on the class: JOptionPane.showX(...)

If you have a dialog popup where the user can type stuff in, it will return the string:
String input = JOptionPane.showX(...);

lethalwire said:

This is correct. And for what you're doing, the JOptionPane class makes your task trivial.
Inside of your main(...) I can see your problem being solved in 3 lines.


Thanks allot to both of you I will read the tutorial and do my best to make this assignment since I have to turn it in tomorrow. Thanks allot.
By the way you guys might asked yourself why am I taking an IB class in CS without knowing the default programming language. The thing is that the basic one was c++ etc and was too easy

#7
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

I can see this being done easily with JDialogs in java.
How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

More specifically, showing an input dialog, then a message dialog. With these there is also no need for your jpanel/jframe. They'll show up without them.

Hey can I actually implement only the things that I need?

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I'd use:
JOptionPane (Java Platform SE 7 )
and
JOptionPane (Java Platform SE 7 )

More specifically:

public static String showInputDialog(Component parentComponent, Object message) // this dialog is where you get the user's name

//and

public static void showMessageDialog(Component parentComponent, Object message) //use this dialog to show the user's name


For the parentComponent you can pass null as the value.

#9
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
I did some improvement (not the best)

I'm still having trouble with that setLabel
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;




public class dialog extends JPanel

{

    JLabel helloMsg;

    

    public dialog(){

    setLayout(new FlowLayout());

  

    

       

    

    }

    

      

    

    

    public static void main(String[] args)

    {/*

    JFrame frame = new JFrame("Assignment1");

    frame.setSize(250, 150);

    frame.setLocation(200, 100);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setContentPane(new dialog());

    frame.setVisible(true);

  */

        JFrame frame = new JFrame ("Test");

        frame.setSize(250, 150);

        Object[] possibilities = {null};

        String name,setLabel;

        

        

            String s = (String)JOptionPane.showInputDialog(

                    frame,

                   "Nice to meet you "+ name,

                    JOptionPane.PLAIN_MESSAGE,

                    null,

                    possibilities,

                    "ham");


//If a string was returned, say so.

if ((s != null) && (s.length() > 0)) {

    setLabel("Nice to meet you" + s );

    return;

}

//If you're here, the return value was null/empty.

setLabel("finish the sentence");

        }

      

}



#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Hmmm... I think you're overdoing it.
Like the tutorial from oracle states, you don't want to limit the user's choices, so you can use another version of showInputDialog.

Quote

If you do not care to limit the user's choices, you can either use a form of the showInputDialog method that takes fewer arguments or specify null for the array of objects.
That's why I listed those 2 methods in an earlier post.

public static String showInputDialog(Component parentComponent, Object message) // this dialog is where you get the user's name

//and

public static void showMessageDialog(Component parentComponent, Object message) //use this dialog to show the user's name

You'll want to set a String s equal to the return value of showInputDialog(null, "Insert message here");
So when the user enters data, it is stored into s. Remember the String s can be empty or null so check this.

Then, you'll simply use the showMessageDialog(null, s) to display the name you want.

#11
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

Thanks for the dialog lethalwire but I decided to do it using the same code the class use (I did needed help but here it is).
Now I have to make it store up to 10 names that should be easier
   import javax.swing.*;

   import java.awt.*;

   import java.awt.event.*;




    public class HelloWorldPanel extends JPanel

   {

      JLabel helloMsg;

      String name, empty;

      JTextField box;

      JButton Enter;

       public HelloWorldPanel(){

         setLayout(new FlowLayout());

         helloMsg = new JLabel("Enter name below");

         add(helloMsg);

      

         

         

         box = new JTextField(20);

         box.setText("Name here");

         box.setHorizontalAlignment(SwingConstants.CENTER);

         add(box);

      

         Enter = new JButton("Enter");

         Enter.addActionListener(new Listener());

         Enter.setLocation(10,50);

         add(Enter);

      	

       

      

      }

       private class Listener implements ActionListener

      {

          public void actionPerformed(ActionEvent e)

         {

         empty = new String("");

         

            name = box.getText();

            if(name.equals(empty)== false) 

            helloMsg.setText("Nice to meet you, " + name);

            else if(name.equals(empty)== true)

         	helloMsg.setText("Please type in a name below");

         }

       

      }

   

      

    

    

       public static void main(String[] args)

      {

         JFrame frame = new JFrame("Assignment1");

         frame.setSize(250, 150);

         frame.setLocation(200, 100);

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         frame.setContentPane(new HelloWorldPanel());

         frame.setVisible(true);

      }

    

   }

By the way do you know why does the actionListerner has to be private?

#12
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

By the way do you know why does the actionListerner has to be private?
It doesn't have to be private. Why do you think it needs to be?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users