简体   繁体   English

使用JPanel.paintComponent()绘制两个正方形-错误的尺寸

[英]Painting two squares with JPanel.paintComponent() - wrong size

I am trying to paint two squares of defined size, one located next to another in a row, using paintComponent() method of JPanel. 我正在尝试使用JPanel的paintComponent()方法绘制两个定义大小的正方形,一个正方形排成一行。 Here what I tried so far: 这是我到目前为止尝试过的:

1. BorderLayout 1. BorderLayout

I tried to draw each square in a separate subclass of JPanel, and then add these JPanels to a JFrame. 我尝试在JPanel的单独子类中绘制每个正方形,然后将这些JPanels添加到JFrame。

Result : squares are squashed to the opposite sides of JPanel: height is as expected but width is minimal. 结果 :将正方形压缩到JPanel的相对侧:高度符合预期,但宽度最小。

JFrame frame = new JFrame();

GreenPanel greenPanel = new GreenPanel();
frame.getContentPane().add(BorderLayout.WEST, greenPanel);
BluePanel bluePanel = new BluePanel();
frame.getContentPane().add(BorderLayout.EAST, bluePanel);

frame.setSize(500, 350);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

class GreenPanel extends JPanel {         
    public void paintComponent(Graphics g) {
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 100, 100);
    }
}

class BluePanel extends JPanel {         
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 100, 100);
    }
}

2. FlowLayout 2. FlowLayout

I tried to add my "children" JPanels with painted squares to a "parent" JPane and then call JFrame.setContentPane(JPanel). 我试图将带有绘制正方形的“子级” JPanel添加到“父级” JPane中,然后调用JFrame.setContentPane(JPanel)。

Result: squares are painted in top-center region as tiny squares. 结果:正方形在顶部中心区域被绘制为微小的正方形。

JFrame frame = new JFrame();
JPanel outerPanel = new JPanel();
frame.setContentPane(outerPanel);

GreenPanel greenPanel = new GreenPanel();
outerPanel.add(greenPanel);
BluePanel bluePanel = new BluePanel();
outerPanel.add(bluePanel);

frame.setSize(500, 350);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

class GreenPanel extends JPanel {         
    public void paintComponent(Graphics g) {
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 100, 100);
    }
}

class BluePanel extends JPanel {         
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 100, 100);
    }
}

What am I doing wrong? 我究竟做错了什么? Is there any way I can force layout managers to respect the size of the squares painted with paintComponent() ? 有什么办法可以迫使布局管理器尊重用paintComponent()绘制的正方形的大小?

Make the following changes. 进行以下更改。 See comments for explanations. 请参阅注释以获取解释。

  JFrame frame = new JFrame();
  //add layout manager. You can achieve the desired layout 
  //with GridLayout, Box layout and others 
  frame.getContentPane().setLayout(new GridLayout(1,2));
  GreenPanel greenPanel = new GreenPanel();
  //set preferred size to the panel 
  greenPanel.setPreferredSize(new Dimension(100,100));
  frame.add(greenPanel);
  BluePanel bluePanel = new BluePanel();
  //set preferred size to the panel 
  bluePanel.setPreferredSize(new Dimension(100,100));
  frame.getContentPane().add(bluePanel);

  //let the frame adapt to the panels size 
  //frame.setSize(500, 350); 
  frame.validate();
  frame.pack();
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Don't hesitate to ask for clarifications as needed. 请随时根据需要进行澄清。


(BTW overriding paintComponent(Graphics g) is not really needed. You could simply set preferred size and color to each JPanel ) (实际上并不需要BTW覆盖paintComponent(Graphics g) 。您只需为每个JPanel设置首选大小和颜色即可)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM