简体   繁体   English

如何停止JPanel上的JTextField和JTextLabel被截断

[英]How to stop JTextField and JTextLabel from truncating on JPanel

I have JTextFields and JLabels added to my JPanel (from left to right) every time the JButton is pressed. 每次按下JButton时,都会将JTextFieldsJLabels添加到我的JPanel (从左到右)。 However, every new JTextField and JLabel that is added becomes smaller and smaller. 但是,添加的每个新JTextFieldJLabel变得越来越小。 How do I fix this? 我该如何解决?

Also I would like to add a JScrollPane to the JPanel but having problems doing so. 我也想向JPanel添加一个JScrollPane ,但是这样做有问题。

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

However, every new JTextField and JLabelthat is added becomes smaller and smaller. 但是,添加的每个新JTextField和JLabel变得越来越小。 How do I fix this? 我该如何解决?

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

Don't set the preferred size. 不要设置首选大小。 If the size is fixed then as you add more components they need to shrink to fit in the allowed space. 如果大小固定,则当您添加更多组件时,它们需要缩小以适合允许的空间。

Don't attempt to set the size of any component. 不要尝试设置任何组件的大小。 Let the layout manager do its job and determine the preferred size of the panel. 让布局管理器完成其工作,并确定面板的首选大小。

When creating a JTextField the code should be something like: 创建JTextField时,代码应类似于:

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

This will allow the text field to determine its own preferred size to dispaly about 10 characters. 这将允许文本字段确定其自己的首选大小,以显示约10个字符。

panel.updateUI();

Don't use updateUI(). 不要使用updateUI()。 That is used internally by Swing when you change the LAF. 更改LAF时,Swing在内部使用该功能。 When you remove/add components you should be using: 删除/添加组件时,应使用:

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

As for the JScrollPane with panel as a viewport, you should not be adding the panel to the frame at all - setting it as the viewport (like you're doing in the JScrollPane constructor) and adding the JScrollPane is sufficient. 至于将panel作为视口的JScrollPane,则根本不应该将面板添加到框架中-将其设置为视口(就像在JScrollPane构造函数中所做的那样),并添加JScrollPane就足够了。 Adding the panel itself may be the cause of your problem. 添加面板本身可能是导致问题的原因。

As for the shrinking problem, I am still trying to understand your layout code - your use of GridBagLayout seems a tad overcomplicated to me. 至于缩小的问题,我仍在尝试了解您的布局代码-您对GridBagLayout的使用对我来说似乎有点复杂。 Maybe you can draw a simple sketch of how you would like the layout look? 也许您可以绘制一个简单的草图,以了解布局的外观?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM