Java 将JFrame传输到JPanel

Java 将JFrame传输到JPanel,java,swing,jpanel,paintcomponent,repaint,Java,Swing,Jpanel,Paintcomponent,Repaint,我创建了一个简单的“电梯”程序(它仍处于开始阶段),当我点击向上时,它会上升1层,反之亦然 当我将所有组件绘制到JFrame中时,我把事情搞砸了,正如预期的那样,每次单击按钮(重新绘制)时,它都会闪烁。我知道在JPanel中绘制解决方案,并将所述面板放入JFrame,但我在将JFrame组件转换为JPanel时遇到问题。我尝试过扩展JPanel,创建一个JFrame对象,然后重写paintComponent()方法并在那里画图,但编译时它根本不会画图。它只创建框架 有谁能帮我或给我一些提示,告诉

我创建了一个简单的“电梯”程序(它仍处于开始阶段),当我点击向上时,它会上升1层,反之亦然

当我将所有组件绘制到JFrame中时,我把事情搞砸了,正如预期的那样,每次单击按钮(重新绘制)时,它都会闪烁。我知道在JPanel中绘制解决方案,并将所述面板放入JFrame,但我在将JFrame组件转换为JPanel时遇到问题。我尝试过扩展JPanel,创建一个JFrame对象,然后重写paintComponent()方法并在那里画图,但编译时它根本不会画图。它只创建框架

有谁能帮我或给我一些提示,告诉我如何将我的编程从基于JFrame的“转移”到基于JPanel的?提前谢谢你

我的代码如下:

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
import java.math.*;



public class MyCanvas extends JFrame {

private int up = 0;
private int down = 0;
private int movefloorup = 0;
private int buildingType;//type of building (1 = Residential, 2 = Commercial)
private int totnumoffloors; //for the total number of floors
private int numofelevators; //for the number of elevators to be generated
private int floorlimit = 0; //to determine up until where the elevator will be moving
private int currenttime; //determine the time of the day the elevator is operating (1 = Morning, 2 = Lunch, 3 = Afternooon)

//For elevator resetting to bottom
private int rectX = 190;
private int switchmarker = 0;

//Lines and stuff
private int horizborder = 0;
private int bordercount = 0;

private class UpAction implements ActionListener //move the elevator up
{
    public void actionPerformed(ActionEvent e)
    {
        if(movefloorup<780){
            repaint();
            up++;
            movefloorup = movefloorup + 130;
            //repaint();

        }
        else
        {
              switchmarker = 1;
              movefloorup = 0;
              repaint();

        }
    }
}

private class DownAction implements ActionListener //move the elevator down
{
    public void actionPerformed(ActionEvent e)
    {

        if(movefloorup>0){
        repaint();
        down++;
        movefloorup = movefloorup - 130;
        //repaint();
        }

        else
        {
            switchmarker = 0;
            movefloorup = 780;
            repaint();
        }
    }
}


public MyCanvas(int buildingType, int totnumoffloors, int numofelevators, int currenttime){ 


    this.buildingType = buildingType;
    this.totnumoffloors = totnumoffloors;
    this.numofelevators = numofelevators;
    this.currenttime = currenttime;
    String title;

    if(this.buildingType == 1)
    {
        title = "Residential Building";
    }
    else
    {
        title = "Commercial Building";
    }

    setLayout(null);

    horizborder = 500*((int)Math.ceil((double)totnumoffloors/7)); //calculating how wide the window should be
    bordercount = ((int)Math.ceil((double)totnumoffloors/7)); //counts how many borders there will be

    //NOTES
    //A floor is 130 units in the Y-Direction


    //Drawing the bulding layout
    if(totnumoffloors>7)
    {
        setSize(horizborder, 1000);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);
        setLayout(new BorderLayout());
        getContentPane().setBackground(Color.WHITE);
    }

    else{
        setSize(500, 1000); //suitable for 7 floors
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);
        setLayout(new BorderLayout());
        getContentPane().setBackground(Color.WHITE);
    }

    JButton upButton = new JButton("UP");
    upButton.addActionListener(new UpAction());
    add(upButton, BorderLayout.NORTH);

    JButton downButton = new JButton("DOWN");
    //downButton.setBounds(0, 0, 220, 30);
    //downButton.setLocation(100, 100);
    downButton.addActionListener(new DownAction());
    add(downButton, BorderLayout.SOUTH);


}


public void paint(Graphics graphics){ //this is where you draw shit

    super.paint(graphics);

    //Floors
    graphics.setColor(Color.RED);

    int numoffloorsY = 830;
    int numoffloorsX = 830;
    int floorbeginning = 0;
    int floorcounter = 1;
    int floorflag = 0;
    int rightedge = 500;

    if(this.totnumoffloors>7) //drawing the floors
    {

        //Default number of floors -> 7
        for(int i = 0;i<totnumoffloors;i++)
        {
            graphics.setColor(Color.RED);
            graphics.drawLine(floorbeginning,numoffloorsX,rightedge,numoffloorsY); //FLOORS
            graphics.setColor(Color.DARK_GRAY);
            graphics.setFont(new Font("TimesRoman", Font.PLAIN, 15)); 
            graphics.drawString("  "+floorcounter, floorbeginning+10, numoffloorsY+20); //SAVE THIS FOR DRAWING FLOORS
            numoffloorsY = numoffloorsY - 130;
            numoffloorsX = numoffloorsX - 130;
            floorcounter++;
            floorflag++;

            if(floorflag==7)
            {
                floorbeginning = floorbeginning + 500;
                rightedge = rightedge+500;
                numoffloorsY = 830;
                numoffloorsX = 830;
                floorflag = 0;
            }


        }



        //Every other floor past 7 will be added here.
        /*for(int i = 0;i<totnumoffloors-7;i++)
        {
            //System.out.println("LOLOOLO");
            graphics.setColor(Color.RED);
            graphics.drawLine(floorbeginning,numoffloorsX,horizborder,numoffloorsY);
            graphics.setColor(Color.DARK_GRAY);
            graphics.setFont(new Font("TimesRoman", Font.PLAIN, 15)); 
            graphics.drawString("  "+floorcounter, floorbeginning, numoffloorsY+20);
            //graphics.setColor(Color.DARK_GRAY);
            //graphics.drawLine(500,0,500,1000);

            floorcounter++;

            numoffloorsY = numoffloorsY - 130;
            numoffloorsX = numoffloorsX - 130;
        }*/

        //DIVIDING LINE -> to determine the first 7 floors from the ones higher up.
        for(int i=0;i<bordercount;i++)
        {
            graphics.setColor(Color.DARK_GRAY);
            graphics.drawLine(500*i,0,500*i,1000);

        }

    }

    else{

        for(int i = 0;i<this.totnumoffloors;i++)
        {
            graphics.setColor(Color.RED);
            graphics.drawLine(0,numoffloorsX,500,numoffloorsY);
            graphics.setColor(Color.DARK_GRAY);
            graphics.setFont(new Font("TimesRoman", Font.PLAIN, 15)); 
            graphics.drawString("  "+floorcounter, floorbeginning+10, numoffloorsY+20); //SAVE THIS FOR DRAWING FLOOR
            numoffloorsY = numoffloorsY - 130;
            numoffloorsX = numoffloorsX - 130;
            floorcounter++;
        }
    }

  //Drawing the elevators
  if(up>0 && movefloorup<1000){

    graphics.setColor(Color.GRAY);
    if(switchmarker==1)
    {
        System.out.println("ELSA");
        rectX = 690;
        //rectX = rectX + 190;

    }
    else
    {
        rectX = 190;
    }

    System.out.println(rectX);
    graphics.fillRect(rectX, 850-movefloorup, 100, 100); //this needs to match the stats of the rectangle to fill it properly
    graphics.drawRect(rectX, 850-movefloorup, 100, 100);

    //Line for the door
    graphics.setColor(Color.BLACK);
    graphics.drawLine(rectX+50, 850-movefloorup, rectX+50, 950-movefloorup);  //match the y-coordinate for the rectangle, add 100 for the y-coordinate of the other end

    System.out.println(movefloorup);
    System.out.println(switchmarker);

    //drawLine(x1, y1, x2, y2); --From (x1,y1) to (x2,y2)
  }

  else if(down>0 && movefloorup>0)
  {
    graphics.setColor(Color.GRAY);

    if(switchmarker==1) //This determines when the elevator should move to the next column of higher floors.
    {
        System.out.println("ELSA");
        rectX = 500;
    }
    System.out.println(rectX);
    graphics.fillRect(rectX, 850-movefloorup, 100, 100); //this needs to match the stats of the rectangle to fill it properly
    //graphics.drawRect(190, 850 + movefloorup, 100, 100); //FIRST FLOOR
    graphics.drawRect(rectX, 850-movefloorup, 100, 100);  //SECOND FLOOR (135 units difference in Y-axis between floors)
    //x-coordinate, y-coordinate, width, height


     //Line for the door
    graphics.setColor(Color.BLACK);
    graphics.drawLine(rectX+50, 850-movefloorup, rectX+50, 950-movefloorup); //match the y-coordinate for the rectangle, add 100 for the y-coordinate of the other end

    System.out.println(movefloorup);
    System.out.println(switchmarker);
  }

  else
  {
    graphics.setColor(Color.GRAY);
    graphics.fillRect(190, 850, 100, 100); //this needs to match the stats of the rectangle to fill it properly
    graphics.drawRect(190, 850, 100, 100); //FIRST FLOOR
    graphics.drawRect(190, 850, 100, 100);  //SECOND FLOOR (135 units difference in Y-axis between floors)
      //x-coordinate, y-coordinate, width, height
     //Line for the door
    graphics.setColor(Color.BLACK);
    graphics.drawLine(240, 850, 240, 950); //match the y-coordinate for the rectangle, add 100 for the y-coordinate of the other end
      //System.out.println("In else!");

  }

}

}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入javax.swing.Timer;
导入java.math.*;
公共类MyCanvas扩展了JFrame{
私有整数up=0;
私有整数向下=0;
私有int=0;
私有int buildingType;//建筑类型(1=住宅,2=商业)
private int totnumofloors;//用于总楼层数
private int numofladders;//用于生成的电梯数量
private int floorlimit=0;//确定电梯移动的位置
private int currenttime;//确定一天中电梯运行的时间(1=上午,2=午餐,3=下午)
//用于电梯复位至底部
私有整数rectX=190;
专用int开关标记=0;
//台词之类的
私有内水平边界=0;
私有整数边界计数=0;
私有类UpAction实现ActionListener//向上移动电梯
{
已执行的公共无效操作(操作事件e)
{
if(movefloorup0){
重新油漆();
down++;
MoveFloorrup=MoveFloorrup-130;
//重新油漆();
}
其他的
{
switchmarker=0;
moveflorup=780;
重新油漆();
}
}
}
公共MyCanvas(int buildingType、int Totnumofloors、int Numof电梯、int currenttime){
this.buildingType=buildingType;
this.totnumofloors=totnumofloors;
this.numoflifests=numoflifests;
this.currenttime=currenttime;
字符串标题;
if(this.buildingType==1)
{
title=“住宅建筑”;
}
其他的
{
title=“商业建筑”;
}
setLayout(空);
horizborder=500*((int)Math.ceil((double)totnumofloors/7));//计算窗口的宽度
bordercount=((int)Math.ceil((double)totnumofloors/7));//计算将有多少个边框
//注释
//楼层在Y方向上为130个单元
//绘制建筑布局图
如果(总楼层数>7)
{
设置大小(水平边界,1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
片名(片名);
setLayout(新的BorderLayout());
getContentPane().setBackground(颜色为.WHITE);
}
否则{
设置大小(5001000);//适用于7层
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
片名(片名);
setLayout(新的BorderLayout());
getContentPane().setBackground(颜色为.WHITE);
}
JButton upButton=新JButton(“UP”);
addActionListener(新的UpAction());
添加(向上按钮,BorderLayout.NORTH);
JButton downButton=新JButton(“DOWN”);
//向下按钮。立根(0,0,220,30);
//向下按钮。设置位置(100100);
addActionListener(新的DownAction());
添加(向下按钮,BorderLayout.SOUTH);
}
公共虚空绘制(图形){//这是绘制狗屎的地方
超级油漆(图形);
//地板
图形.设置颜色(颜色.红色);
国际货币基金组织=830;
int numofloorsx=830;
int floorbeginning=0;
int floorcounter=1;
int floorflag=0;
int righedge=500;
if(this.totnumofloors>7)//绘制楼层
{
//默认楼层数->7

对于(inti=0;i这将有点混乱

首先创建一个从
JPanel
扩展而来的自定义组件(我称之为
ElevatorPane

获取当前
paint
方法的内容,并将其放置在此组件
paintComponent
方法中。这将涉及移动
paintComponent
方法需要的实例变量,包括,
totnumofloors
bordercount
向上
向下
movefloorup
开关标记
rectX

这就是它变得有点混乱的地方

您需要获取
ActionListeners的内容
并将其转换为
提升窗格
中的方法,这样您就可以在不公开细节的情况下公开功能

电梯窗格
中创建一个采用楼层数的构造函数

覆盖
ElevatorPane
getPreferedSize
方法,并返回组件满足您需要的大小

MyCanvas
中创建
ElevatorPane
的实例字段,实例化它并将其添加到框架中


清理、构建、运行……

首先,非常感谢您回答这个问题。创建一个新组件,您的意思是创建一个新类,对吗?对不起,我对Java不太精通。