Hi everyone!
So I have a problem with java applet.

import java.awt.*;
import java.applet.*;
// import an extra class for the ActionListener
import java.awt.event.*;

// Tells the applet you will be using the ActionListener methods.

public class project extends Applet implements ActionListener
{

	TextField nameField;
	Checkbox cb1;
	Checkbox cb2;
	Checkbox cb3;
	Checkbox cb4;
	Checkbox option;
	Choice cho;
	Button okButton;
	Button resetButton;
	String s;
	FlowLayout f;

	
	public void init(){
		setLayout(new FlowLayout());
		okButton = new Button("Submit");
		resetButton = new Button("Reset");
	
		nameField = new TextField("Your name",35);
		radioGroup = new CheckboxGroup();
		
		add(new Label("What's your name?"));
				add(nameField);
		
		ch1 = new Checkbox("Music", radioGroup,false);
		ch2 = new Checkbox("Computers", radioGroup,true);
		ch3 = new Checkbox("Sport", radioGroup,false);
		ch4 = new Checkbox("TV", radioGroup,false);
		
		add(new Label("What country are you from?"));
		Choice c = new Choice();
       c.addItem("Australia");
       c.addItem("USA");
       c.addItem("EU");
       add(c);
       
       add(new Label("What's your hobby?"));
		add(ch1);
		add(ch2);
		add(ch3);
		add(ch4);
                add(okButton);
                add(resetButton);
		okButton.addActionListener(this);
		resetButton.addActionListener(this);

	}

	public void paint(Graphics g){

		if (ch1.getState()) g.setColor(Color.red); //I DIDN'T KNOW WHAT TO WRITE
		else if (ch2.getState()) g.setColor(Color.blue);
		else if (ch3.getState()) g.setColor(Color.yellow);
		else g.setColor(Color.green);
		g.drawString(nameField.getText(),20,180);
	}
	  public boolean combocox(Event e, Object o){ //CHOICE FOR THE COMBOBOX
        if (e.target instanceof Choice){
           cho  = (Choice) e.target;
           MenuEvent=cho.getItem(cho.getSelectedIndex());
           repaint();
        }

	public void actionPerformed(ActionEvent evt) {
		//if (evt.getSource() == okButton){ - IT SHOULD OUTPUT ALERT OR MESSAGE BOX
                	repaint();
		}
		else if (evt.getSource() == resetButton){
		       // resetButton.setLabel("It's default!"); - IT SHOULD RESET ALL THE FIELDS
		//	nameField.setText("This is deafault text!");
			repaint();
		}
	}

	
}

What it should do is first to imput your name in text field, then to select from which country are you (from the combo box), then to check some of the checkboxes about your hobby, and then to have 2 buttons reset and submit. Reset should reset all the fields and submit should output a message box with this contenet : Hello (your name from the text box), from (country from combobox), your hobby is (checkboxes)....and ...and.
And to check for empty text field.
I'll be soooo grateful if someone help me with this, because I really want to learn how to do this.
Thank you.

Recommended Answers

All 4 Replies

Here is a little ugly looking applet that may do what you want.
Please fix the layout, I didn't bother with it too much.
Hope this gives you an idea of how to do applets.
The code uses Swing, but you should easily be able to port it to AWT, if thats what you need to use.

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

public class Main extends JApplet implements ActionListener
{
	private JButton btnSubmit, btnReset;
	private JTextField txtName;
	private JComboBox cbCountry;
	private JCheckBox cb1, cb2, cb3, cb4;
	
	public Main() {
		super();
		createFields();
		layoutFields();
	}
	
	private void createFields() {
		btnSubmit = new JButton("Submit");
		btnReset = new JButton("Reset");
		txtName = new JTextField(35);
		cb1 = new JCheckBox("Music");
		cb2 = new JCheckBox("Computers");
		cb3 = new JCheckBox("Sports");
		cb4 = new JCheckBox("TV");
		cbCountry = new JComboBox(new Object[] { "", "Australia", "USA", "EU" });
		
		btnSubmit.addActionListener(this);
		btnReset.addActionListener(this);
	}
	
	private void layoutFields() {
		JPanel pane = new JPanel();
		BoxLayout layout = new BoxLayout(pane, BoxLayout.Y_AXIS);
		pane.setLayout(layout);
		
		pane.add(new JLabel("Your name:", JLabel.WEST));
		pane.add(txtName);
		
		pane.add(new JLabel("Where are you from?"));
		pane.add(cbCountry);
		
		JPanel cbpanel = new JPanel();
		cbpanel.setLayout(new BoxLayout(cbpanel, BoxLayout.X_AXIS));
		cbpanel.add(cb1);
		cbpanel.add(cb2);
		cbpanel.add(cb3);
		cbpanel.add(cb4);
		
		pane.add(new JLabel("What are your hobbies?"));
		pane.add(cbpanel);
		
		JPanel btnpane = new JPanel();
		btnpane.setLayout(new BoxLayout(btnpane, BoxLayout.X_AXIS));
		btnpane.add(btnSubmit);
		btnpane.add(btnReset);
		
		pane.add(btnpane);
		
		add(pane);
	}
	
	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();
		if (source == btnSubmit)
			submitForm();
		else if (source == btnReset)
			resetForm();
	}
	
	private void submitForm() {
		String name = txtName.getText();
		String country = (String)cbCountry.getSelectedItem();
		String hobby = "";
		boolean and = false;
		if (cb1.isSelected()) {
			hobby += cb1.getLabel();
			and = true;
		}
		if (cb2.isSelected()) {
			if (and)
				hobby += " and ";
			hobby += cb2.getLabel();
			and = true;
		}
		if (cb3.isSelected()) {
			if (and)
				hobby += " and ";
			hobby += cb3.getLabel();
			and = true;
		}
		if (cb4.isSelected()) {
			if (and)
				hobby += " and ";
			hobby += cb4.getLabel();
			and = true;
		}
		String msg = "Hello " + name + " from " + country + ". Your hobbies are: " + hobby;
		JOptionPane.showMessageDialog(this, msg);
	}
	
	private void resetForm() {
		txtName.setText("");
		cbCountry.setSelectedItem("");
		cb1.setSelected(false);
		cb2.setSelected(false);
		cb3.setSelected(false);
		cb4.setSelected(false);
	}
}
commented: great! +3

Thank you, thank you so much!
The layout isn't that important, I just need to see how things work.
I just have one little problem.
On line 12 (public Main)it says:

invalid method declaration; return type required

I've tried some return values, but it shows the same error.

Oh sorry, I fixed line 12.
On line 13 it says:

call to super must be first statement in constructor

Help?

Solved. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.