2013年12月18日星期三

java Swing doubts



Why is my own definition of JButton class does not correctly display ?
I think we should rewrite JButton some way?
The results are:

------ Solution ------- -------------------------------------
lz beginner swing it, go see their landlord achieve JButton, feel landlord has the idea to create the code for lz in :
1.lz own custom JButton is already added to the JFrame , but did not display button content.
2.lz To display the contents MyButton , you need to add a constructor in the MyButton class :
public MyButton(String text) {
super(text);
}

because you MyButton class contains a JButton, the JButton and you add in the JFrame MyButton not directly related , so that you created in the Test MyButton method is not displayed content.
------ Solution ---------------------------------------- ----
MyButton inherited JButton, new MyButton () to create the button name is not displayed , so the display is right on the screen.
You can try this.add (my_bt.bt);
------ Solution ------------------- -------------------------
you write something called "Creating did not add ."
------------------
your MyButton inherited JButton, meaning MyButton itself is jbutton,
your constructor super () this one is the MyButton set as the default JButton ().
At this point, you have a constructor which bt = JButton ("Save");
You probably online, do not show me this how ah ?
Yes, your bt actually exist, but your JFrame 's add () to add the MyButton, rather than bt = JButton ("Save"); That bt.
-------------------
you here actually creates three Button, but there are two added into the JFrame ...

not believe it, you can try to add Test in this sentence:
JButton bu = my_bt.bt;
this.add ();
try ...... [I just look at your code, have not tried , there is a problem ask it ... ]
------ For reference only ---------------------------------------
Thank stride _ YUAN Shen Jian , Yimenchuixue, weidu23 three answers , I just started to learn Swing, yesterday to write a panel in the project , there have this problem. Thank you for your answer ! ! !

没有评论:

发表评论