java - 如何让我的 JButtons 位于左侧而不是中间

标签 java swing jbutton

我在这里有点迷失,当我运行程序时,按钮位于输入的中间,而不是直接位于与其对齐的底部。我不确定我做错了什么。我还试图找出如何获取输入的统计信息,例如最小值和最大值以及平均字长。我有点迷失,谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Arrays;
public class CopyTextPanel extends JPanel
{
private JTextField input;
private JLabel output, inlabel, outlabel;
private JButton compute, clear;
private JPanel panel, panel1, panel2;

public CopyTextPanel()
{
    setLayout (new BoxLayout(this, BoxLayout.Y_AXIS));
    inlabel = new JLabel("Input:  ");
    outlabel = new JLabel("Text Statistics Result: ");
    input = new JTextField (50);
    output = new JLabel();
    compute = new JButton("Compute Statistics");
    compute.addActionListener (new ButtonListener());
    clear = new JButton("Clear Text");
    clear.addActionListener (new ButtonListener());
    panel = new JPanel();
    panel1 = new JPanel();
    panel2 = new JPanel();

    output.setMaximumSize (new Dimension(500, 30));
    output.setMinimumSize (new Dimension(500, 30));
    panel.setMaximumSize (new Dimension(500, 30));
    panel.setMinimumSize (new Dimension(500, 30));
    panel.setBackground(Color.gray);
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(inlabel);
    panel.add(input);
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
    panel1.add(compute);
    panel1.add(clear);
    add (Box.createRigidArea (new Dimension(0, 10)));
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
    panel2.add(outlabel);
    panel2.add(output);
    setMaximumSize (new Dimension(600, 250));
    setMinimumSize (new Dimension(600, 250));
    setBackground(Color.white);
    add (panel);
    add (panel1);
    add (panel2);
}

private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent event)
    {

        String inputText = input.getText();//sets what is typed by the user 
to a String object

        String[] splitted = inputText.trim().split("\\p{javaSpaceChar}
{1,}");//makes a String array, and trims all the whitespaces from the user 
input
        int numberofWords = splitted.length;//it then sets splitted length 
to an integer
        String numow = Integer.toString(numberofWords);// finally it makes 
the numberofwords int into a string 
        Arrays.sort(splitted);
        if (event.getSource()==compute)//if the user presses the compute 
button
        {
            output.setText (numow + " words; " );//the output is the string 
of integers of how many words were typed
        }
        else//if the user presses another button
            input.setText(" "); // clear text filed after copying
    }
}
}

最佳答案

所以根据想要的结果...

Desired Result

我建议考虑使用一系列面板,专门生成每行的布局要求,然后将它们包装到一个容器中。

就我而言,GridBagLayout 提供了最灵活的选项,同时也提供了更复杂的选项之一。

此示例仅关注布局要求,您稍后必须弄清楚如何应用该功能。

Example output

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class Test {

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

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws HeadlessException {
            setBorder(new EmptyBorder(5, 5, 5, 5));
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2, 2, 2, 2);
            JPanel fieldPane = new JPanel(new GridBagLayout());

            JTextField inputField = new JTextField("Computer Science 1");
            fieldPane.add(new JLabel("Input Text:"), gbc);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            fieldPane.add(inputField, gbc);

            JPanel buttonPane = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.insets = new Insets(2, 2, 2, 2);

            buttonPane.add(new JButton("Computer Statistics"), gbc);
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.weightx = 1;
            buttonPane.add(new JButton("Clear Text"), gbc);

            JPanel resultsPane = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.insets = new Insets(2, 2, 2, 2);

            resultsPane.add(new JLabel("Text Statistics Result:"));
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.weightx = 1;
            resultsPane.add(new JLabel("3 words"), gbc);

            gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(fieldPane, gbc);
            add(buttonPane, gbc);
            add(resultsPane, gbc);
        }

    }

}

我强烈建议您查看Laying Out Components Within a Container有关各种布局管理器如何工作的更多详细信息

关于java - 如何让我的 JButtons 位于左侧而不是中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46920337/

相关文章:

java - java中的等效 cURL --data-binary @filename -H "Content-Type:text/csv"

java - 我应该在哪里正确声明我的 "count"可用?

java - doClick() 和西蒙 : All buttons will unpress at the same time instead of individually

java - GUI 中的 JButton

java - 如何设置 JScrollPane 只显示特定数量的行

java - jOOQ代码生成器

java - 如何在android中使用java制作ImageView?

swing:为了更好的可移植性使用哪些字体?

java - 如何将 Applet 转换为 JFrame

java - 我如何区分 actionPerformed 中的两个不同的 JButton?