如何阻止JTextField和JTextLabel在JPanel上截断

时间:2017-12-22 14:07:52

标签: java swing jpanel jtextfield jscrollpane

每次按下JTextFields时,我JLabelsJPanel都会添加到JButton(从左到右)。但是,添加的每个新JTextFieldJLabel都会变得越来越小。我该如何解决这个问题?

此外,我想向JScrollPane添加JPanel,但遇到问题。

     public class MyExample 
{
    // Field members
    static JPanel panel = new JPanel();
    static Integer indexer = 1;
    static List<JLabel> listOfLabels = new ArrayList<JLabel>();
    static List<JTextField> listOfTextFields = new ArrayList<JTextField>();
    static JScrollPane scrollPane = new JScrollPane( panel );

    public static void main(String[] args)
    {       
        // Construct frame
        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        frame.setPreferredSize(new Dimension(2000, 2000));
        frame.setTitle("My Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(scrollPane);

        // Frame constraints
        GridBagConstraints frameConstraints = new GridBagConstraints();

        // Construct button
        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ButtonListener());

        // Add button to frame
        frameConstraints.gridx = 0;
        frameConstraints.gridy = 0;
        frame.add(addButton, frameConstraints);

        // Construct panel
        panel.setPreferredSize(new Dimension(1000, 1000));
        panel.setLayout(new GridBagLayout());
        panel.setBorder(LineBorder.createBlackLineBorder());

        // Add panel to frame
        frameConstraints.gridx = 0;
        frameConstraints.gridy = 1;
        frameConstraints.weighty = 1;
        frame.add(panel, frameConstraints);

        // Pack frame
        frame.pack();

        // Make frame visible
        frame.setVisible(true);
    }

    static class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent arg0) 
        {       
            // Clear panel
            panel.removeAll();

            // Create label and text field
            JTextField jTextField = new JTextField();
            jTextField.setSize(100, 200);
            listOfTextFields.add(jTextField);
            listOfLabels.add(new JLabel("Label " + indexer));

            // Create constraints
            GridBagConstraints textFieldConstraints = new GridBagConstraints();
            GridBagConstraints labelConstraints = new GridBagConstraints();

            // Add labels and text fields
            for(int i = 0; i < indexer; i++)
            {
                // Text field constraints
                textFieldConstraints.gridx = i;
                textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                textFieldConstraints.weightx = 0.5;
                textFieldConstraints.insets = new Insets(10, 10, 10, 10);
                textFieldConstraints.gridy = 1;

                // Label constraints
                labelConstraints.gridx = i;
                labelConstraints.gridy = 0;
                labelConstraints.insets = new Insets(10, 10, 10, 10);

                // Add them to panel
                panel.add(listOfLabels.get(i), labelConstraints);
                panel.add(listOfTextFields.get(i), textFieldConstraints);
            }

            // Align components
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = indexer;
            c.weighty = 1;
            panel.add(new JLabel(), c);

            // Increment indexer
            indexer++;
            panel.updateUI();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

  

但是,添加的每个新JTextField和JLabelthat都会变得越来越小。我该如何解决这个问题?

panel.setPreferredSize(new Dimension(1000, 1000));

不要设置首选尺寸。如果大小是固定的,那么当您添加更多组件时,他们需要缩小以适应允许的空间。

请勿尝试设置任何组件的大小。让布局管理器完成其工作并确定面板的首选大小。

创建JTextField时,代码应该是:

//JTextField jTextField = new JTextField();
JTextField jTextField = new JTextField(10);

这将允许文本字段确定其自己的首选大小,以显示大约10个字符。

panel.updateUI();

不要使用updateUI()。当您更改LAF时,Swing会在内部使用它。当您删除/添加应该使用的组件时:

panel.revalidate();
panel.repaint();

答案 1 :(得分:0)

对于以panel作为视口的JScrollPane,您不应该将面板添加到框架中 - 将其设置为视口(就像您在JScrollPane构造函数中所做的那样)并添加JScrollPane足够了。添加面板本身可能是导致问题的原因。

至于缩小的问题,我仍然试图理解你的布局代码 - 你使用GridBagLayout似乎有点过于复杂。也许你可以画一幅关于你想要布局外观的简单草图?