调整JPanel大小会导致其他组件错误地调整大小

时间:2017-08-10 18:15:07

标签: java swing jpanel jtextarea gridbaglayout

我无法分享我的实际代码,但简介是我有两个并排的JPanel,都有Flow Layouts。您可以单击一个组件,并让左侧JPanel在两种尺寸之间切换。它开始变大,如果单击该按钮,则另一个面板上的所有内容都会完全调整大小以适应额外的空间。然而,当我再次点击时,左侧JPanel变大,但右侧DON的内容调整大小,将部分内容从屏幕上移开。

这个错误只发生在我的一些标签上,我认为它与调整大小问题的JPanel包含更多带有Flow或Grid Bag布局的JPanel这一事实有关。

我已经尝试删除违规的JPanels,调整大小,然后重新放入,没有工作。我试过把JPanels拿出来,清理它们,调整大小,重新填充它们,重新填充它们,这也是行不通的。

如果我完全取出嵌套的JPanels,一切都会调整好。

我已经能够使用此示例代码稍微复制此问题,但无法提供我的实际内容:

import java.awt.EventQueue;

import javax.swing.JFrame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JPanel;

import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

public class ResizeExample {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ResizeExample window = new ResizeExample();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ResizeExample() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 734, 605);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
            FormFactory.DEFAULT_COLSPEC,
            ColumnSpec.decode("default:grow"),},
        new RowSpec[] {
            RowSpec.decode("default:grow"),}));

    JPanel panel = new JPanel();
    panel.setBackground(Color.DARK_GRAY);
    frame.getContentPane().add(panel, "1, 1, fill, fill");
    FormLayout small = new FormLayout(new ColumnSpec[] {
            ColumnSpec.decode("50px"),},
        new RowSpec[] {
            RowSpec.decode("default:grow"),});

    FormLayout big = new FormLayout(new ColumnSpec[] {
            ColumnSpec.decode("200px"),},
        new RowSpec[] {
            RowSpec.decode("default:grow"),});


    panel.setLayout(big);

    JButton btnNewButton = new JButton("  ");



    JPanel panel_1 = new JPanel();
    frame.getContentPane().add(panel_1, "2, 1, fill, fill");
    panel_1.setLayout(new FormLayout(new ColumnSpec[] {
            ColumnSpec.decode("default:grow"),},
        new RowSpec[] {
            RowSpec.decode("default:grow"),}));

    JTextArea textArea = new JTextArea("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore "
            + "et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo "
            + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
            + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
    textArea.setLineWrap(true);
    textArea.setEditable(false);

    panel_1.add(textArea, "1, 1, fill, center");

    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel.removeAll();
            panel_1.removeAll();
            if(panel.getLayout().equals(big))
                panel.setLayout(small);
            else
                panel.setLayout(big);
            panel.add(btnNewButton, "1, 1, center, center");
            panel_1.add(textArea, "1, 1, fill, center");
            frame.getContentPane().revalidate();
            frame.getContentPane().repaint();
        }
    });
    panel.add(btnNewButton, "1, 1, center, center");

    frame.setResizable(false);
}

}

0 个答案:

没有答案