Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 415,729 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,865 people online right now.Registration is fast and FREE... Join Now!



Java GUI Rate Topic: -----

#1 chili5  Icon User is offline

  • D.I.C Lover
  • PipPipPipPipPip

Reputation: 16
  • View blog
  • Posts: 1,123
  • Joined: 28-December 07


Dream Kudos: 0

Share |

Java GUI

Posted 27 December 2008 - 04:32 AM

I'm working on this very simple problem with a Java GUI:

Quote

Have the computer ask the name of the sales representative. After clicking the "Name Update" command button (that you will create), this name will then be included in all further questions. Ask the user for both the total sales for the person as well as the commission rate. The computer will then display the commission.


I have a few panels on the form to hold my containers, but what I don't understand is why is the second panel filling the form? Also how come my text boxes are so big? Does it have to do with the BoxLayout and BorderLayout?


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

/**
 *
 * @author James
 */
class form extends JFrame implements ActionListener {

	// these go in a panel together
	JPanel pnlName = new JPanel();
	JLabel lblName = new JLabel("Enter name: ");
	JTextField txtName = new JTextField(5);
	// these go in a panel together
	JPanel pnlCost = new JPanel();
	JLabel lblCost = new JLabel(" enter cost: ");
	JTextField txtCost = new JTextField(6);

	// these go in a panel together
	JPanel pnlComm = new JPanel();
	JLabel lblComm = new JLabel(" commission: ");
	JTextField txtComm = new JTextField(6);
	JPanel pnlButtons = new JPanel();
	JButton btnUpdate = new JButton("Update Name");

	form() {
		setTitle("Commission");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		add();

		btnUpdate.addActionListener(this);
	}

	void add() {
		setLayout(new FlowLayout());
		pnlName.setLayout(new BoxLayout(pnlName, BoxLayout.Y_AXIS));
		pnlName.add(lblName);
		pnlName.add(txtName);

		pnlCost.setLayout(new BoxLayout(pnlCost, BoxLayout.Y_AXIS));
		pnlCost.add(lblCost);
		pnlCost.add(txtCost);

		pnlComm.setLayout(new BoxLayout(pnlComm, BoxLayout.Y_AXIS));
		pnlComm.add(lblComm);
		pnlComm.add(txtComm);

		pnlButtons.setLayout(new BoxLayout(pnlButtons, BoxLayout.Y_AXIS));
		pnlButtons.add(btnUpdate);

		pnlCost.setVisible(false);
		pnlComm.setVisible(false);

		setLayout(new BorderLayout());
		add(pnlName, BorderLayout.NORTH);
		add(pnlCost, BorderLayout.CENTER);
		add(pnlComm, BorderLayout.SOUTH);
		add(pnlButtons, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent evt) {
		String sCost;
		sCost = txtName.getText() + lblCost.getText();
		lblCost.setText(sCost);

		pnlCost.setVisible(true);
		pnlComm.setVisible(true);
		pnlButtons.setVisible(false);


	}
}

public class e1d {

	public static void main(String[] args) {
		form e1d = new form();
		e1d.setVisible(true);
		e1d.setSize(300, 200);
	}
}


Was This Post Helpful? 0
  • +
  • -


#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • Icon

Reputation: 110
  • View blog
  • Posts: 1,852
  • Joined: 12-December 08


Dream Kudos: 575

Re: Java GUI

Posted 27 December 2008 - 05:10 AM

Which IDE are you using?
Was This Post Helpful? 0
  • +
  • -

#3 chili5  Icon User is offline

  • D.I.C Lover
  • PipPipPipPipPip

Reputation: 16
  • View blog
  • Posts: 1,123
  • Joined: 28-December 07


Dream Kudos: 0

Re: Java GUI

Posted 27 December 2008 - 05:19 AM

netbeans but that shouldn't matter.
Was This Post Helpful? 0
  • +
  • -

#4 rajaera  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 27-December 08


Dream Kudos: 0

Re: Java GUI

Posted 27 December 2008 - 08:21 AM

View Postchili5, on 27 Dec, 2008 - 04:32 AM, said:

I'm working on this very simple problem with a Java GUI:

Quote

Have the computer ask the name of the sales representative. After clicking the "Name Update" command button (that you will create), this name will then be included in all further questions. Ask the user for both the total sales for the person as well as the commission rate. The computer will then display the commission.


I have a few panels on the form to hold my containers, but what I don't understand is why is the second panel filling the form? Also how come my text boxes are so big? Does it have to do with the BoxLayout and BorderLayout?


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

/**
 *
 * @author James
 */
class form extends JFrame implements ActionListener {

	// these go in a panel together
	JPanel pnlName = new JPanel();
	JLabel lblName = new JLabel("Enter name: ");
	JTextField txtName = new JTextField(5);
	// these go in a panel together
	JPanel pnlCost = new JPanel();
	JLabel lblCost = new JLabel(" enter cost: ");
	JTextField txtCost = new JTextField(6);

	// these go in a panel together
	JPanel pnlComm = new JPanel();
	JLabel lblComm = new JLabel(" commission: ");
	JTextField txtComm = new JTextField(6);
	JPanel pnlButtons = new JPanel();
	JButton btnUpdate = new JButton("Update Name");

	form() {
		setTitle("Commission");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		add();

		btnUpdate.addActionListener(this);
	}

	void add() {
		setLayout(new FlowLayout());
		pnlName.setLayout(new BoxLayout(pnlName, BoxLayout.Y_AXIS));
		pnlName.add(lblName);
		pnlName.add(txtName);

		pnlCost.setLayout(new BoxLayout(pnlCost, BoxLayout.Y_AXIS));
		pnlCost.add(lblCost);
		pnlCost.add(txtCost);

		pnlComm.setLayout(new BoxLayout(pnlComm, BoxLayout.Y_AXIS));
		pnlComm.add(lblComm);
		pnlComm.add(txtComm);

		pnlButtons.setLayout(new BoxLayout(pnlButtons, BoxLayout.Y_AXIS));
		pnlButtons.add(btnUpdate);

		pnlCost.setVisible(false);
		pnlComm.setVisible(false);

		setLayout(new BorderLayout());
		add(pnlName, BorderLayout.NORTH);
		add(pnlCost, BorderLayout.CENTER);
		add(pnlComm, BorderLayout.SOUTH);
		add(pnlButtons, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent evt) {
		String sCost;
		sCost = txtName.getText() + lblCost.getText();
		lblCost.setText(sCost);

		pnlCost.setVisible(true);
		pnlComm.setVisible(true);
		pnlButtons.setVisible(false);


	}
}

public class e1d {

	public static void main(String[] args) {
		form e1d = new form();
		e1d.setVisible(true);
		e1d.setSize(300, 200);
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 chili5  Icon User is offline

  • D.I.C Lover
  • PipPipPipPipPip

Reputation: 16
  • View blog
  • Posts: 1,123
  • Joined: 28-December 07


Dream Kudos: 0

Re: Java GUI

Posted 27 December 2008 - 08:29 AM

Um you quoted my code? lol
Was This Post Helpful? 0
  • +
  • -

#6 rajaera  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 27-December 08


Dream Kudos: 0

Re: Java GUI

Posted 27 December 2008 - 09:52 AM

//try to this code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {
	JLabel northLabel;
	JLabel centerLabel;
	   
	JTextField northText;   
	JTextField centerText;
	JButton southButton;

	JPanel northPanel;
	JPanel centerPanel;
	JPanel southPanel; 
	

	public Test(){
	northLabel = new JLabel("label one");
	northText = new JTextField("text");
	northText.setPreferredSize(new Dimension(100, 20));//width and height
	northPanel = new JPanel();
	
	centerLabel = new JLabel("label two");
	centerText = new JTextField("text");
	centerText.setPreferredSize(new Dimension(100, 20));
	centerPanel = new JPanel();
	centerPanel.setVisible(false);

	southButton = new JButton("click");
	southButton.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		centerPanel.setVisible(true);
		southButton.setVisible(false);
		}
	});	
	southPanel = new JPanel();
	southPanel.setLayout(new BorderLayout());
	
	northPanel.add(northLabel);
	northPanel.add(northText);
	add(northPanel, BorderLayout.NORTH);
	
	centerPanel.add(centerLabel);
	centerPanel.add(centerText);	
	add(centerPanel, BorderLayout.CENTER);

	southPanel.add(southButton);
	add(southPanel, BorderLayout.SOUTH);
	
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	pack(); // pack instead of give a size
	setVisible(true);
	}

	public static void main(String[] args){
	new Test();
	}
}


*Edited to add the [ code] tags
Hey guys... I can understand how newbies can post a question wirhout them but if you start to answer without them that will keep me busy forever

This post has been edited by pbl: 28 December 2008 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users