JPanel Window not scaling when resize the main fra

2019-08-08 03:06发布

问题:

I was working on my code to draw a number of lines that the user inputted and the first end-point is centered at the y-coordinate while the second end-points are apart from each other by height/(number of lines the user entered). I verified my code and everything seems to be working fine except the fact that the JPanel does not scale as it should, corresponding to the size of the main frame. Can anyone please give me an advice?

import java.awt.Graphics; 
import javax.swing.JPanel; 
import java.util.Scanner; 

public class DrawPanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int width = getWidth(); 
        int height = getHeight(); 

        Scanner input = new Scanner(System.in); //create object input from Scanner class
        System.out.println("Enter number of lines + 1 to draw: "); //ask for user input
        int stepSize = input.nextInt(); //initialize stepSize to take user input
        int endX = width; //starting position of second pt for x
        int endY = height; //starting position of second pt for y

        for(int i = 0; i < stepSize + 1; i++)
        {
            int verticalStep = height / stepSize; //separate y-coordinate second endpts apart
            int midPoint = height / 2; 
            g.drawLine(0, midPoint, endX, endY); 
            endY = endY - verticalStep; 
        }   
    }
}

import javax.swing.JFrame; 

public class DrawPanelTest 
{

    public static void main(String[] args) 
    {
        DrawPanel panel = new DrawPanel(); 
        JFrame window = new JFrame(); 

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        window.add(panel); 
        window.setSize(500, 500); //set size of the application
        window.setVisible(true); //display the application
    }
}

回答1:

The default layout of JPanel is FlowLayout, which uses the preferred size of the enclosed components. Instead, use GridLayout, for example. It will grow and shrink as the frame is resized. Instead of setSize(), give your panel an initial preferred size as shown here.



回答2:

Thank you for your feedbacks guys. I have figured out how to do it as I should not use Scanner class to ask for the user input. Below is my edited code for this assignment:

 public class DrawPanel extends JPanel
{
   private int userChoice; 
   public DrawPanel(int choice)
   {
      userChoice = choice; 
   }

   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);//call paintComponent to ensure the window displays correctly
      int width = getWidth(); //total width
      int height = getHeight();//total height

      int endX = width; //starting position of second pt for x
      int endY = height; //starting position of second pt for y

      //increment from 0 to the user inputted stepSize to draw lines    
      for(int i = 1; i <= userChoice + 1; i++)
      {
         int verticalStep = height / userChoice; //separate y-coordinate second endpts apart
         int midPoint = height / 2; //center the starting position in the y-axis of first endpoint
         g.drawLine(0, midPoint, endX, endY); //draw lines
         endY = endY - verticalStep; //move the second endpoints either up or down
      } 
   }
}


public class DrawPanelTest 
{
   public static void main(String[] args) 
   {
      int choice = Integer.parseInt(args [0]); 
      DrawPanel panel = new DrawPanel(choice);//create a panel that contains the lines 
      JFrame window = new JFrame(); //create a frame to hold the panel

      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set frame to exit when closed
      window.add(panel); //add panel to the frame
      window.setSize(500, 500);//set size for the frame
      window.setVisible(true); //display the frame
   }