将JPanel添加到JFrame中

时间:2013-08-07 02:07:58

标签: swing user-interface jframe jpanel

我知道这听起来像一个简单的问题,但我正在用按钮等进行简单的文本冒险,而我无法弄清楚如何将我的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 个答案:

答案 0 :(得分:0)

尝试

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

或只是简单地

object1.add(panel);

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

以下是一些有用的代码:

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);
    }
}

答案 1 :(得分:0)

也许这样......?

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();

    }
}

答案 2 :(得分:0)

这就是我通常的做法:

public class SonomaRoller extends JFrame {

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

答案 3 :(得分:0)

这就是问题:

frame panel=new frame();

将此行代替上述代码:

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