160 likes | 306 Views
Layout Management. Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout ( layoutmanager object); By default a panel’s layout is being managed by a FlowLayout() object.
E N D
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout ( layoutmanager object); By default a panel’s layout is being managed by a FlowLayout() object. FLOW LAYOUT - components are added left to right, starting a new row if needed Different layouts: flow layout , border layout , grid layout
Border Layout Border layout places components into positions (center, north, west, south, eas Set up and use of Border layout panel.setLayout(new BorderLayout()); panel.add(myBtn, BorderLayout.SOUTH); *Border layout grows components to fit area(Flow layout retains size) *To avoid growth, place component into another panel (with flow layout), then add panel to content pane
Grid Layout • Lays out components in rows and columns • All components have the same size • Add components left to right (top row first, then second row, etc.) • panel.setLayout(new GridLayout(4, 3));panel.add(button7);panel.add(button8);panel.add(button9);panel.add(button6);
Frame’s Default Layout is BorderLayout Suppose we want a program which provides: * a graphics panel * textfields * buttons You might want to utilize the JFrame default layout – the Border Layout. Look at file Comp0.java – frame application which provides theses things
Use Methods to Reduce Complexity As you can see, a frame’s main program can get quite complex as more components are added. By providing methods which set up all components on a panel, this complexity can be reduced.. See File : Comp.java Methods CreateIn and CreateOut return panels ready to be added to the frame.
Use Inheritance to Reduce Complexity One more modification can be made, allowing each method which sets up a panel to do all the work, including adding the panel to the frame. See File: FinalComp.java The object which is setting up the frame IS a JFRAME, so has direct access to the JFrame methods. Each method in this class can complete it’s own set up.
The inheritance also allows the application class to be instantiated and reused in other applications (together with other frames). public class RunFrame{ public static void main (String [] args) { JFrame frame = new FinalComp(); frame.show(); } }
public class Run2Frame{ public static void main (String [] args) { JFrame frame = new FinalComp(); frame.show(); JFrame frame2 = new FinalComp(); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //app ends when 2nd frame closes frame2.show(); } }
We want to repeat our TriFrame application (which draws triangles), to allow the user to input a postion used for the drawing… First, we must update the TriPanel class to provide a way for a position to be communicated to that panel , and how the panel is redrawn …. public class TriPanel2 extends JPanel{ private Triangle mytri = new Triangle(10,10); public TriPanel () { setPreferredSize( new Dimension(400,400)); } public void setTri(int xx, int yy){ //NEW METHOD mytri.setTri(xx,yy); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D gg = (Graphics2D) g; mytri.draw(gg); } }
Now, we must write the frame, which will create a TriPanel2 object, and another panel for input components. The two panels must communicate.. public class TriFrame2 extends JFrame{ private TriPanel2 mypanel = new TriPanel2(); //this panel will be used by various methods in this class public TriFrame2() { // set up and pack object getContentPane().add(mypanel,BorderLayout.CENTER); controlPanel(); pack(); }
public void controlPanel(){ final JButton inpt = new JButton("MOVE"); final JTextField xtxt = new JTextField(5); final JTextField ytxt = new JTextField(6); JLabel xlbl = new JLabel("X:"); //create input panel JLabel ylbl = new JLabel("Y:"); JPanel controlpnl = new JPanel(); controlpnl.add(xlbl); //put interactive components on panel controlpnl.add(xtxt); //flow layout used here controlpnl.add(ylbl); controlpnl.add(ytxt); // METHOD CONTINUED… (next slide)
// controlpanel method continued… class AListen implements ActionListener { public void actionPerformed(ActionEvent e) { int x = Integer.parseInt(xtxt.getText()); int y = Integer.parseInt(ytxt.getText()); mypanel.setTri(x,y); //call to ‘graphics’ panel } } AListen btnl = new AListen(); inpt.addActionListener(btnl); controlpnl.add(inpt); getContentPane().add(controlpnl,BorderLayout.SOUTH); } }
//instantiate and show a TriFrame2 object import javax.swing.*; public class RunFrame2{ public static void main (String [] args) { JFrame frame = new TriFrame2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } }