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;
}