私の用語やフレーズが正しくない場合、私は今や謝ります。また、私はこれを動作させるように努力してきた、控えめで知的な方法をお詫び申し上げます。swing - なぜ2つの独立したボックスが作成されているのですか?
私はJTextAreaを内部に持つ単一のボックスを作成しようとしていますが、この領域(まだ同じボックスにあります)にはいくつかのボタンがあります。しかし、私がやったことは、ボタンが入ったボックスとテキストエリアのある別のボックスを作ることだけでした。
私は、人々が同様のことをしようとしている多くの例を見てきました。私はそれを理解し実装することができたと思っていましたが、まだ成功していません。誰かが私が誤解しているコードの部分を私に指摘できたら、私は非常に感謝しています。
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.*;
public class GameGUI extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea(50, 50);
JScrollPane scrollPane = new JScrollPane(textArea);
String action;
private static GUIClient client = new GUIClient();
JButton n = new JButton("N");
JButton e = new JButton("E");
JButton s = new JButton("S");
JButton w = new JButton("W");
JButton look = new JButton("LOOK");
JButton pickup = new JButton("PICKUP");
JButton hello = new JButton("HELLO");
JButton quit = new JButton("QUIT");
public GameGUI()
{
textArea.setEditable(false);
panel.add(new JScrollPane(textArea));
this.setBounds(300, 300, 750, 500);
this.setVisible(true);
this.setResizable(false);
this.setLayout(null);
n.setBounds(225, 25, 50, 50);
e.setBounds(300, 100, 50, 50);
s.setBounds(225, 175, 50, 50);
w.setBounds(150, 100, 50, 50);
look.setBounds(50, 362, 100, 50);
pickup.setBounds(200, 362, 100, 50);
hello.setBounds(350, 362, 100, 50);
quit.setBounds(500, 362, 100, 50);
this.add(n);
this.add(e);
this.add(s);
this.add(w);
this.add(look);
this.add(pickup);
this.add(hello);
this.add(quit);
n.addActionListener(this);
e.addActionListener(this);
s.addActionListener(this);
w.addActionListener(this);
look.addActionListener(this);
pickup.addActionListener(this);
hello.addActionListener(this);
quit.addActionListener(this);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void displayInGUI(String fromServer)
{
textArea.append(fromServer);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
public void actionPerformed(ActionEvent f)
{
if(f.getSource()==n)
{
action = "MOVE N";
client.display(action);
}
else if(f.getSource()==e)
{
action = "MOVE E";
client.display(action);
}
else if(f.getSource()==s)
{
action = "MOVE S";
client.display(action);
}
else if(f.getSource()==w)
{
action = "MOVE W";
client.display(action);
}
else if(f.getSource()==look)
{
action = "LOOK";
client.display(action);
}
else if(f.getSource()==pickup)
{
action = "PICKUP";
client.display(action);
}
else if(f.getSource()==hello)
{
action = "HELLO";
client.display(action);
}
else if(f.getSource()==quit)
{
action = "QUIT";
client.display(action);
}
}
}
は、私は(私も本当に醜いようで、このクラス、GameGUI、中GUIClientのインスタンスを作成したGUIClientでそのインスタンスを作成することにより、このクラスを使用しましたが、それは私が考えることができる唯一の方法でしたボタンが押されたときに通知する)。
ゲームを実行したい場合は、サーバーをセットアップし、GUIClientを実行して2つのボックスを作成します。 GUIGameは、ボタンが押されたことに気付き、この情報をクライアントに送信します。
もう一度申し訳ありませんが、これはひどく複雑です。私の説明が明確になるのを助けてくれると嬉しいです。
多くのありがとう、 エリス。
なぜあなたは' GameGUI'は 'JFrame'を延長していますか? – flakes
@flkes申し訳ありませんが、私はこれがどのように動作しているのか分かりませんでした。ありがとう - 私は今これを修正します。 – Elise