简体   繁体   English

如何将JPanel添加到JFrame

[英]How to add JPanel to a JFrame

I am a beginner in java and I have this objective to be done: 我是Java的初学者,我有一个目标要完成: 在此处输入图片说明

This is my partial code: 这是我的部分代码:

public class Trial extends JFrame { 公共类试用版扩展了JFrame {

public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final int SCALE = 3;

private BufferedImage backBuffer;

public KeyboardInput input;
private Stage stage;
public static Trial Trial;
public static String message = "";
private Object Object;
private Message MessageBox;

public void init() {
    this.Trial = this;
    input = new KeyboardInput();
    this.addWindowListener(new WinListener());
    this.setSize(WIDTH, HEIGHT);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setUndecorated(false);
    this.setVisible(true);
    this.addKeyListener(input);
    client = new TrialClient(SERVER_IP, this);
    MessageBox = new Message(username);
    backBuffer = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
}

public Stage getStage() {
    return stage;
}

public class WinListener extends WindowAdapter {

    @Override
    public void windowClosing(WindowEvent e) {
        disconnect();
        System.exit(0);
    }

}


private Font font = new Font("Munro Small", Font.PLAIN, 96);
private Font font2 = new Font("Munro Small", Font.PLAIN, 50);
private Font fontError = new Font("Munro Small", Font.PLAIN, 25);
private int op = 0;


public void updateMenu() {
    if (input.up.isPressed()) {
        input.up.toggle(false);
    } else if (input.down.isPressed()) {
        input.down.toggle(false);
    } else if (input.enter.isPressed() && op == 0) {
    } else if (input.enter.isPressed() && op == 1) {
        System.exit(0);
    }
}


public void drawMenu() {
    Graphics g = getGraphics();
    Graphics bbg = backBuffer.getGraphics();
    bbg.setFont(font);
    bbg.setColor(Color.white);

    bbg.drawString("Sample", 189, 180);
    bbg.setFont(font2);
    if (op == 0) {
        bbg.setColor(Color.red);
        bbg.drawString("Start", 327, 378);
    }

    g.drawImage(backBuffer, 0, 0, this);
}

public void draw() {
    Graphics g = getGraphics();
    Graphics bbg = backBuffer.getGraphics();
    bbg.setColor(Color.black);
    bbg.fillRect(0, 0, WIDTH, HEIGHT);
    stage.drawStage(bbg, this);
    for (Object t : stage.getPlayers()) {
        t.draw(bbg, SCALE, this);
    }
    g.drawImage(backBuffer, 0, 0, this);
}


public void drawLogin() {
    Graphics g = getGraphics();
    Graphics bbg = backBuffer.getGraphics();
    bbg.setColor(Color.black);
    bbg.fillRect(0, 0, 800, 600);
    bbg.setColor(Color.red);
    bbg.setFont(fontError);
    bbg.drawString(message, 100, 100);
    bbg.setFont(font2);
    bbg.setColor(Color.white);
    bbg.drawString("Username", 284, 254);
    bbg.setColor(Color.red);
    bbg.drawString(username, 284, 304);
    g.drawImage(backBuffer, 0, 0, this);
}



public class StringWait extends Thread {

    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
    }
}

public void start() {
    init();
    while (true) {
       ...
    }
}

public static void main(String[] args) {
    Trial g = new Trial();
    g.start();
}

} }

how will I declare the Jpanel in JFrame? 如何在JFrame中声明Jpanel? Or there is a way to create a Jpanel in different window after the JFrame was created? 还是有一种方法可以在创建JFrame之后在其他窗口中创建Jpanel? I am doing this in netbeans. 我在netbeans中这样做。

I think the problem is that the line 我认为问题是线

backBuffer = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);

please modify it, so that it will not occupy the whole area. 请对其进行修改,以使其不会占据整个区域。

Just declare a variable JPanel and add it to the Frame. 只需声明一个变量JPanel并将其添加到Frame中即可。

JPanel panel = new JPanel();
this.setContentPane(panel);

Declare variable outside init methode Create jpanel, add component to the panel create jscrollpane with panel as view set jscrollpane as contentpane of your frame see exemple below 在init方法外声明变量创建jpanel,将组件添加到面板中创建jscrollpane,将面板作为视图集jscrollpane作为框架的contentpane参见以下示例

public class Game extends JFrame {

public Game() throws HeadlessException {
    setSize(100, 100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
}

private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;

void init() {

    jButton1 = new JButton("jButton1");

    jTextArea1 = new JTextArea("jTextArea1");
    jTextField1 = new JTextField("jTextField1");
    JPanel panel2 = new JPanel();
    panel2.setLayout(new FlowLayout());
    panel2.add(jTextField1);
    panel2.add(jButton1);
    jPanel1 = new JPanel();
    jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));
    jPanel1.add(jTextArea1);
    jPanel1.add(panel2);
    jScrollPane1 = new JScrollPane(jPanel1);
    setContentPane(jScrollPane1);
    System.out.println("init");
    revalidate();
}

public static void main(String[] args) throws InterruptedException {
    Game g = new Game();
    //wait just to find the effect of revalidate
    Thread.sleep(1000);

    g.init();
}

} }

You need to add 您需要添加

JPanel panel = new JPanel();

and other declarations inside the init(), then make sure that BufferedImage function don't overlaps on it 和init()内的其他声明,然后确保BufferedImage函数不重叠在上面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM