【问题标题】:Transferring JFrame to JPanel将 JFrame 传输到 JPanel
【发布时间】:2014-01-29 04:49:47
【问题描述】:

我创建了一个简单的“电梯”程序(它仍处于初始阶段),当我点击 UP 时它会上升 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!");

  }

}

}

主类只是从用户那里获取输入,例如楼层数、一天中的时间等。

【问题讨论】:

    标签: java swing jpanel paintcomponent repaint


    【解决方案1】:

    这会有点乱。

    首先创建一个从JPanel 扩展的自定义组件(我将其称为ElevatorPane)。

    获取当前paint 方法的内容并将它们放在此组件paintComponent 方法中。这将涉及移动paintComponent 方法需要的实例变量,包括totnumoffloorsbordercountupdownmovefloorupswitchmarkerrectX

    这里有点乱……

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

    ElevatorPane 中创建一个构造函数,它采用楼层数。

    覆盖ElevatorPanegetPrefferedSize 方法并返回组件需要满足您的需求的大小...

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

    清理、构建、运行...

    【讨论】:

    • 首先,非常非常感谢您回答这个问题。通过创建一个新组件,您的意思是创建一个新类,对吧?抱歉,我对 Java 不太熟悉。
    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多