I have this part of my code. I have a JLabel and a JButton that are in a panel. The I place that panel into another panel that has a border layout and I set the panel containing the label and button to be "NORTH", but the JLabel does not show. When I romove the button it does. Why? How can I fix this? Thanks. Here is my code:

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


public class ISU implements ActionListener{

	JFrame mainFrame;
	JLabel descriptionLabel;
	public JLabel pictureLabel;
	JButton launchApplication;
	public ISU (){
		mainFrame = new JFrame ("Physics ISU Library");
		JPanel mainPane = new JPanel ();
		JPanel descriptPane = new JPanel ();
		JPanel picPane = new JPanel ();
		
		pictureLabel = new JLabel ();
		
		JMenuBar menuBar = new JMenuBar ();
		JMenu motionItem = new JMenu ("Motion");
		
		JMenu energyItem = new JMenu ("Energy");
		
		JMenu soundItem = new JMenu ("Sound");
		JMenuItem transverseWave = new JMenuItem ("Transverse Wave"); transverseWave.setActionCommand("transverseWave");transverseWave.addActionListener(this);
		JMenuItem longitudinalWave = new JMenuItem ("Longitudinal Wave"); longitudinalWave.setActionCommand("longitudinalWave"); longitudinalWave.addActionListener (this);
		JMenuItem dopplerEffect = new JMenuItem ("Doppler Effect"); dopplerEffect.setActionCommand("dopplerEffect"); dopplerEffect.addActionListener(this);
		
		JMenu electricityItem = new JMenu ("Electricity");
		
		JMenu magnetismItem = new JMenu ("Magnetism");
		
		
		soundItem.add(transverseWave); soundItem.add(longitudinalWave); soundItem.add(dopplerEffect);
		
		menuBar.add(motionItem); menuBar.add(energyItem); menuBar.add(soundItem); menuBar.add(electricityItem); menuBar.add(magnetismItem);
		mainFrame.add("North",menuBar);
		
		descriptPane.setLayout (new BorderLayout ());
		
		
		descriptionLabel = new JLabel ("This is the program that demonstrates how a blank works.");
		
		launchApplication = new JButton ("Launch");
		
		descriptPane.add(descriptionLabel);
		descriptPane.add (launchApplication);
		
		mainPane.setLayout(new BorderLayout ());
		mainPane.add (descriptPane, BorderLayout.NORTH);
		
		mainPane.add(picPane, BorderLayout.CENTER);
		
		mainFrame.add (mainPane);
		
		mainFrame.setSize (600,500);
		mainFrame.setVisible(true);
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ISU isu = new ISU ();
	}
}

Try...

JPanel Top_Panel = new JPanel (new FlowLayout ());
Top_Panel.add (descriptionLabel);
Top_Panel.add (launchApplication);
descriptPane.add (Top_Panel, BorderLayout.NORTH);
Comments
very good ++

Thanks. I realized that I had the border layout code still added to the top pane. Thanks for your help.

I am having trouble printing a sine wave (transverse wave) a wave that kind of looks like this ~

except wounded. you can just goolge it here i will do it for you. here


that is what it is supposed to look like. One cycle, but my program gives me more and I dont know why. Thanks for the help

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


public class TransverseWaveProgram extends JPanel implements ActionListener{

		
		int [] xaxis = new int [360];
		int [] yaxis = new int [360];
	
		public void TransverseWave (){
			for (int i = 0; i < 360; i++){
				xaxis[i] = i;
				yaxis[i] = ((int)(Math.round( (Math.sin(i) * - 1) * 100)));
				yaxis[i] = yaxis[i] + 300;
			}
			
			repaint();
			
		}
		
		
		
		
		
		public void paintComponent (Graphics g){
			super.paintComponent (g);
			
			for (int i = 0; i < 360; i++){
				
				g.fillPolygon (xaxis,yaxis, 360);
				
			}
			
			
		}

		
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
		
		
		
	
}

if you are wondering this is the basic equation

y = sinx

again. Thanks for the help.

Edited 5 Years Ago by sirlink99: n/a

is there an easier way to graph it? I need it to stretch over a normal distance. Ho would I accomplish this? is there a graphing option that uses mathematical terms to graph a function at a certain point and allows you to vary it throughout the program? Also sometimes when I use my code it glitches and it flashes the drawing and then disappears. What could be the cause of this? Thanks.

You just need to subdivide the period of your function over a certain number of pixels and build a Path2D from the (x,y) value pairs.

Here are some fragments of pseudocode to get you started:

periodValueOfPixel = x/graphWidth*periodOfFunction

Looping over an x range you calc

y = Math.sin(periodValueOfPixel)*graphHeight

and connect your path to the next point

path.lineTo(x,y)

When your path is complete use Graphics2D.draw(Shape) to render it.

Edited 5 Years Ago by Ezzaral: n/a

This article has been dead for over six months. Start a new discussion instead.