官术网_书友最值得收藏!

Time for action – using layouts

Now that the ClockWidget has been created, multiple instances can be added into the ClockView.

  1. Modify the createPartControl method in the ClockView class to create three ClockWidget instances, and assign them to local variables:
    final ClockWidget clock1 = new ClockWidget(parent, SWT.NONE);
    final ClockWidget clock2 = new ClockWidget(parent, SWT.NONE);
    final ClockWidget clock3 = new ClockWidget(parent, SWT.NONE);
  2. Run the target Eclipse instance, and show the Clock View. Three clocks will be shown, counting in seconds:
  3. At the start of the ClockView class's createPartControl method, create a new RowLayout with SWT.HORIZONTAL, and then set it as the layout on the parent Composite:
    public void createPartControl(Composite parent) {
      RowLayout layout = new RowLayout(SWT.HORIZONTAL);
      parent.setLayout(layout);
  4. Run the code again now, and the clocks will be in a horizontal row:
  5. Resize the view; the clocks will flow into different rows:

    Note

    The RowLayout has a number of fields that can affect how widgets are laid out:

    • center: If components are centered (vertically or horizontally)
    • fill: If the entire size of the parent should be taken up
    • justify: If the components should be spaced so that they reach the end
    • pack: If components should get their preferred size or be expanded to fill space
    • wrap: If the components should wrap at the end of the line

    There are also options to control any pixel spacing between elements (spacing) and any margins at the edge (marginHeight and marginWidth, or ones that can be specified individually such as marginTop, marginBottom, marginLeft, and marginRight).

  6. Every SWT widget has an optional layout data object, which is specific to the kind of layout being used by its containing parent. In the ClockView method createPartControl, add a RowData object to the clocks with a different size:
    clock1.setLayoutData(new RowData(20,20));clock2.setLayoutData(new RowData(50,50));clock3.setLayoutData(new RowData(100,100));
  7. Open the Clock View, and the clocks will be shown in increasing size:

What just happened?

A Composite is capable of handling multiple widgets, and the job of deciding where to put these components is performed by the associated LayoutManager. The standard layout managers include FillLayout, RowLayout, GridLayout, FormLayout, and CellLayout. The default for Eclipse views is to use a FillLayout, though a manually created Composite has no associated layout by default.

Both FillLayout and RowLayout create a horizontal or vertical set of widgets with controlled sizes. The FillLayout is the default for views and expands the size of the widgets to the space available. RowLayout will set the component's sizes to their default as calculated by computeSize(0,0).

Layout managers have different properties, such as SWT.HORIZONTAL and SWT.VERTICAL, and to change how elements are wrapped if the row gets full. The documentation for each layout manager has information as to what it supports.

Layout data objects are used to specify different values for objects within the Composite. The preceding example looked at the RowData options.

The corresponding FillData class for the FillLayout has no public fields. Other layout managers such as GridLayout have more extensive customization options in the GridData class. Remember that when changing a LayoutManager, the associated layout data objects will need to be modified accordingly.

Pop quiz: understanding views

Q1. What is the parent class of any view in the Eclipse 3.x model?

Q2. How do you register views with the Eclipse workbench?

Q3. Which two arguments are passed into every SWT widget and what are they for?

Q4. What does it mean for a widget to be disposed?

Q5. How do you draw a circle on a Canvas?

Q6. What listener do you have to register to execute drawing operations?

Q7. What happens if you try and update an SWT object from outside a UI thread?

Q8. How do you update SWT components from a different thread?

Q9. What value is SWT.DEFAULT used for?

Q10. How do you specify a specific size for a widget in a RowLayout?

Have a go hero – drawing hour and minute hands

Now that the clock view is animating a second hand, do the same calculation for the hour and minute hands. Minutes will be calculated the same way as seconds; for hours, multiply the hours by 5 to map onto the same path.

Draw lines for every five minutes using the drawLine method. Some simple math will be required to calculate the start and end points of the line.

Finally, draw the text lettering for the numbers in the right locations. The drawText method can be used to place a string at a particular place. Use this to print out the current time in the center of the clock, or print out the date.

主站蜘蛛池模板: 迁西县| 吴旗县| 灵山县| 津市市| 甘南县| 青铜峡市| 桑日县| 宣恩县| 印江| 仁怀市| 上高县| 苏州市| 华坪县| 东乡县| 吉木萨尔县| 天水市| 江油市| 通州区| 原平市| 台山市| 大姚县| 浙江省| 黄平县| 色达县| 柞水县| 郧西县| 枝江市| 湖州市| 原阳县| 铜梁县| 郴州市| 盐城市| 广灵县| 衡阳县| 韶关市| 临颍县| 房山区| 慈利县| 新宾| 建始县| 南和县|