Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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 如何使JScrollPanel上的JPanel仅具有GridLayout填充宽度而不具有填充高度?_Java_Swing_Layout - Fatal编程技术网

Java 如何使JScrollPanel上的JPanel仅具有GridLayout填充宽度而不具有填充高度?

Java 如何使JScrollPanel上的JPanel仅具有GridLayout填充宽度而不具有填充高度?,java,swing,layout,Java,Swing,Layout,我尝试制作如下所示的布局。但我在添加新的MyPanel时遇到了一个问题。第一个MyPanel将填充其所有父面板(中间面板)。如何使MyPanel只采用所需的高度和填充宽度 public class Main extends JFrame { JPanel upPanel = new JPanel(); JPanel midPanel = new JPanel(); JPanel downPanel = new JPanel(); JButton button = new JButton("ad

我尝试制作如下所示的布局。但我在添加新的MyPanel时遇到了一个问题。第一个MyPanel将填充其所有父面板(中间面板)。如何使MyPanel只采用所需的高度和填充宽度

public class Main extends JFrame {

JPanel upPanel = new JPanel();
JPanel midPanel = new JPanel();
JPanel downPanel = new JPanel();
JButton button = new JButton("add new Panel");

Main() {
    this.setSize(800, 600);
    this.setLayout(new BorderLayout());

    this.add(upPanel, BorderLayout.PAGE_START);
    JScrollPane jsp = new JScrollPane(midPanel);
    this.add(jsp, BorderLayout.CENTER);
    this.add(downPanel, BorderLayout.PAGE_END);
    this.midPanel.setLayout(new GridLayout(0, 1, 3, 3));

    upPanel.add(button);

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            midPanel.add(new MyPanel());
            midPanel.updateUI();

        }
    });

}

public static void main(String[] args) {
    new Main().setVisible(true);
}
}
class MyPanel extends JPanel {

    MyPanel() {
        this.setLayout(new GridLayout(2, 5, 5, 5));
        this.setBorder(BorderFactory.createRaisedBevelBorder());

        this.add(new JLabel("Nomo:"));
    }
}


问题是您在中间添加了中间面板。中心使用全部可用空间。将中间面板设置为BorderLayout,然后在North向其添加另一个面板,然后在那里添加有效的MyPanels

public class Main extends JFrame {

JPanel upPanel = new JPanel();
JPanel newMidPanel = new JPanel();
JPanel downPanel = new JPanel();
JButton button = new JButton("add new Panel");

Main() {
    this.setSize(800, 600);
    this.setLayout(new BorderLayout());
    JPanel midPanel = new JPanel(new BorderLayout());
    this.add(upPanel, BorderLayout.PAGE_START);
    JScrollPane jsp = new JScrollPane(midPanel);
    this.add(jsp);
    this.add(downPanel, BorderLayout.PAGE_END);
    this.newMidPanel = new JPanel(new GridLayout(0, 1, 3, 3));
    upPanel.add(button);
    midPanel.add(newMidPanel, BorderLayout.NORTH);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            newMidPanel.add(new MyPanel());
            newMidPanel.updateUI();

        }
    });
}

public static void main(String[] args) {
    new Main().setVisible(true);
}
}

class MyPanel extends JPanel {

MyPanel() {
    this.setLayout(new GridLayout(2, 5, 5, 5));
    this.setBorder(BorderFactory.createRaisedBevelBorder());

    this.add(new JLabel("Nomo:"));
}
}

我相信如果你使用
wrap\u content
@JosefE。你能告诉我更多吗?它不是一个ID应用程序。它是JavaSwing.+1,用于一般解决方案-1,用于在添加面板时使用updateUI()方法。updateUI()方法在执行外观更改时由内部Swing方法使用,在面板中添加/删除组件时不应使用。相反,您应该使用
revalidate()
repaint()
方法。@camickr我只采用从到的代码并添加问题的答案。但你是对的:)