如何在 Java SWING 中换行开始一些事情?

How to start something on a new line in Java SWING?

我正在尝试将我的 JTextArea 设置为占据屏幕的最大水平长度,以便接下来的事情(在本例中为按钮)将从新行开始,但我不知道该怎么做它。我通过将 JTextArea 的大小设置为从 20 更改为 1000 来搞砸了,但这没有做任何事情。

如何让我的文本区域占据整个第一行,然后让我添加的下一个项目从下一行开始?这是我目前所拥有的...

MyFrame(){//constructor

        super("Simple Calculator");
        p = new JPanel();
        grid = new GridLayout(4, 4, 3, 3);
        p.setLayout(grid);
        setSize(400, 500);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setUpTextScreen();
        //create buttons
        for(int i = 0; i < buttonValues.length; i++){
            p.add(new JButton(buttonValues[i]));
        }
        add(p);
        setVisible(true);
    }

    private void setUpTextScreen() {

        textOnScreen = new JTextArea(7, 1000);
        textOnScreen.setText("0");//default
        textOnScreen.setEditable(false);
        p.add(textOnScreen);
    }

看看 Nested layouts,您可以添加一个带有 BorderLayout 的面板(不过也有其他选项)并向其添加文本区域。然后您只需要一个带有显示按钮的 GridLayout 的面板。这是一个示例:(请注意,此代码中有几行是不必要的,但它们有助于理解布局)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

public class Example extends JFrame {

    Example() {//

        super("Simple Calculator");

        // The Main Panel where the 2 other panels will be on
        JPanel mainPanel = new JPanel(new BorderLayout());

        // The textarea will be inside this panel
        JPanel areaPanel = new JPanel(new BorderLayout());

        JTextArea area = new JTextArea(
                "This is a JTextArea -Long text to show it works -Long text to show it works- -Long text to show it works- -Long text to show it works- -Long text to show it works- -Long text to show it works-");
        area.setBorder(new LineBorder(Color.BLACK));
        area.setWrapStyleWord(true);
        area.setLineWrap(true);

        // Fill the whole space of the panel with the area
        areaPanel.add(area, BorderLayout.CENTER);

        // The buttons will be inside this panel
        JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 3, 3));
        for (int i = 0; i < 16; i++) { // Adding buttons
            buttonPanel.add(new JButton("Button" + i));
        }

        // The textarea-panel should be on top of the main panel
        mainPanel.add(areaPanel, BorderLayout.NORTH);

        // The panel with the buttons should fill the remaining space
        mainPanel.add(buttonPanel, BorderLayout.CENTER);

        getContentPane().add(mainPanel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }

}

How can I get my textarea to take up the entire first row and then have the next item that I add to begin on the following row?

将您的布局分解为符合逻辑的部分。使用 BorderLayout.

从主面板开始
  1. 首先,我将使用 JTextField 作为计算器显示,而不是 JTextArea。然后您可以使用以下方式添加文本字段:mainPanel.add(textField, BorderLayout.PAGE_START);

  2. 然后使用 GridLayout 为按钮创建 JPanel。然后将按钮添加到按钮面板并使用:maonPanel.add(buttonPanel, BorderLayout.CENTER);

例如:

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

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
//              display.setCaretPosition( display.getDocument().getLength() );
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(30, 30) );
            buttonPanel.add( button );

            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(KeyStroke.getKeyStroke(text), text);
            inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

您还可以使用 html 标签,例如:

JButton button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");

或者像您得到的任何其他 JComponent。

因此您可以使用 <BR> 标签来满足您的需要。