如何在作为BufferedImage()的JFrame中添加JPanel

时间:2014-12-08 02:13:41

标签: java swing jframe jpanel

到目前为止我有这个: enter image description here

逻辑是:

a。)我将按下按钮'S'然后游戏将开始

b。)JTextArea将显示用户的对话(注意:我没有为了调试目的而禁用它)

c。)JTextField将是用户输入文本的字段。

我有这些工作代码:

package game;

//import 


public class Game extends JFrame {


public static final String SERVER_IP = "localhost";


public static final int WIDTH = 1200;
public static final int HEIGHT = 800;
public static final int SCALE = 1;


private final int FPS = 60;
private final long targetTime = 1000 / FPS;

private BufferedImage backBuffer;

public KeyboardInput input;
private Stage stage;

public String username = "";

public GameClient client;
public static Game game;

public static String message = "";

private Tank tank;

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;

public Game() throws HeadlessException {
    setSize(1000, 1000);
    addWindowListener(new WinListener());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
    //setUndecorated(false);
    addKeyListener(input);
    setVisible(true);
}    
public void init() {
    this.game = this;
    input = new KeyboardInput();

    //this.setSize(WIDTH, HEIGHT);
    //this.setLocationRelativeTo(null);
    //this.setResizable(false);


  Dimension expectedDimension = new Dimension(900, 50);
  Dimension expectedDimension2 = new Dimension(100, 50);
   jButton1 = new JButton("jButton1");

jTextArea1 = new JTextArea(6,6);
jTextArea1.setBounds(0,200,200,200);
jTextArea1.setBackground(Color.BLUE);
//jTextArea1.setFocusable(false);
jTextField1 = new JTextField("jTextField1");
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(jTextField1);
panel2.add(jButton1);
panel2.setBackground(Color.BLACK); // for debug only
panel2.setPreferredSize(expectedDimension);
panel2.setMaximumSize(expectedDimension);
panel2.setMinimumSize(expectedDimension);
jPanel1 = new JPanel();
jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));
jPanel1.add(jTextArea1);
jPanel1.add(panel2);
jPanel1.setBackground(Color.RED); // for debug only
jPanel1.setPreferredSize(expectedDimension2);
jPanel1.setMaximumSize(expectedDimension2);
jPanel1.setMinimumSize(expectedDimension2);
jScrollPane1 = new JScrollPane(jPanel1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
setContentPane(jScrollPane1);

System.out.println("init");
revalidate();

    client = new GameClient(SERVER_IP, this);

    backBuffer = new BufferedImage(800* SCALE,600 * 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);
    }

}


public void disconnect() {
    Packet01Disconnect p = new Packet01Disconnect(username);
    p.writeData(client);
    client.closeSocket();
    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()) {
        if (op == 1) {
            op = 0;
        } else {
            op++;
        }
        input.up.toggle(false);
    } else if (input.down.isPressed()) {
        if (op == 0) {
            op = 1;
        } else {
            op--;
        }
        input.down.toggle(false);
    } else if (input.enter.isPressed() && op == 0) {
        runningMenu = false;
        input.enter.toggle(false);
    } 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);
        bbg.setColor(Color.white);
        bbg.drawString("Quit", 342, 425);
    } else if (op == 1) {
        bbg.setColor(Color.white);
        bbg.drawString("Start", 327, 378);
        bbg.setColor(Color.red);
        bbg.drawString("Quit", 342, 425);
    }

    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 (Tank t : stage.getPlayers()) {
        t.draw(bbg, SCALE, this);
    }
    g.drawImage(backBuffer, 0, 0, this);
}


public void update() {

    tank.update(stage);

    stage.update();
}

private long time = 0;


public void updateLogin() {
    if (username.length() < 8) {
        if (input.letter.isPressed()) {
            username += (char) input.letter.getKeyCode();
            input.letter.toggle(false);
        }
    }

    if (input.erase.isPressed() && username.length() > 0) {
        username = username.substring(0, username.length() - 1);
        input.erase.toggle(false);
    }

    if (input.enter.isPressed() && username.length() > 0) {
        input.enter.toggle(false);
        time = System.currentTimeMillis();
        Packet00Login packet = new Packet00Login(username, 0, 0, 0); 
        packet.writeData(client);
    }

    if (message.equalsIgnoreCase("connect server success")) {
        time = 0;
        runningLogin = false;
        return;
    }

    if (message.equalsIgnoreCase("Username already exists")) {
        drawLogin();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
        }
        message = "";
        username = "";
        time = 0;
    }

    if (message.equalsIgnoreCase("Server full")) {
        drawLogin();
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
        }
        System.exit(0);
    }

    if (time != 0 && message.equals("") && (System.currentTimeMillis() - time) >= 5000) {
        message = "cannot connect to the server";
        drawLogin();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
        }
        message = "";
        time = 0;
    }
}


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 static String waitPlayers = "Waiting for others players";
public String auxWaitPlayers = waitPlayers;
public static int quantPlayers = 0;

public class StringWait extends Thread {

    public void run() {
        while (true) {
            try {
                waitPlayers = "waiting  for  others  players";
                Thread.sleep(1000);
                waitPlayers = "waiting  for  others  players.";
                Thread.sleep(1000);
                waitPlayers = "waiting  for  others  players..";
                Thread.sleep(1000);
                waitPlayers = "waiting  for  others  players...";
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
    }
}


public void updateWaitPlayers() {
    if (quantPlayers == 1) {
        runningWaitPlayer = false;
    }
}


public void drawWaitPlayers() {
    Graphics g = getGraphics();
    Graphics bbg = backBuffer.getGraphics();
    bbg.setColor(Color.black);
    bbg.fillRect(0, 0, 800, 600);
    bbg.setColor(Color.white);
    bbg.setFont(fontError);
    bbg.drawString(waitPlayers, 100, 100);
    g.drawImage(backBuffer, 0, 0, this);
}

public boolean runningMenu = true, runningLogin = true, runningWaitPlayer = true, runningGame = true;

public int op2 = 0;


public void start() {
    long start;
    long elapsed;
    long wait;
    init();
    while (true) {
        runningGame = true;
        runningMenu = true;
        runningWaitPlayer = true;
        runningLogin = true;
        switch (op2) {
            //..
    }
}


public void setGameState(boolean state) {
   //...
}

public static void main(String[] args) throws InterruptedException {
    Game g = new Game();
    Thread.sleep(1000);
    g.start();
}

}

这些是我的客观界面:enter image description here

我希望有人会帮我解决我的问题。

1 个答案:

答案 0 :(得分:4)

  • 设置&#34; main&#34;容器布局管理器到BorderLayout
  • 点击此处,在GameInterface位置
  • 中添加BorderLayout.CENTER
  • 创建另一个(&#34;互动&#34;)容器并将其布局管理器设置为BorderLayout,将其添加到&#34; main&#34;容器BorderLayout.SOUTH职位
  • JTextArea换成JScrollPane并将其添加到您的&#34;互动&#34;的BorderLayout.CENTER位置容器
  • 创建另一个容器(&#34;消息&#34;),这可以使用GridBagLayout。在此处添加JTextField(将GridBagConstraints#weightx设置为0并将GridBagConstraints#weightx设置为1)并将按钮添加到下一个单元格({{1设置为GridBagConstraints#gridx1设置为GridBagConstraints#weightx

有关详细信息,请参阅:

注意:

0不是应该如何进行自定义绘画。相反,覆盖Graphics g = getGraphics();等组件的paintComponent并在那里执行自定义绘画!

有关详细信息,请参阅

示例

Test layout

JPanel