【问题标题】:JFrame ResizingJFrame 调整大小
【发布时间】:2012-07-19 04:06:31
【问题描述】:

我切换到了 MigLayout,当我将窗口拖得更小时,组件会调整大小,但是当我将它拖得比启动程序时的大小时,它们不会变大。我如何使它变大变小?

public SpriteEditor() {
    SubstanceColorChooserUI col = new SubstanceColorChooserUI();
    while (mode == 0);
    setResizable(true);
    setTitle("DawnGuard Index 32/34 Editor");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(483, 374);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    textField = new JTextField();
    textField.setColumns(10);

    JButton btnLoadCache = new JButton("Load cache");
    btnLoadCache.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                String loc = textField.getText();
                if (loc.equals("")) {
                    JOptionPane.showMessageDialog(SpriteEditor.this, "Please specify a location for the cache.", "Unable to load", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!loc.endsWith("\\"))
                    loc = loc + "\\";
                cache = new Store(loc);
                loadImages();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(SpriteEditor.this, "Cache failed to initialize.\n" + e.getMessage(), "Unable to load", JOptionPane.ERROR_MESSAGE);
            }
        }
    });

    JSplitPane splitPane = new JSplitPane();
    splitPane.setDividerLocation(150);
    splitPane.setContinuousLayout(true);

    JPanel panellie = new JPanel();
    panellie.setLayout(new BorderLayout(0, 0));
    panel = new ImagePanel(null);

    scrollPane_1 = new JScrollPane();
    panellie.add(scrollPane_1, "Center");

    scrollPane_1.setViewportView(panel);
    splitPane.setRightComponent(panellie);

    JPanel panel_1 = new JPanel();
    splitPane.setLeftComponent(panel_1);
    panel_1.setLayout(new BorderLayout(0, 0));

    JScrollPane scrollPane = new JScrollPane();
    panel_1.add(scrollPane, BorderLayout.CENTER);

    model = new DefaultListModel<ListedImage>();
    list = new JList<ListedImage>(model);
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent arg0) {
            if (arg0.getValueIsAdjusting())
                return;
            ListedImage img = list.getModel().getElementAt(list.getSelectedIndex());
            panel.setImage(img.getImage());
        }
    });
    scrollPane.setViewportView(list);

    progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    progressBar.setDoubleBuffered(true);

    contentPane.setLayout(new MigLayout("", "[328px][13px][112px]", "[23px][279px][14px]"));
    contentPane.add(textField, "cell 0 0,growx,aligny center");
    contentPane.add(btnLoadCache, "cell 2 0,growx,aligny top");
    contentPane.add(splitPane, "cell 0 1 3 1,grow");
    contentPane.add(progressBar, "cell 0 2 3 1,grow");

}

【问题讨论】:

  • 我自己,我建议不要尝试手动编码 GroupLayout。使用嵌套的 JPanel 更好,这些 JPanel 使用更简单的布局管理器或下载 MigLayout 并使用它。
  • 我在 Eclipse 中使用 windowbuilder
  • 编辑:我只使用了 MigLayout,但它无法调整大小,只能变小。
  • 许多布局确实尊重给定组件的首选大小,尽管有某些布局,如果使用得当,可以给你想要的结果,例如GridLayoutGridBagLayout(in this you can control the size of each component)/@ 987654324@

标签: java swing user-interface jframe miglayout


【解决方案1】:

在你的布局定义中:

contentPane.setLayout(new MigLayout("", "[328px][13px][112px]", "[23px][279px][14px]"));

你可以试着告诉布局用布局约束fill 填充,现在你应该告诉当面板随着行/列约束grow 增长时应该增长哪些列和行。所以...

contentPane.setLayout(new MigLayout("fill", "[328px, grow][13px][112px]", "[23px][279px, grow][14px]"));

【讨论】:

    【解决方案2】:

    我猜最大尺寸太小了,你试图设置一个比对象的最大尺寸更大的尺寸。尝试设置最大宽度和最大高度。祝你好运。

    【讨论】:

    • 只需调用 JFrame 对象的 setMaximumSize 方法。您应该将 Dimension 对象传递给它,如下所示: myFrame.setMinimumSize(new Dimension(800,550));其中 myFrame 是您的 JFrame 对象。
    • 你在哪里设置的最大尺寸?我在您的代码中没有看到该部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多