0

The following code is from my school assignment. Made an object of mainFrame class which suppose to show some text fields and buttons. The problem is everything I create falls in the same line and unfortunately, I can't use java FX. How can I make them fall in different lines?Thank you

    public class TempConverter extends MainFrame {
            public TempConverter() {
            super("Simple Temperature Converter Application", 350,160);
              //  add(new ConverterPanel());
            }
            static public void main(String[] args) {
                TempConverter tcApp = new TempConverter();
                tcApp.display();
            }
   }

//==mainFrame

     /**
      * Created by Shirin on 2/4/17.
      */
    import javax.swing.*;
    import java.awt.*;

    public class MainFrame extends JFrame{

         JPanel panel = new JPanel();


        private JLabel lab;
        JButton toCelsius = new JButton("To Celsius");
        JButton toFahrenheit = new JButton("To Fahrenheit ");
        TextField fahrenheitText = new TextField(), celsiusText = new TextField();
        TextField toCelsiusText = new TextField(),toFahrenheitText = new TextField();


            public MainFrame(String title, int width, int height) {
                super(title);
                this.setFrame(width, height);
            }


            public void display() {

                setVisible(true);
            }

            private void setFrame(int width, int height) {
                setSize(width, height);
                setLocationRelativeTo(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


                this.addComponents();


            }
            public void addComponents(){
                fahrenheitText.setSize(42,42);
                fahrenheitText.setLocation(0,0);


                toCelsius.setSize(42,42);

                toCelsiusText.setLocation(2,2);
                toCelsiusText.isEnabled();

                celsiusText.setSize(200,100);
                celsiusText.isEnabled();
                celsiusText.setLocation(6,6);
                toFahrenheit.setSize(42,42);

                panel.add(fahrenheitText);
                panel.add(toCelsius);
                panel.add(toCelsiusText);
                panel.add(celsiusText);
                panel.add(toFahrenheit);

                this.add(panel);

            }

        }
shirin
  • 152
  • 1
  • 14
  • 1
    the solution to your question is to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's. You can find the layout manager tutorial here: [Layout Manager Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), and you can find links to the Swing tutorials and to other Swing resources here: [Swing Info](http://stackoverflow.com/questions/tagged/swing). – Hovercraft Full Of Eels Feb 04 '17 at 19:23
  • 1
    `The problem is everything I create falls in the same line` - the default layout manager for a JPanel is the `FlowLayout` which places all the components on the same line which is why you need to use a different layout manager or combination of nested panels with different layout managers. Also, don't use "TextField", that is an AWT component. You should be using `JTextField` which is the Swing component. – camickr Feb 04 '17 at 19:46

0 Answers0