Hi everyone ...
I hope that all of u r OK...
This is the second semester for me in JAVA ..
I have an assignment to create a GUI using JAVA and I almost finish but the problem is the teacher asked to use any component that she didn't teach ... My frame is for calculating the body mass and I used (button,labels,textfields) can anyone give me a suggestion...

Recommended Answers

All 6 Replies

you could use a combination of: JMenuBar, JMenu and JMenuItem as an alternative for the function of your JButtons. This way you won't have to "invent" unneccessary functions for your application

Ezzaral....stultuske... Thank u so much.. I used menu but there is a smal error and I couldn't solve.. I will copy the program and I hope u will help

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class BodyMass extends JFrame implements ActionListener
{
    JTextField T1,T2,T3,T4;
    public BodyMass()
    {
        JPanel P1=new JPanel();
        JPanel P2=new JPanel();
        JPanel P3=new JPanel();
        FlowLayout W=new FlowLayout();
        Font N=new Font("Tahoma",Font.BOLD,20);
        T1=new JTextField(5);
        T2=new JTextField(5);
        T3=new JTextField(15);
        T4=new JTextField(10);
        add(P1,BorderLayout.NORTH);
        add(P2,BorderLayout.CENTER);
        add(P3,BorderLayout.SOUTH);
        JLabel L1=new JLabel("Hight in cm");
        JLabel L2=new JLabel("Weight in kg");
        JLabel L3=new JLabel("Your Body Mass is");
        JLabel L4=new JLabel("Your weight is");
        JButton B=new JButton("Calculate Body Mass");
        ImageIcon pic=new ImageIcon("body.jpg");
        JLabel L5=new JLabel(pic);
        JMenu M=new JMenu("your mode");
        JMenuItem m=new JMenuItem("MAGENTA");
        JMenuItem k=new JMenuItem("CYAN");
        JMenuItem o=new JMenuItem("ORANGE");
        JMenuItem p=new JMenuItem("PINK");
        JMenuBar bar=new JMenuBar();
        bar.add(M);
        setJMenuBar(bar);
        P1.setLayout(W);
        P3.setLayout(W);
        L1.setOpaque(true);
        L1.setBackground(Color.ORANGE);
        L1.setForeground(Color.RED);
        L1.setFont(N);
        L2.setOpaque(true);
        L2.setBackground(Color.ORANGE);
        L2.setForeground(Color.RED);
        L2.setFont(N);
        L3.setOpaque(true);
        L3.setBackground(Color.ORANGE);
        L3.setForeground(Color.RED);
        L3.setFont(N);
        L4.setOpaque(true);
        L4.setBackground(Color.ORANGE);
        L4.setForeground(Color.RED);
        L4.setFont(N);
        B.setBackground(Color.PINK);
        B.setForeground(Color.GRAY);
        B.setFont(N);
        B.setToolTipText("Clic to calculate your body mass");
        B.addActionListener(this);
        m.addActionListener(this);
        k.addActionListener(this);
        o.addActionListener(this);
        p.addActionListener(this);
        P1.add(L1);
        P1.add(T1);
        P1.add(L2);
        P1.add(T2);
        P1.add(B,BorderLayout.CENTER);
        P2.add(L5);
        P3.add(L3);
        P3.add(T3);
        P3.add(L4);
        P3.add(T4);
        M.add(m);
        M.add(k);
        M.add(o);
        M.add(p);
    }
    public void actionPerformed(ActionEvent ae)
    {
        double a=Double.parseDouble(T1.getText());
        double b=Double.parseDouble(T2.getText());
        T3.setText(Double.toString(b/((a/100)*(a/100))));
        double c=Double.parseDouble(T3.getText());
        if(c<16.5)
            T4.setText("Too slim");
        else if((c>=16.5)&&(c<20))
            T4.setText("Slim");
        else if((c>=20)&&(c<25))
            T4.setText("Normal");
        else if((c>=25)&&(c<30))
            T4.setText("heavy");
        else if ((c>=30)&&(c<40))
            T4.setText("Too heavy");
        else 
            T4.setText("Obisity");

        String n=ae.getActionCommand();
        if (n.equals("MAGENTA"))
        P1.setBackground(Color.MAGENTA);
        P2.setBackground(Color.MAGENTA);
        P3.setBackground(Color.MAGENTA);
        else if (n.equals("CYAN"))
        P1.setBackground(Color.CYAN);
        P2.setBackground(Color.CYAN);
        P3.setBackground(Color.CYAN);
        else if (n.equals("ORANGE"))
        P1.setBackground(Color.ORANGE);
        P2.setBackground(Color.ORANGE);
        P3.setBackground(Color.ORANGE);
        else
        P1.setBackground(Color.PINK);
        P2.setBackground(Color.PINK);
        P3.setBackground(Color.PINK);
    }
    public static void main(String[] args)
    {
        BodyMass BM=new BodyMass();
        BM.setVisible(true);
        BM.setTitle("Calculating Body Mass");
        BM.setSize(900,450);
        BM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

The code compiles if you make the below changes

JTextField T1,T2,T3,T4;
JPanel P1;
JPanel P2;
JPanel P3;
public BodyMass()
{
	P1=new JPanel();
	P2=new JPanel();
	P3=new JPanel();

Enclose your multiple statements after if and else in curly braces.

An example:

else if (n.equals("ORANGE"))
{
P1.setBackground(Color.ORANGE);
P2.setBackground(Color.ORANGE);
P3.setBackground(Color.ORANGE);
}

Please use code tags.

thomas_naveen.... thank u sooooooooooooooo much...it worked..
sorry.. I don't know how to use code tags..

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.