2013年10月5日星期六

Java, button layout how to arrange ?

I am in the process of setting the size and position of each button , how does not work ? There program produces window, click the Close button can close the window , you can not close the process , how to change ?
part of the code :
......
this.setTitle ("test");
this.setBounds (300,250,500,200);
this.setLayout (null);
j1.setBounds (60,100,400,50);
this.add (j1);
jb1.setSize (100,50);
jb1.setLocation (120,100);
this.add (jb1);

------ Solution ------------------------------------ --------
this.setLayout (null);
you this what is meant by this ? You can do this:

JPanel panel=new JPanel();
panel.setLayout(null);
JLabel label=new JLabel("Label");
label.setBounds(*,*,*,*);
panel.add(label);


is needed to set the layout of the panel layout is null, then the component is added to the panel, and then finally on the panel added to the Frame .

As you said, only click on the Close button to close the window can not be closed process because you do not use right way . Please refer to
API - JFrame 's setDefaultCloseOperation () method .
------ Solution ---------------------------------------- ----
directly on the JFrame add Button?
------ Solution - -------------------------------------------
first with getContentPane ( ) method to obtain the JFrame content pane , and then add components to its
Frame not a container . . Added directly to be wrong
------ Solution - -------------------------------------------
Container container = getContentPane ();
container.setLayout (new FlowLayout ());
container.add (......);


------ For reference only ---------------------------------- -----
Thanks a floor. Some of my questions are not clear .
actual definition of the class is derived from JFrame, so you can directly borrow this operation. The question is: When I was in the position of the button specified in the program , so that a few buttons in a row , the program executes the effect does not meet code requirements , but only in a row. What should I do ?
public class test extends JFrame
{
JButton jb1 = new JButton (" copy" ) ;
JButton jb2 = new JButton (" out of order " ) ;
JButton jb3 = new JButton (" cluster " ) ;
public test ()
{
this.setBounds (300,250,500,200);
this.setLayout (null);
jb1.setSize (100,50);
jb1.setLocation (120,100);
this.add (jb1);
jb2.setSize (100,50);
jb2.setLocation (180,100);
this.add (jb2);
jb3.setSize (100,50);
jb3.setLocation (240,100);
this.add (jb3);
........................
}

------ For reference only ---------------------------------- -----
2 floor is a positive solution

------ For reference only ---------------------------------- -----
is there no other way to the layout ?

没有评论:

发表评论