将jpanel从方法添加到jframe

时间:2023-01-28 16:21:10

In my project I have some classes. One class to create the gui, except of the JFrame. I will create the JFrame in my Main class such as:

在我的项目中,我有一些课程。创建gui的一个类,除了JFrame。我将在我的Main类中创建JFrame,例如:

import javax.swing.*;
import java.awt.*;

public class KodeHusker {

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run(){
            JFrame f = new JFrame();
            f.setLayout(new FlowLayout());
            f.add(new JLabel("test"));
            f.add(new GUI().viewProgram());//it works fine, when i remove this
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    });
}
}

the gui class is where I create all the gui, and the viewProgram method is declared as:

gui类是我创建所有gui的地方,viewProgram方法声明为:

public JPanel viewProgram(){}

which returns a JPanel.

返回一个JPanel。

As the comment in the code tells, when I remove that line it all works fine, but when I have it, the JFrame never shows, although there aren't any exceptions. The shortcut to close the program doesn't work neither.

正如代码中的注释所说,当我删除该行时,一切正常,但是当我拥有它时,JFrame永远不会显示,尽管没有任何异常。关闭程序的快捷方式也不起作用。

Anybody who has an idea of what i'm doing wrong? Thanks in advise.

谁知道我做错了什么?谢谢你的建议。

1 个解决方案

#1


0  

here is what a jPanel should look like, not sure (can see your code?)

这是jPanel应该是什么样子,不确定(可以看到你的代码吗?)

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class JPanelExt extends JPanel  implements Runnable {
private int xCoor = 50;
private boolean moving = false;

 public JPanelExt() {       
      //Thread 
          thread = new Thread (this);       //
          thread.start();   }   

  public void paintComponent (Graphics g)
     {  
            super.paintComponent(g);
    g.drawString("DVC 10.0",xCoor,30);  
            g.drawRect(50, 50, 200,100);    
            g.drawOval(50,50,200,100);  
}   
    @Override
public void run() {
while (true)
        {   
    if (moving==true)
                   {    
          this.xCoor = this.xCoor+10;
        if (xCoor > this.getWidth())
                             {  
            xCoor = 0;      
                       }
            this.repaint(); 
               }        
        try {
            Thread.sleep(50);
        }
                catch (InterruptedException e) {    
        e.printStackTrace();            
                    }   
   }    
   public void setMoving(boolean moving) 
       {    
            this.moving = moving;   
        }
}

#1


0  

here is what a jPanel should look like, not sure (can see your code?)

这是jPanel应该是什么样子,不确定(可以看到你的代码吗?)

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class JPanelExt extends JPanel  implements Runnable {
private int xCoor = 50;
private boolean moving = false;

 public JPanelExt() {       
      //Thread 
          thread = new Thread (this);       //
          thread.start();   }   

  public void paintComponent (Graphics g)
     {  
            super.paintComponent(g);
    g.drawString("DVC 10.0",xCoor,30);  
            g.drawRect(50, 50, 200,100);    
            g.drawOval(50,50,200,100);  
}   
    @Override
public void run() {
while (true)
        {   
    if (moving==true)
                   {    
          this.xCoor = this.xCoor+10;
        if (xCoor > this.getWidth())
                             {  
            xCoor = 0;      
                       }
            this.repaint(); 
               }        
        try {
            Thread.sleep(50);
        }
                catch (InterruptedException e) {    
        e.printStackTrace();            
                    }   
   }    
   public void setMoving(boolean moving) 
       {    
            this.moving = moving;   
        }
}