我只想在Java SWT中的两个页面之间进行简单的链接,就像这个HTML代码一样。
page1.html:
<html>
<body>
<p>I'm on Page 1</p>
<a href="page2.html"><input type="button" value="Go to Page 2"></a>
</body>
</html>
page2.html:
<html>
<body>
<p>I'm on Page 2</p>
<a href="page1.html"><input type="button" value="Go to Page 1"></a>
</body>
</html>
你能给我一些在Java SWT中做同样事情的最好方法吗?
请注意,我不想链接到HTML页面。我想在两个Java SWT视图之间建立链接。
目前,我有这段代码:
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Composite composite = new Page1(shell, SWT.NONE);
composite.setBounds(0, 0, 430, 260);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
Page1.class:
public class Page1 extends Composite {
public Page1(Composite parent, int style) {
super(parent, style);
Label lblPage1 = new Label(this, SWT.NONE);
lblPage1.setLocation(10, 10);
lblPage1.setSize(80, 15);
lblPage1.setText("I'm on Page 1");
Button btnPage2 = new Button(this, SWT.NONE);
btnPage2.setLocation(10, 30);
btnPage2.setSize(80, 25);
btnPage2.setText("Go to Page 2");
}
}
Page2.class:
public class Page2 extends Composite {
public Page2(Composite parent, int style) {
super(parent, style);
Label lblPage2 = new Label(this, SWT.NONE);
lblPage2.setLocation(10, 10);
lblPage2.setSize(80, 15);
lblPage2.setText("I'm on Page 2");
Button btnPage1 = new Button(this, SWT.NONE);
btnPage1.setLocation(10, 30);
btnPage1.setSize(80, 25);
btnPage1.setText("Go to Page 1");
}
}
答案 0 :(得分:1)
基本上你可以做两件事:
TabFolder
并在标签StackLayout
并将您的个人内容作为堆栈中的单独图层以下是TabFolder
:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout());
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
for (int i= 0; i< 10; i++)
{
TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
tabItem.setText("Tab " + i);
Text text = new Text(tabFolder, SWT.BORDER);
text.setText("This is page " + i);
tabItem.setControl(text);
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
以下是StackLayout
:
private static int current = 0;
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout());
final Composite parent = new Composite(shell, SWT.NONE);
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final StackLayout layout = new StackLayout();
parent.setLayout(layout);
final Button[] buttons = new Button[10];
for (int i = 0; i < 10; i++)
{
buttons[i] = new Button(parent, SWT.PUSH);
buttons[i].setText("Button " + i);
}
layout.topControl = buttons[0];
Button switchButton = new Button(shell, SWT.PUSH);
switchButton.setText("Show Next Button");
switchButton.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event e)
{
current = (current + 1) % buttons.length;
layout.topControl = buttons[current];
parent.layout();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
<强>更新强>:
好的,这里有。现在,您可以在没有“主按钮”的情况下切换“页面”:
private static int current = 0;
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout());
final Composite parent = new Composite(shell, SWT.NONE);
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final StackLayout layout = new StackLayout();
parent.setLayout(layout);
final Composite[] pages = new Composite[5];
for (int i = 0; i < pages.length; i++)
{
pages[i] = new Composite(parent, SWT.NONE);
pages[i].setLayout(new GridLayout(3, true));
pages[i].setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for (int j = 0; j < 9; j++)
new Label(pages[i], SWT.NONE).setText("C" + i + " B" + j);
Button goToNext = new Button(pages[i], SWT.PUSH);
goToNext.setText("Go to next");
goToNext.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
current = (current + 1) % pages.length;
layout.topControl = pages[current];
parent.layout();
}
});
}
layout.topControl = pages[0];
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
答案 1 :(得分:0)
App.java:
public class App {
static Composite parent;
static StackLayout stackLayout;
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setText("SWT Application");
shell.setLayout(new GridLayout());
parent = new Composite(shell, SWT.NONE);
stackLayout = new StackLayout();
parent.setLayout(stackLayout);
//----------Page 1----------
final Page1 page1 = new Page1(parent, SWT.NONE);
page1.setLayout(new GridLayout());
//----------Page 2----------
final Page2 page2 = new Page2(parent, SWT.NONE);
page2.setLayout(new GridLayout());
//----------Vector----------
final Vector<Composite> pageVector = new Vector<Composite>();
pageVector.add(0, page1);
pageVector.add(1, page2);
//----------addLink----------
page1.addLink(pageVector);
page2.addLink(pageVector);
//----------topControl----------
stackLayout.topControl = page1;
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Page1.java:
public class Page1 extends Composite {
Button btnPage2;
public Page1(Composite parent, int style) {
super(parent, style);
Label lblPage1 = new Label(this, SWT.NONE);
lblPage1.setLocation(10, 10);
lblPage1.setSize(80, 15);
lblPage1.setText("I'm on Page 1");
btnPage2 = new Button(this, SWT.NONE);
btnPage2.setLocation(10, 30);
btnPage2.setSize(80, 25);
btnPage2.setText("Go to Page 2");
}
public void addLink(final Vector<Composite> pageVector) {
btnPage2.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
App.stackLayout.topControl = pageVector.get(1);
App.parent.layout();
}
});
}
}
Page2.java:
public class Page2 extends Composite {
Button btnPage1;
public Page2(Composite parent, int style) {
super(parent, style);
Label lblPage2 = new Label(this, SWT.NONE);
lblPage2.setLocation(10, 10);
lblPage2.setSize(80, 15);
lblPage2.setText("I'm on Page 2");
btnPage1 = new Button(this, SWT.NONE);
btnPage1.setLocation(10, 30);
btnPage1.setSize(80, 25);
btnPage1.setText("Go to Page 1");
}
public void addLink(final Vector<Composite> pageVector) {
btnPage1.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
App.stackLayout.topControl = pageVector.get(0);
App.parent.layout();
}
});
}
}