Search code examples
javaswingjlabelflowlayout

New line for JLabel?


I've finished my assignment, I just have one tiny problem. The "title" lines show up in the same row as the rest of the program. Here's how it looks:

enter image description here

I need the "Integer Calculation" to be in that line, centered, and everything else under it. Same for the rational one. I understand that it's like this because of the FlowLayout, but is there any way to use a new line on this? I read somewhere that JTextArea could be used, but I have no idea how to implement it, especially with all the text fields I have currently.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class HW3TabbedPane extends JFrame {
private JPanel intPanel = new JPanel();
private JPanel intPanel1 = new JPanel();
private JPanel intPanel2 = new JPanel();
private JPanel ratPanel = new JPanel();
private JPanel ratPanel1 = new JPanel();
private JPanel ratPanel2 = new JPanel();
private JTabbedPane tab = new JTabbedPane();
private FlowLayout flow = new FlowLayout();
private BorderLayout intBorder = new BorderLayout();
private BorderLayout ratBorder = new BorderLayout();
private JButton intAdd = new JButton("Add");
private JButton intSubstract = new JButton("Substract");
private JButton intMultiply = new JButton("Multiply");
private JButton intDivide = new JButton("Divide");
private JButton ratAdd = new JButton("Add");
private JButton ratSubstract = new JButton("Substract");
private JButton ratMultiply = new JButton("Multiply");
private JButton ratDivide = new JButton("Divide");
private JLabel intLabel = new JLabel("Integer Calculation");
private JLabel ratLabel = new JLabel ("Rational Calculation");
private JLabel n1IntLabel = new JLabel("Number 1");
private JLabel n2IntLabel = new JLabel("Number 2");
private JLabel n1RatLabel = new JLabel("Number 1");
private JLabel n2RatLabel = new JLabel("Number 2");
private JLabel intResultLabel = new JLabel("Result");
private JLabel ratResultLabel = new JLabel("Result");
private JTextField n1IntField = new JTextField();
private JTextField n2IntField = new JTextField();
private JTextField intResultField = new JTextField();
private JTextField n1RatField = new JTextField();
private JTextField n2RatField = new JTextField();
private JTextField ratResultField = new JTextField();
private Integer x;
private Integer y;
private Double w;
private Double z;

public HW3TabbedPane() {
    intPanel1.setLayout(flow);
    intPanel1.add(intLabel);
    intPanel1.add(n1IntLabel);
    intPanel1.add(n1IntField);
    intPanel1.add(n2IntLabel);
    intPanel1.add(n2IntField);
    intPanel1.add(intResultLabel);
    intPanel1.add(intResultField);
    intPanel2.add(intAdd);
    intPanel2.add(intSubstract);
    intPanel2.add(intMultiply);
    intPanel2.add(intDivide);
    ratPanel1.setLayout(flow);
    ratPanel1.add(ratLabel);
    ratPanel1.add(n1RatLabel);
    ratPanel1.add(n1RatField);
    ratPanel1.add(n2RatLabel);
    ratPanel1.add(n2RatField);
    ratPanel1.add(ratResultLabel);
    ratPanel1.add(ratResultField);
    ratPanel2.add(ratAdd);
    ratPanel2.add(ratSubstract);
    ratPanel2.add(ratMultiply);
    ratPanel2.add(ratDivide);
    n1IntField.setColumns(3);
    n2IntField.setColumns(3);
    intResultField.setColumns(4);
    intResultField.setEditable(false);
    n1RatField.setColumns(3);
    n2RatField.setColumns(3);
    ratResultField.setColumns(4);
    ratResultField.setEditable(false);

    intPanel.setLayout(intBorder);
    intPanel.add(intPanel1, BorderLayout.NORTH);
    intPanel.add(intPanel2, BorderLayout.SOUTH);
    ratPanel.setLayout(ratBorder);
    ratPanel.add(ratPanel1, BorderLayout.NORTH);
    ratPanel.add(ratPanel2, BorderLayout.SOUTH);

    JTabbedPane tab = new JTabbedPane();
    tab.addTab("Integer Operations", intPanel);
    tab.addTab("Rational Operations", ratPanel);

    add(tab);


     intAdd.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int res;
                x = Integer.parseInt(n1IntField.getText());
                y = Integer.parseInt(n2IntField.getText());
                res = x+y;
                intResultField.setText(""+res);
            }

            });

     intSubstract.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int res;
                x = Integer.parseInt(n1IntField.getText());
                y = Integer.parseInt(n2IntField.getText());
                res = x-y;
                intResultField.setText(""+res);
            }

            });

     intMultiply.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int res;
                x = Integer.parseInt(n1IntField.getText());
                y = Integer.parseInt(n2IntField.getText());
                res = x*y;
                intResultField.setText(""+res);
            }

            });
     intDivide.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int res;
                x = Integer.parseInt(n1IntField.getText());
                y = Integer.parseInt(n2IntField.getText());
                res = x/y;
                intResultField.setText(""+res);
            }

            });

     ratAdd.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                double res;
                w = Double.parseDouble(n1RatField.getText());
                z = Double.parseDouble(n2RatField.getText());
                res = w+z;
                ratResultField.setText(""+res);
            }

            });

     ratSubstract.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                double res;
                w = Double.parseDouble(n1RatField.getText());
                z = Double.parseDouble(n2RatField.getText());
                res = w-z;
                ratResultField.setText(""+res);
            }

            });

     ratMultiply.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                double res;
                w = Double.parseDouble(n1RatField.getText());
                z = Double.parseDouble(n2RatField.getText());
                res = w*z;
                ratResultField.setText(""+res);
            }

            });

     ratDivide.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                double res;
                w = Double.parseDouble(n1RatField.getText());
                z = Double.parseDouble(n2RatField.getText());
                res = w/z;
                ratResultField.setText(""+res);
            }

            });
     }



 public static void main(String[] args) {
        HW3TabbedPane frame = new HW3TabbedPane();
        frame.setTitle("Tabbed Pane");
        frame.setSize(400, 350);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

Solution

  • Note that this has nothing to do with JLabel having a new line and all to do with smart use of layout managers to place components. The secret here is to simply use more JPanels each with its own layout and thereby combine layouts. For e.g.,

    // Center the text in the JLabel using SwingConstants
    private JLabel intLabel = new JLabel("Integer Calculation", SwingConstants.CENTER); 
    
    // ...
    
    
    public HW3TabbedPane() {
        // GridLayout to hold JPanels in one column and in several rows
        intPanel1.setLayout(new GridLayout(0, 1));
        intPanel1.add(intLabel);  // add the title JLabel to the top
    
        // next row's JPanel
        JPanel nextJPanel = new JPanel(); // leave it FlowLayout
        nextJPanel.add(n1IntLabel);
        nextJPanel.add(n1IntField);
        nextJPanel.add(n2IntLabel);
        nextJPanel.add(n2IntField);
        nextJPanel.add(intResultLabel);
        nextJPanel.add(intResultField);
    
        // add to the intPanel1
        intPanel1.add(nextJPanel);