Tried java sun forums - maybe you can help?

Soldato
Joined
17 Mar 2005
Posts
4,042
Location
Home
I need help with a bit of java codeing. I have a borderlayout program with a menu bar which contains two menus. When i click on menu button i want to call a new class file which is where im stuck.

This is my main class which contains the border. Further after this code is the class i want menuNew to call.

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

public class Main extends JFrame implements ActionListener
{
private JPanel JPanelNorth, JPanelCenter, JPanelWest, JPanelSouth, JPanelEast;

JMenuItem menuItem;
JMenu menu, submenu;
JMenuItem menuSave, menuQuit, menuAbout;
JMenuItem menuNew;
JFrame frame;
ActionListener actionListener;
JMenuBar menuBar;
private JPanel jMenuNorth;
bmitest myscreen;

public boolean RIGHT_TO_LEFT = false;

public void addComponentsToPane(Container pane)

{
if (!(pane.getLayout() instanceof BorderLayout))
{
pane.add(new JLabel("Container doesn't use BorderLayout!"));
return;
}

if (RIGHT_TO_LEFT)
{
pane.setComponentOrientation(
java.awt.ComponentOrientation.RIGHT_TO_LEFT);
}
JPanel JPanelNorth = new JPanel();
JPanelNorth.setPreferredSize(new Dimension(200, 200));
JPanelNorth.setBackground(new Color(0,0,0));
pane.add(JPanelNorth, BorderLayout.PAGE_START);

jMenuNorth = new JPanel();
pane.add(jMenuNorth, BorderLayout.PAGE_START);
jMenuNorth.setBackground(new Color(245,245,255));

menuBar = new JMenuBar();
menu = new JMenu ("File");

JMenuItem menuNew = new JMenuItem("New");
menu.add(menuNew);
menuNew.addActionListener(this);
menuBar.add(menu);

JMenuItem menuSave = new JMenuItem("Save");
menu.add(menuSave);

JMenuItem menuQuit = new JMenuItem("Quit");
menu.add(menuQuit);
//menuQuit.addActionListener(this);
menuBar.add(menu);


menu = new JMenu ("Help");
menuAbout = new JMenuItem("About");
menu.add(menuAbout);
//menuAbout.addActionListener(this);
menuBar.add(menu);

//Make the center component big, since that's the
//typical usage of BorderLayout.
JPanel JPanelCenter = new JPanel();
JPanelCenter.setPreferredSize(new Dimension(700, 700));
JPanelCenter.setBackground(new Color(100,100,100));
pane.add(JPanelCenter, BorderLayout.CENTER);


JPanel JPanelWest = new JPanel();
JPanelWest.setPreferredSize(new Dimension(200, 200));
JPanelWest.setBackground(new Color(200,200,200));
pane.add(JPanelWest, BorderLayout.LINE_START);

JPanel JPanelSouth = new JPanel();
JPanelSouth.setPreferredSize(new Dimension(200, 20));
JPanelSouth.setBackground(new Color(10,200,150));
pane.add(JPanelSouth, BorderLayout.PAGE_END);

JPanel JPanelEast = new JPanel();
JPanelEast.setPreferredSize(new Dimension(200, 200));
JPanelEast.setBackground(new Color(255,255,255));
pane.add(JPanelEast, BorderLayout.LINE_END);




}
private void createAndShowGUI()
{
//Create and set up the window.

JFrame window = new JFrame("BorderLayout");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the content pane.
addComponentsToPane(window.getContentPane());
window.setJMenuBar(menuBar);

//Display the window.
window.setSize(1024, 720);
window.setVisible(true);
}

public void actionPerformed(ActionEvent arg0)
{
if (arg0.getSource()== menuNew)
{
if(myscreen == null)
{
myscreen = new bmitest();
System.out.println("hello");
}
}

}


public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
Main m = new Main();

public void run()
{
m.createAndShowGUI();
}
});
}



}











Class file i want to call.








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


public class bmitest extends JFrame
{


private static final long serialVersionUID = 1L;


public static void main(String[] args)
{
bmitest window = new bmitest();
window.setVisible(true);
}


// Declare and initialize instance variables that are
// referred to when the program is running.
private JTextField _mField = new JTextField(4); // height
private JTextField _kgField = new JTextField(4); // weight
private JTextField _bmiField = new JTextField(4); // BMI


public bmitest()
{
System.out.println("hellobmi");

// Create button and add action listener.
JButton bmiButton = new JButton("Compute BMI");
bmiButton.addActionListener(new BMIListener());

// Set layout and add components.
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(new JLabel("Weight in kilograms"));
content.add(_kgField);
content.add(new JLabel("Height in meters"));
content.add(_mField);
content.add(bmiButton);
content.add(new JLabel("Your BMI is"));
content.add(_bmiField);

// Set the window characteristics.
setContentPane(content);
setTitle("Body Mass Index");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); // Do layout.
setLocationRelativeTo(null); // Center window.
}


private class BMIListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double kilograms = Double.parseDouble(_kgField.getText());
double meters = Double.parseDouble(_mField.getText());
int bmi = (int)computeBMI(kilograms, meters);
_bmiField.setText("" + bmi);
}
}


public static double computeBMI(double weight, double height)
{
return weight / (height*height);
}
}








Thanks in advance
 
Last edited:
I suggest you post again this time only posting the bits of the code that are actually relevant, since I doubt many people can be bothered to go through that lot themselves.
 
O soz, i thought it might have been easier if people could understand the code.

Well the part thats now working is


public void actionPerformed(ActionEvent arg0)
{
if (arg0.getSource()== menuNew)
{
if(myscreen == null)
{
myscreen = new bmitest();
System.out.println("hello");
}
}

}



The program runs without errors but doesn't call this class.
 
First of, you need to remove the main method from the bmitest class, its just confusing the whole issue, Secondly why in you main method in your Main class are you trying to implement a Runnable interface, just create your class and call
m.createAndShowGUI(); similar to how you have done it in bmitest.run

Its also not a good idea to call a class Main, its just confusing.
There is also a second redundant call to menuBar.add(menu); in addComponentsToPane, there is no need for this second call.

The reason your call isnt working is to do with your check in the actionPerformed method,
if (arg0.getSource()== menuNew)
This is not going to work as menuNew is always null, you declare it at the top of the class but you never define it, you are defining a local varaible called menuNew when you create your menu.

to setup the menu you need this
Code:
        menuBar = new JMenuBar();
        menu = new JMenu ("File");
        menuNew = new JMenuItem("New");
        menuNew.addActionListener(this);
        menuSave = new JMenuItem("Save");
        menuQuit = new JMenuItem("Quit");
        menu.add(menuNew);
        menu.add(menuSave);
        menu.add(menuQuit);
        menuBar.add(menu);

your actionListener should look like this
Code:
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource()== menuNew)
        {
            if(myscreen == null)
            {
                myscreen = new bmitest();
                myscreen.setVisible(true);
            }
        }
    }

and you main method like this:
Code:
    public static void main(String[] args)
    {
        (new Main()).createAndShowGUI();
    }

don't forget to remove the main method from bmitest.

there are loads of things I could tell you but just doing the above gets it working for now.
 
Back
Top Bottom