Java JLabel的新产品线?

Java JLabel的新产品线?,java,swing,jlabel,flowlayout,Java,Swing,Jlabel,Flowlayout,我已经完成了我的任务,我只有一个小问题。“标题”行与程序的其余部分显示在同一行中。下面是它的外观: 我需要“整数计算”在这条线上,居中,以及它下面的所有内容。理性的也一样。我知道这是因为FlowLayout,但是有没有办法在这上面使用一条新的线?我在某个地方读到可以使用JTextArea,但我不知道如何实现它,尤其是对于我目前拥有的所有文本字段 import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.e

我已经完成了我的任务,我只有一个小问题。“标题”行与程序的其余部分显示在同一行中。下面是它的外观:

我需要“整数计算”在这条线上,居中,以及它下面的所有内容。理性的也一样。我知道这是因为FlowLayout,但是有没有办法在这上面使用一条新的线?我在某个地方读到可以使用JTextArea,但我不知道如何实现它,尤其是对于我目前拥有的所有文本字段

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

    }

}

请注意,这与JLabel拥有一个新行无关,而与智能使用布局管理器放置组件有关。这里的秘诀是简单地使用更多的JPanel,每个都有自己的布局,从而组合布局。例如

// 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);

我想这与使用不同的布局管理器和其他东西有关,但我不知道如何开始,也不知道从哪里开始。这就像一个魅力,所以谢谢你,我一定会在未来更多地研究这一点!