guys, i have been trying to do this for 4 days straight... i'm not getting anywhere... i really need ur help. i can do the formulas and the action listener and all that, but the only thing that i seem to be having a problem is with the layout of the applet. all i need is a simple window to pop up, that has 2 text boxes for temperatures and a button that converts. i cant get it to be the way i want it to. i've been trying so hard. please help me. this is what i have so far

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Converter extends JApplet implements ActionListener
{

    private double accumulator = 0;
    private double store1 = 0;
    private double store2 = 0;

    public void init()
    {
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridBagLayout());
        JTextField inputOutputField;
        JScrollPane scroller;
        JTextArea outputArea;
        String text1 = " ";
        JLabel j1, j2, j3;


        GridBagLayout g;
        GridBagConstraints gc;

        j1 = new JLabel( "Conversion between Fahrenheit and Celcius");
        g = new GridBagLayout();
        contentPane.setLayout( g );
        gc = new GridBagConstraints ();


        gc.weightx =0;
        gc.ipadx =0;
        gc.ipady = 0;
        gc.fill =gc.NONE;
        gc.gridwidth = gc.REMAINDER;
        gc.anchor = gc.NORTH;
        g.setConstraints (j1, gc);
        contentPane.add(j1);
/*
        JPanel topButtonPanel = new JPanel();
        topButtonPanel.setBackground(Color.GRAY);
        topButtonPanel.setLayout(new GridBagLayout());

        JButton addButton = new JButton("Enter");
        addButton.addActionListener(this);
        topButtonPanel.add(addButton);


        JPanel textPanel = new JPanel();
        textPanel.setBackground(Color.BLUE);
        textPanel.setLayout(new GridBagLayout());




        j2= new JLabel("Enter");
        contentPane.add(j2);



        inputOutputField = new JTextField("Enter Fahrenheit.", 10);
        inputOutputField.setBackground(Color.WHITE);
        textPanel.add(inputOutputField);
        contentPane.add(textPanel);


        inputOutputField = new JTextField("Enter Celsius.", 10);
                inputOutputField.setBackground(Color.WHITE);
                textPanel.add(inputOutputField);
        contentPane.add(textPanel);






                textPanel.setBackground(Color.RED);
                textPanel.setLayout(new FlowLayout());

                inputOutputField = new JTextField("BULL.", 10);
                inputOutputField.setBackground(Color.WHITE);
                textPanel.add(inputOutputField);
        contentPane.add(textPanel, BorderLayout.NORTH);
*/
}

    public void actionPerformed(ActionEvent e)
    {

    }

    private static double stringToDouble(String stringObject)
    {
        return Double.parseDouble(stringObject.trim());
    }
}

Dani AI

Generated

If all you need is a small window with two text boxes and a convert button, GridBagLayout is overkill while you learn Swing. Use a simple layout (GridLayout or BoxLayout), run GUI creation on the EDT, call pack() and set the frame visible last. That removes most layout headaches. your init() approach is fine, but try a minimal JFrame first to verify the UI, then port the same layout into your applet if you must.

A compact example that does the job (reads one field, fills the other, and validates input):

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

public class TempConverter {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Temperature Converter");
                JPanel p = new JPanel(new GridLayout(3,2,6,6));
                p.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));

                JLabel fLabel = new JLabel("Fahrenheit:");
                final JTextField fField = new JTextField(10);
                JLabel cLabel = new JLabel("Celsius:");
                final JTextField cField = new JTextField(10);
                JButton convert = new JButton("Convert");

                convert.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            String fText = fField.getText().trim();
                            String cText = cField.getText().trim();
                            if (!fText.isEmpty() && cText.isEmpty()) {
                                double f = Double.parseDouble(fText);
                                cField.setText(String.format("%.2f", (f - 32) * 5.0/9.0));
                            } else if (!cText.isEmpty() && fText.isEmpty()) {
                                double c = Double.parseDouble(cText);
                                fField.setText(String.format("%.2f", c * 9.0/5.0 + 32));
                            } else {
                                JOptionPane.showMessageDialog(frame, "Fill exactly one field.", "Input", JOptionPane.INFORMATION_MESSAGE);
                            }
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(frame, "Enter a valid number.", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                });

                p.add(fLabel); p.add(fField);
                p.add(cLabel); p.add(cField);
                p.add(new JLabel()); p.add(convert);

                frame.setContentPane(p);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getRootPane().setDefaultButton(convert);
                frame.setVisible(true);
            }
        });
    }
}

Troubleshooting checklist: always build UI on the EDT; call pack() before setVisible(true); use setPreferredSize sparingly; handle NumberFormatException; use frame.getRootPane().setDefaultButton(btn) so Enter works; and center with setLocationRelativeTo(null). If you must stay with JApplet, add components to getContentPane() and call revalidate() (pack() is for top-level windows only). 's example was a good pointer — use it for ideas, but start with the smaller pattern above to get the layout right quickly.

Recommended Answers

All 4 Replies

btw please use code tags next time it makes your code a heck of a lot easier to read, hope that link helps!

this isnt the exact thing i wanted but close enough... i think i can modify this a lil bit. thank alotttttttttttt!!! really really appreciate it!!

no problems, glad I could help

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.