gpt4 book ai didi

java - 页面链接: easy with HTML but not with Java SWT

转载 作者:行者123 更新时间:2023-12-01 12:48:38 26 4
gpt4 key购买 nike

我只想在 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 View 之间建立链接。

此时,我有这样的代码:

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");
}
}

最佳答案

基本上您可以做两件事:

  1. 使用 TabFolder 并在选项卡中显示您的个人内容
  2. 使用 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)静态 int 当前 = 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();
}

关于java - 页面链接: easy with HTML but not with Java SWT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24437097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com