Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调整大小会使事情出错_Java_Swing_Jinternalframe - Fatal编程技术网

Java 调整大小会使事情出错

Java 调整大小会使事情出错,java,swing,jinternalframe,Java,Swing,Jinternalframe,我想创建一个包含一些组件的JInternalFrame 我的目标是用Java设计一个bash控制台 我的框架由4个组件组成: JTextArea包含在JScrollPane中 JLabel,文本为“Cmd:” JTextField 带有文本“Send”的JButton 我有以下代码: Box box = Box.createHorizontalBox(); box.add(Box.createVerticalStrut(5)); box.add(this.cmd_label); box.ad

我想创建一个包含一些组件的JInternalFrame

我的目标是用Java设计一个bash控制台

我的框架由4个组件组成:

  • JTextArea包含在JScrollPane中
  • JLabel,文本为“Cmd:”
  • JTextField
  • 带有文本“Send”的JButton
我有以下代码:

Box box = Box.createHorizontalBox();
box.add(Box.createVerticalStrut(5));
box.add(this.cmd_label);
box.add(Box.createVerticalStrut(5));
box.add(this.cmd_input);
box.add(Box.createVerticalStrut(5));
box.add(this.submit);
box.add(Box.createVerticalStrut(5));

Box mainBox = Box.createVerticalBox();
mainBox.add(Box.createHorizontalStrut(5));
mainBox.add(this.result_scroll);
mainBox.add(Box.createHorizontalStrut(5));
mainBox.add(box);
mainBox.add(Box.createHorizontalStrut(5));
add(mainBox);
因此,当框架未最大化时,我有一个正确的外观:

但当我将其最大化时,所有组件的位置都不正确:

所以,我的问题是:我如何设置组件的权重以每次固定它们的位置,或者,我如何固定它


谢谢。

我认为使用
边框布局会更好。在边界布局中,指定为中心构件的构件将展开以填充尽可能多的空间,而其他构件将保持其首选尺寸

int hgap = 5;
int vgap = 5;
internalFrame.getContentPane().setLayout(new BorderLayout(hgap, vgap));
internalFrame.getContentPane().add(this.result_scroll, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(this.cmd_label);
bottomPanel.add(this.cmd_input);
bottomPanel.add(this.submit);
internalFrame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);

请尝试以下代码,这种行为是否可以例外:

import java.awt.*;
import javax.swing.*;

public class LayoutExample
{
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("LAYOUT EXAMPLE");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout(5, 5));
        centerPanel.setBorder(
                    BorderFactory.createEmptyBorder(2, 2, 2, 2));
        JTextArea tarea = new JTextArea(10, 10);
        tarea.setBackground(Color.DARK_GRAY.darker());  
        tarea.setForeground(Color.WHITE);
        tarea.setCaretColor(Color.WHITE);
        tarea.setLineWrap(true);

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

        JPanel footerPanel = new JPanel();
        footerPanel.setLayout(new BorderLayout(5, 5));
        footerPanel.setBorder(
                    BorderFactory.createEmptyBorder(2, 2, 2, 2));       
        JLabel cmdLabel = new JLabel("Cmd : ");         
        JTextField tfield = new JTextField(10);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(
                    BorderFactory.createEmptyBorder(2, 2, 2, 2));
        JButton sendButton = new JButton("SEND");

        footerPanel.add(cmdLabel, BorderLayout.LINE_START);
        footerPanel.add(tfield, BorderLayout.CENTER);
        buttonPanel.add(sendButton);
        footerPanel.add(buttonPanel, BorderLayout.LINE_END);

        frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
        frame.getContentPane().add(footerPanel, BorderLayout.PAGE_END);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {   
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new LayoutExample().createAndDisplayGUI();
            }
        });
    }
}
输出:


基本问题这里是我认为JTrdFieldMax布局提示中的一个bug:它在水平和垂直维度上都是无界的。后者对于设计用于显示单行文本的组件来说完全是胡说八道。要修复,请创建子类并让其返回高度的pref,如:

JTextField cmdInput = new JTextField() {

    @Override
    public Dimension getMaximumSize() {
        Dimension max = super.getMaximumSize();
        max.height = getPreferredSize().height;
        return max;
    }

};
由于BoxLayout尊重maxSize,因此现在将仅为机顶盒提供额外高度

从长远来看,考虑转换为第三方管理者,允许在一对一的面板方法中进行微调。是的,我现在最喜欢的是MigLayout。将以下几行代码与上面的所有嵌套和边界技巧进行比较,并从中获得乐趣:-)


谢谢,它可以工作,但是JTextField不再占用整个宽度。我该怎么做呢?使用另一个
BorderLayout
作为底部面板:)+1,我喜欢这种方法,这是我第一眼看到这个问题时想到的。+1用于再次修复核心Java组件;-)对于LayoutManager:我真的不能在MigLayout和DesignGridLayout之间做出决定。。。
MigLayout layout = new MigLayout("wrap 3, debug", 
        "[][grow, fill][]", // 3 columns, middle column filled and allows growing
        "[grow, fill][]"); // two rows, first filled and allows growing

JComponent content = new JPanel(layout);
// the scrollPane in the first row spanning all columns
// and growing in both directions 
content.add(new JScrollPane(new JTextArea(20, 20)), "span, grow");
// auto-wrapped to first column in second row
content.add(new JLabel("Cmd:"));
content.add(new JTextField());
content.add(new JButton("Submit"));