Java JPanel窗口在调整主框架大小时不缩放

Java JPanel窗口在调整主框架大小时不缩放,java,swing,jpanel,Java,Swing,Jpanel,我正在编写代码,以绘制用户输入的多条直线,第一个端点以y坐标为中心,而第二个端点通过高度/(用户输入的直线数)彼此分开。我验证了我的代码,除了JPanel没有按照主框架的大小进行伸缩之外,其他一切似乎都正常工作。谁能给我一个建议吗 import java.awt.Graphics; import javax.swing.JPanel; import java.util.Scanner; public class DrawPanel extends JPanel { public

我正在编写代码,以绘制用户输入的多条直线,第一个端点以y坐标为中心,而第二个端点通过高度/(用户输入的直线数)彼此分开。我验证了我的代码,除了JPanel没有按照主框架的大小进行伸缩之外,其他一切似乎都正常工作。谁能给我一个建议吗

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
    }
}
导入java.awt.Graphics;
导入javax.swing.JPanel;
导入java.util.Scanner;
公共类DrawPanel扩展了JPanel
{
公共组件(图形g)
{
超级组件(g);
int width=getWidth();
int height=getHeight();
Scanner input=new Scanner(System.in);//从Scanner类创建对象输入
System.out.println(“输入要绘制的行数+1:”;//请求用户输入
int stepSize=input.nextInt();//初始化stepSize以接受用户输入
int endX=width;//x的第二个pt的起始位置
int endY=高度;//y的第二个pt的起始位置
对于(int i=0;i<步长+1;i++)
{
int verticalStep=高度/步长;//分开y坐标的第二个端点
int中点=高度/2;
g、 抽绳(0,中点,端点,端点);
endY=endY-垂直步长;
}   
}
}
导入javax.swing.JFrame;
公共类测试
{
公共静态void main(字符串[]args)
{
DrawPanel panel=新的DrawPanel();
JFrame窗口=新JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
窗口。添加(面板);
setSize(500500);//设置应用程序的大小
window.setVisible(true);//显示应用程序
}
}

JPanel的默认布局是
FlowLayout
,它使用封闭组件的首选尺寸。例如,使用
GridLayout
。它将随着帧大小的调整而增长和收缩。不要使用
setSize()
,而是使用如图所示的面板首字母。

感谢各位的反馈。我已经知道如何做了,因为我不应该使用Scanner类来请求用户输入。以下是我为此作业编辑的代码:

 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
   }
公共类DrawPanel扩展了JPanel
{
私人选择;
公共绘图面板(int选项)
{
用户选择=选择;
}
公共组件(图形g)
{
super.paintComponent(g);//调用paintComponent以确保窗口正确显示
int width=getWidth();//总宽度
int height=getHeight();//总高度
int endX=width;//x的第二个pt的起始位置
int endY=高度;//y的第二个pt的起始位置
//从0增加到用户输入的步长以绘制线条

对于(int i=1;i),您不应该要求用户在paintComponent()方法中输入。您无法控制何时调用此方法-可以在需要绘制面板的任何时候调用它,因此在调整组件大小时,可能会在几毫秒内调用多次。