codestory

Die Anleitung zu Java SWT Link

  1. SWT Link
  2. Das Beispiel von SWT Link

1. SWT Link

SWT Link bezeichnet eine Hyper Link. Sie ist so ähnlich wie die Hyper Link in HTML
// Create a Link
Link link = new Link(shell, SWT.NONE);
link.setText("Go to <a href=\"htpps://eclipse.org\">Eclipse</a> Home Page.");

// Event handling when users click on links.
link.addSelectionListener(new SelectionAdapter()  {
    @Override
    public void widgetSelected(SelectionEvent e) {
        Program.launch("https:/eclipse.org");
    }
});

2. Das Beispiel von SWT Link

Das Beispiel baut einen Hyperlink . Wenn Sie ihn klicken, wechselt er zur Starseite von Eclipse (https://eclipse.org).
LinkDemo.java
package org.o7planning.swt.link;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;

public class LinkDemo {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT Link (o7planning.org)");
        
        RowLayout rowLayout= new RowLayout();
        rowLayout.marginLeft=10;
        rowLayout.marginTop=10;
        shell.setLayout(new RowLayout());
        // Create a Link
        Link link = new Link(shell, SWT.NONE);
        link.setText("Go to <a href=\"htpps://eclipse.org\">Eclipse</a> Home Page.");
        
        // Event handling when users click on links.
        link.addSelectionListener(new SelectionAdapter()  {
            @Override
            public void widgetSelected(SelectionEvent e) {
                Program.launch("https:/eclipse.org");
            }
        });
        shell.setSize(400, 250);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}