[英]How do I ignore Eclipse errors if I include junit.jar from somewhere else?
[英]How do i use a gui i build together with functions from somewhere else?
您好,我是新來的。
public static void main(String[] args) {
Frame frame1 =new Frame("TickTacToe");
frame1.setLayout(null);
frame1.setBounds(250,150,500,500);
frame1.setVisible(true);
frame1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
final Button button11 = new Button("");
button11.addActionListener(null);
final Button button12 = new Button("");
button11.addActionListener(null);
final Button button13 = new Button("");
button11.addActionListener(null);
final Button button21 = new Button("");
button11.addActionListener(null);
final Button button22 = new Button("");
button11.addActionListener(null);
final Button button23 = new Button("");
button11.addActionListener(null);
final Button button31 = new Button("");
button11.addActionListener(null);
final Button button32 = new Button("");
button11.addActionListener(null);
final Button button33 = new Button("");
button11.addActionListener(null);
button11.setBounds(100, 100, 80, 70);
button12.setBounds(100, 200, 80, 70);
button13.setBounds(100, 300, 80, 70);
button21.setBounds(200, 100, 80, 70);
button22.setBounds(200, 200, 80, 70);
button23.setBounds(200, 300, 80, 70);
button31.setBounds(300, 100, 80, 70);
button32.setBounds(300, 200, 80, 70);
button33.setBounds(300, 300, 80, 70);
frame1.add(button11);
frame1.add(button12);
frame1.add(button13);
frame1.add(button21);
frame1.add(button22);
frame1.add(button23);
frame1.add(button31);
frame1.add(button32);
frame1.add(button33);
}
我想向按鈕添加動作偵聽器,但不在這個空的Ideall中甚至沒有其他類,因此我可以創建一種方法來運行功能循環,例如播放器轉彎器,如果將其轉彎,它將按鈕文本設置為x而且,如果是其他人,我至少知道我需要使用的代碼,但是我無法找到一種從其他地方使用gui的方法,而不是它自己的void。 我不太清楚我在尋找什么,因此非常感謝任何幫助。
首先,這種程序結構非常糟糕。 從外觀上,我可以告訴您來自過程編程語言,例如C或Basic或諸如此類。 Java全部涉及面向對象的體系結構。 Java可以在程序上運行,但並非必須如此。 我首先要做的是擺脫main()。 這是執行此操作的好方法:
public class TicTacToe
{
public TicTacToe()
{
}
public static void main(String args[])
{
new TicTacToe();
}
}
TicTacToe看起來很簡單,但是同時發生了很多事情,人們單擊按鈕,因此您需要ActionListeners,需要在每次移動后更新屏幕的用戶界面,需要檢查以確保每一次移動都有效,您需要在每步動作后檢查是否有獲勝,還有更多。 這在main內部幾乎是不可能的。
將任何種類的Component直接添加到JFrame中通常是一個壞主意。 最好將JPanel放在JFrame內,然后將組件添加到JPanel。
嘗試為您的游戲制作一個類結構。 這是我會做的:
TicTacToe.class --> Checks rules, checks for wins and starts and stops game
Player.class (implements ActionListener) --> Listens for each Player's input
Board.class (extends JPanel) --> this will display the components for the game
Computer.class (extends Player) --> if you wanted to create an AI this is where you would do so
如果我是你,我會讀一本有關Java游戲開發的書或上一堂課。 如果您想精通Java,那么這是一個不錯的起點。 您缺少了很多關鍵知識,而這些知識甚至是完成TicTacToe游戲之類的簡單任務所必需的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.