将JPanel添加到JFrame中

时间:2023-01-28 16:02:17

I know this sounds like an easy question, but I am making a simple text adventure with buttons and such, and I cant figure out how to add my Jpanel into my JFrame. My JPanel has a bunch of buttons and graphics and stuff if that makes a difference. I have provided the code below. frame panel=new frame(); is the other class which extends JPanel. I know its confusing that its called "frame", its because I used to have it extend JFrame. Anyways my code doesnt produce the buttons, graphics etc, from the other class like it should. Thanks,

我知道这听起来像一个简单的问题,但我正在用按钮等进行简单的文本冒险,我无法弄清楚如何将我的Jpanel添加到我的JFrame中。我的JPanel有一堆按钮和图形和东西,如果这有所作为。我提供了以下代码。 frame panel = new frame();是扩展JPanel的另一个类。我知道它被称为“框架”令人困惑,因为我曾经让它扩展了JFrame。无论如何我的代码不会像其应该那样从其他类生成按钮,图形等。谢谢,

package sonomaroller;

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

public class SonomaRoller extends JFrame {

    public static Dimension size = new Dimension(550,550); //Dimension of Frame
    public static String title = "Sonoma Roller v0.00" ;


    public SonomaRoller(){

      setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null); // null centers window on screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        System.out.println("hello?");  
        //setLayout(null); 
        setVisible(true);

    }


   public static void main(String[] args) {

       SonomaRoller object1=new SonomaRoller();   
       frame panel=new frame();


    }
}

4 个解决方案

#1


0  

Try

SonomaRoller object1=new SonomaRoller();   
frame panel=new frame();
object1.getContentPane().add(panel);

or just simply

或者只是简单地

object1.add(panel);

From: JFrame#getContentPane() and Container#add(Component)

来自:JFrame#getContentPane()和Container#add(Component)

Here's some code that works if it helps:

以下是一些有用的代码:

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

public class MyFrame extends JFrame {

    public MyFrame() {
        add(new MyPanel());
        setSize(640, 480);
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }
}

class MyPanel extends JPanel {

    @Override protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillOval(20, 20, 80, 80);
    }
}

#2


0  

maybe like this...?

也许这样......?

package sonomaroller;

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

public class SonomaRoller extends JFrame {

    public static Dimension size = new Dimension(550,550); //Dimension of Frame
    public static String title = "Sonoma Roller v0.00" ;


    public SonomaRoller(){

      setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null); // null centers window on screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        System.out.println("hello?");  
        //setLayout(null); 
        setVisible(true);

        frame panel=new frame();
        setContentPane(panel);
    }


   public static void main(String[] args) {

       SonomaRoller object1=new SonomaRoller();

    }
}

#3


0  

This is how I normally do it:

这就是我通常这样做的方式:

public class SonomaRoller extends JFrame {

    public SonomaRoller(){
        setLayout(new BorderLayout(0, 0));
        JPanel newPanel = new JPanel();
        getContentPane().add(newPanel);
    }
}

#4


0  

this is the problem :

这就是问题 :

frame panel=new frame();

put this line instead of above code :

把这一行代替上面的代码:

Frame frame = new Frame();
Panel panel = new Panel();
frame.add(panel);

#1


0  

Try

SonomaRoller object1=new SonomaRoller();   
frame panel=new frame();
object1.getContentPane().add(panel);

or just simply

或者只是简单地

object1.add(panel);

From: JFrame#getContentPane() and Container#add(Component)

来自:JFrame#getContentPane()和Container#add(Component)

Here's some code that works if it helps:

以下是一些有用的代码:

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

public class MyFrame extends JFrame {

    public MyFrame() {
        add(new MyPanel());
        setSize(640, 480);
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }
}

class MyPanel extends JPanel {

    @Override protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillOval(20, 20, 80, 80);
    }
}

#2


0  

maybe like this...?

也许这样......?

package sonomaroller;

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

public class SonomaRoller extends JFrame {

    public static Dimension size = new Dimension(550,550); //Dimension of Frame
    public static String title = "Sonoma Roller v0.00" ;


    public SonomaRoller(){

      setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null); // null centers window on screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        System.out.println("hello?");  
        //setLayout(null); 
        setVisible(true);

        frame panel=new frame();
        setContentPane(panel);
    }


   public static void main(String[] args) {

       SonomaRoller object1=new SonomaRoller();

    }
}

#3


0  

This is how I normally do it:

这就是我通常这样做的方式:

public class SonomaRoller extends JFrame {

    public SonomaRoller(){
        setLayout(new BorderLayout(0, 0));
        JPanel newPanel = new JPanel();
        getContentPane().add(newPanel);
    }
}

#4


0  

this is the problem :

这就是问题 :

frame panel=new frame();

put this line instead of above code :

把这一行代替上面的代码:

Frame frame = new Frame();
Panel panel = new Panel();
frame.add(panel);

相关文章