Search code examples
javaswingboxlayout

Struggling with BoxLayout in Java Swing


Whats up guys I'm struggling to understand how to implement BoxLayout or any layout in java swing. I have been looking at tutorials on oracle and others but i just can't get it to work. This is for an assignment in college so I would appreciate not giving me the solution straight up but maybe just point me in the right direction. I think the problem is my code is different to what is in the tutorials so I'm not sure what goes where.

import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.BoxLayout;


class Window extends JFrame implements ActionListener 
{


    JPanel panel = new JPanel();
    JTextField input = new JTextField(10);
    JButton but1 = new JButton ("Convert");
    JLabel label = new JLabel();
    JTextArea output = new JTextArea(1, 20);



    public static void main(String[] args)
        {
            Window gui = new Window();
            String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

        }


    public Window()
        {
            super("Swing Window");
            setSize(500, 200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            panel.add(input);
            but1.addActionListener(this);
            add(panel);
            panel.add(output);
            label.setText ("please enter celsius to be converted to Fahrenheit");
            panel.add(but1);
            panel.add(label);
            setVisible(true);   
        }

    public void actionPerformed(ActionEvent event)
    {
        String inputStr = input.getText();
        inputStr = inputStr.trim();
        double input = Double.parseDouble(inputStr);
        double fahrenheit = input * 1.8 + 32;


        if (event.getSource() == but1)
        {
            output.setText("Here is degrees celsius " + input + " converted `to Fahrenheit: " + fahrenheit);`
        }
    }


}

There is the executable code.


Solution

  • I skimmed your code, but did not execute it. As others already mentioned in the comments, it is helpful for you to describe what the program does and to describe what you expect/want it to do. Otherwise we are just guessing as to what is wrong and what would be correct.

    From my reading of your code, I think you see nothing displayed. Correction: you did call add(); as indicated in one of your more recent comments Here are some notes/explanations:

    • Your method addComponentsToPane() is never called, thus you never create any BoxLayout objects
    • Recommendation: variable names begin with lowercase, and don't name a variable the same as a class; it easily creates confusion when reading the code. Thus don't name the argument Window.
    • Your method addComponentsToPane(), if it were called, creates a Layout object and sets it on the component passed to it but does not actually add any components. The name is thus misleading.
    • A JFrame's content pane has a BorderLayout by default. When components are added without any additional constraints, the BorderLayout chooses to place the components in a certain order (starting with BorderLayout.CENTER). See documentation of the JFrame class where it says the default is BorderLayout.
    • A JPanel, when created with the default constructor has a FlowLayout by default.
    • Since your program never changed the panel's layout manager, this is what gives you the left-to-right flow that you observed.
    • You want to set the panel's layout to a vertical BoxLayout before you add components to it