Hi,
learning swing at the moment and was following a tutorial off the sun website, made mine with eclipse instead of netbeans. but all I get when I try to run it is the following in the Console window:
gap= 4
gap= 0

Not sure what is going on here. I am pretty sure it is something small as I followed the tutorial bang on, any help would be great. Thanks

package learn;

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


public class CelsiusConverterGUI extends JFrame {
	public CelsiusConverterGUI() {
		initComponents();
	}

	private void button1ActionPerformed(ActionEvent e) {
	    //Parse degrees Celsius as a double and convert to Fahrenheit.
	    int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
	            * 1.8 + 32);
	    fahrenheitLabel.setText(tempFahr + " Fahrenheit");

	}

	private void initComponents() {
		celsiusLabel = new JLabel();
		fahrenheitLabel = new JLabel();
		tempTextField = new JTextField();
		button1 = new JButton();

		//======== this ========
		setTitle("Celsius Converter");
		Container contentPane = getContentPane();

		//---- celsiusLabel ----
		celsiusLabel.setText("Celsius ");
		celsiusLabel.setFont(celsiusLabel.getFont().deriveFont(celsiusLabel.getFont().getStyle() | Font.BOLD));

		//---- fahrenheitLabel ----
		fahrenheitLabel.setText("Farenheit");
		fahrenheitLabel.setFont(fahrenheitLabel.getFont().deriveFont(fahrenheitLabel.getFont().getStyle() | Font.BOLD));

		//---- button1 ----
		button1.setText("Convert");
		button1.setFont(button1.getFont().deriveFont(button1.getFont().getStyle() | Font.BOLD));
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button1ActionPerformed(e);
			}
		});

		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
		contentPane.setLayout(contentPaneLayout);
		contentPaneLayout.setHorizontalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
						.addComponent(button1, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
						.addComponent(tempTextField, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
						.addComponent(celsiusLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(fahrenheitLabel, GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
					.addContainerGap())
		);
		contentPaneLayout.setVerticalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createSequentialGroup()
					.addGap(23, 23, 23)
					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
						.addComponent(tempTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(celsiusLabel))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(button1)
						.addComponent(fahrenheitLabel))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		pack();
		setLocationRelativeTo(getOwner());
	
	}


	private JLabel celsiusLabel;
	private JLabel fahrenheitLabel;
	private JTextField tempTextField;
	private JButton button1;
	
}

Recommended Answers

All 6 Replies

I cannot see a main() method in the code that you have posted, also you have not made your JFrame visible using the setVisible(boolean) method.

Yes , I thought it was strange without a main so I tried inserting the public void main... a few different ways but still could not get it to work - could you tell me how you would incorporate a main method into this program? Thanks for the help

> few different ways
And what were they ?

>how you would incorporate a main method into this program?
By writing:

public static void main(String [] args)

Yes, I put that in underneath where i declare my main class with the proper curly brackets, didn't work. I put it in at the bottom and tried to get the main to call the class I had just made and a few other ways but couldn't figure out the proper way. I knew the code just did not know where to put it or what to write in my main function...

private JLabel celsiusLabel;
private JLabel fahrenheitLabel;
private JTextField tempTextField;
private JButton button1;

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

"Made mine with eclipse instead of netbeans"

What do you mean by that? I'd suggest you don't follow any tutorials meant to be used with netbeans if you're using Eclipse. The sun tutorials are good but if you look at all of the examples they provide, they provide the full code. You should try doing some simple tasks on whatever tutorial you happen to be viewing and make sure you can perform simple tasks yourself. For example, make sure you can make a button do something when it is clicked.

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.