代码之家  ›  专栏  ›  技术社区  ›  GonePhisin

如何使我的按钮位于左侧而不是中间

  •  0
  • GonePhisin  · 技术社区  · 7 年前

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

    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
        }
    }
    }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   MadProgrammer    7 年前

    所以根据预期结果。。。

    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 有关各种布局管理器工作方式的更多详细信息

        2
  •  0
  •   mertk    7 年前

    您应该使用BorderLayout之类的布局,这可能会对您有所帮助。 panel.add(new JButton("East"),BorderLayout.EAST); 等我希望这有帮助。如果你不使用布局,它们最终会随机化。