drawing simple rectangles on a Jframe in java

前端 未结 3 526
自闭症患者
自闭症患者 2020-12-11 05:06

I am extending JFrame like this:

public GameFrame() {
    this.setBounds(30, 30, 500, 500);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    initializeS         


        
3条回答
  •  一生所求
    2020-12-11 05:30

    Even I was having an issue while displaying multiple rectangles on a jframe with absolute layout i.e layout set to null.

    JFrame frame = new JFrame("hello");
    frame.setLayout(null);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    frame.getContentPane().setBackground(Color.red);
    frame.getContentPane().add(new Square(10,10,100,100));//My rectangle class is similar to the Square mentioned in the question and it extends JComponent.
    

    Still I was getting issues as the rectangle was not displayed.Unless you explicitly set the bounds on the Square(JComponent) ,it will not work.Even though the Square has the location passed in the constructor ,setbounds only fixed the issue.So the below fixed the issue and this issue is for an absolute layout jframe.

    Square square = new Square(10,10,100,100);
    square.setBounds(10,10,100,100);
    frame.getContentPane().add(square);
    

提交回复
热议问题