Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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
Swing Gui-JPanel将JComponent添加到GridLayout单元_Swing_User Interface_Jpanel_Jcomponent_Grid Layout - Fatal编程技术网

Swing Gui-JPanel将JComponent添加到GridLayout单元

Swing Gui-JPanel将JComponent添加到GridLayout单元,swing,user-interface,jpanel,jcomponent,grid-layout,Swing,User Interface,Jpanel,Jcomponent,Grid Layout,我试图使用GridLayout显示一个框架,但我的一个面板没有显示 我遇到问题的JPanel(gridPanel)应该有一个50乘50的GridLayout,网格中的每个单元格都应该添加一个正方形对象。然后,应该将该面板添加到框架中,但它不显示 import java.awt.*; import javax.swing.*; public class Gui extends JFrame{ JPanel buttonPanel, populationPanel, velocityPa

我试图使用GridLayout显示一个框架,但我的一个面板没有显示

我遇到问题的JPanel(gridPanel)应该有一个50乘50的GridLayout,网格中的每个单元格都应该添加一个正方形对象。然后,应该将该面板添加到框架中,但它不显示

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

public class Gui extends JFrame{

    JPanel buttonPanel, populationPanel, velocityPanel, gridPanel;
    JButton setupButton, stepButton, goButton;
    JLabel populationLabel, velocityLabel;
    JSlider populationSlider, velocitySlider;
    Square [] [] square;

    public Gui() {

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //Set up JButtons
        buttonPanel = new JPanel();
        setupButton = new JButton("Setup");
        stepButton = new JButton("Step");
        goButton = new JButton("Go");

        buttonPanel.add(setupButton);
        buttonPanel.add(stepButton);
        buttonPanel.add(goButton);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;   //2 columns wide
        add(buttonPanel, c);


        //Set up populationPanel    
        populationPanel = new JPanel();
        populationLabel = new JLabel("Population");
        populationSlider = new JSlider(JSlider.HORIZONTAL,0, 200, 1);

        populationPanel.add(populationLabel);
        populationPanel.add(populationSlider);

        c.fill = GridBagConstraints.LAST_LINE_END;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;   //2 columns wide
        add(populationPanel, c);        


        //Set up velocityPanel
        velocityPanel = new JPanel();
        velocityLabel = new JLabel("    Velocity");
        velocitySlider = new JSlider(JSlider.HORIZONTAL,0, 200, 1);

        velocityPanel.add(velocityLabel);
        velocityPanel.add(velocitySlider);

        c.fill = GridBagConstraints.LAST_LINE_END;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;   //2 columns wide
        add(velocityPanel, c);  


        //Set up gridPanel
        JPanel gridPanel = new JPanel(new GridLayout(50, 50));
        square = new Square[50][50];

        for(int i = 0; i < 50; i++){
            for(int j = 0; j < 50; j++){
                    square[i][j] = new Square();
                    gridPanel.add(square[i][j]);
            }
        }

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;   //2 columns wide
        add(gridPanel, c);  
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        Gui frame = new Gui();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

你看不到任何东西的原因有两个方面(尽管不能完全确定第二个原因是否是故意的,猜测一下:)

  • JComponent是一个简单的容器,它没有ui委托,也没有固有的大小——返回合理的min/max/pref值取决于您的代码
  • 绘画矩形硬编码为内部的“某处”,可能在实际组件区域内,也可能在实际组件区域内
下面显示的是(仅用于查看边界的边框)


你看不到任何东西的原因有两个方面(尽管不能完全确定第二个原因是否是故意的,猜测一下:)

  • JComponent是一个简单的容器,它没有ui委托,也没有固有的大小——返回合理的min/max/pref值取决于您的代码
  • 绘画矩形硬编码为内部的“某处”,可能在实际组件区域内,也可能在实际组件区域内
下面显示的是(仅用于查看边界的边框)

getPreferredSize(){return new Dimension(10,10);…
(礼貌咳嗽)欢迎来到强制组件成为特定大小的黑暗面。
getPreferredSize(){return new Dimension(10,10);…
(礼貌咳嗽)欢迎来到强制组件成为特定大小的黑暗面。;)
import java.awt.*;
import javax.swing.*;

public class Square extends JComponent{

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(10, 10, 10, 10);
    }

}
public static class Square extends JComponent {

    public Square() {
        setBorder(BorderFactory.createLineBorder(Color.RED));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(10, 10);
    }
    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }
    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

}