java - How do i use a gui i build together with functions from somewhere else? -


hello im new here making small tictactoe game have made gui follows:

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

i want add action listeners buttons not in void ideall different class can create way run loop of functions such player turn changer if turn 1 set button text x , o if othei know more orless code need use cant figure out way use gui anzwhere else own void. dont quite know im searching appreciated.

first of all, program structure pretty bad. looks of can tell came procedural programming language c or basic or of sort. java object oriented architecture. java can run procedurally not meant be. first thing start out of main(). here way that:

public class tictactoe { public tictactoe() { } public static void main(string args[]) { new tictactoe(); } } 

tictactoe might seem simple has lot of things going on @ once, people clicking buttons need actionlisteners, need update ui of screen after every move, need check make sure each move valid, need check win after every move , much more. impossible inside of main.

it's bad idea add sort of component straight jframe. better put jpanel inside of jframe , add components jpanel.

try making class structure game. here do:

tictactoe.class --> checks rules, checks wins , starts , stops game player.class (implements actionlistener) --> listens each player's input board.class (extends jpanel) --> display components game computer.class (extends player) --> if wanted create ai 

if read book java game development or take class. if want @ java place start. missing lot of crucial knowledge need simple task such make tictactoe game.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -