gpt4 book ai didi

Java JTree 节点是一个可点击的 URL 链接

转载 作者:行者123 更新时间:2023-11-30 02:31:05 24 4
gpt4 key购买 nike

我正在尝试使节点成为可点击的 URL,但我似乎不知道如何实现。

我到处寻找,但似乎找不到解决方案。

这是我的代码:

public class NyttigeLinks {

private static JFrame nyttigeLinks;

public static void main(String[] args) {

initialize();
}

public NyttigeLinks() {

}

private static void initialize() {
nyttigeLinks = new JFrame();
nyttigeLinks.setBounds(new Rectangle(0, 0, 350, 650));
nyttigeLinks.getContentPane().setBounds(new Rectangle(0, 0, 350, 650));
nyttigeLinks.getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
nyttigeLinks.getContentPane().setLayout(null);

JLabel logoLabel = new JLabel("");
logoLabel.setIcon(new ImageIcon(NyttigeLinks.class.getResource("/images/ssiLogo.jpg")));
logoLabel.setBounds(0, 0, 350, 60);

JTree tree = new JTree();

nyttigeLinks.getContentPane().add(logoLabel);

JScrollPane scrollPane = new JScrollPane(tree);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(10, 71, 324, 508);
nyttigeLinks.getContentPane().add(scrollPane);

tree.setModel(new DefaultTreeModel(
new DefaultMutableTreeNode("Nyttige Links\t") {
{
DefaultMutableTreeNode node_1;
node_1 = new DefaultMutableTreeNode("Projekt Wiki");
node_1.add(new DefaultMutableTreeNode("AO"));
node_1.add(new DefaultMutableTreeNode("Attends"));
node_1.add(new DefaultMutableTreeNode("Carlsberg"));
node_1.add(new DefaultMutableTreeNode("COOP"));
node_1.add(new DefaultMutableTreeNode("Dafgaard"));
node_1.add(new DefaultMutableTreeNode("Jysk DK"));
node_1.add(new DefaultMutableTreeNode("Jysk SE"));
node_1.add(new DefaultMutableTreeNode("Kvadrat"));
node_1.add(new DefaultMutableTreeNode("Solar"));
node_1.add(new DefaultMutableTreeNode("Stockmann"));
node_1.add(new DefaultMutableTreeNode("Tine"));
node_1.add(new DefaultMutableTreeNode("Unicef"));
node_1.add(new DefaultMutableTreeNode("Vectura"));
add(node_1);
node_1 = new DefaultMutableTreeNode("Helpdesk Norcic");
node_1.add(new DefaultMutableTreeNode("Test"));
add(node_1);
node_1 = new DefaultMutableTreeNode("Test");
node_1.add(new DefaultMutableTreeNode("AO"));
node_1.add(new DefaultMutableTreeNode("Attends"));
node_1.add(new DefaultMutableTreeNode("Carlsberg"));
node_1.add(new DefaultMutableTreeNode("COOP"));
node_1.add(new DefaultMutableTreeNode("Dafgaard"));
node_1.add(new DefaultMutableTreeNode("Jysk DK"));
node_1.add(new DefaultMutableTreeNode("Jysk SE"));
node_1.add(new DefaultMutableTreeNode("Kvadrat"));
node_1.add(new DefaultMutableTreeNode("Solar"));
node_1.add(new DefaultMutableTreeNode("Stockmann"));
node_1.add(new DefaultMutableTreeNode("Tine"));
node_1.add(new DefaultMutableTreeNode("Unicef"));
node_1.add(new DefaultMutableTreeNode("VecturaAO"));
node_1.add(new DefaultMutableTreeNode("Attends"));
node_1.add(new DefaultMutableTreeNode("Carlsberg"));
node_1.add(new DefaultMutableTreeNode("COOP"));
node_1.add(new DefaultMutableTreeNode("Dafgaard"));
node_1.add(new DefaultMutableTreeNode("Jysk DK"));
node_1.add(new DefaultMutableTreeNode("Jysk SE"));
node_1.add(new DefaultMutableTreeNode("Kvadrat"));
node_1.add(new DefaultMutableTreeNode("Solar"));
node_1.add(new DefaultMutableTreeNode("Stockmann"));
node_1.add(new DefaultMutableTreeNode("Tine"));
node_1.add(new DefaultMutableTreeNode("Unicef"));
node_1.add(new DefaultMutableTreeNode("VecturaAO"));
node_1.add(new DefaultMutableTreeNode("Attends"));
node_1.add(new DefaultMutableTreeNode("Carlsberg"));
node_1.add(new DefaultMutableTreeNode("COOP"));
node_1.add(new DefaultMutableTreeNode("Dafgaard"));
node_1.add(new DefaultMutableTreeNode("Jysk DK"));
node_1.add(new DefaultMutableTreeNode("Jysk SE"));
node_1.add(new DefaultMutableTreeNode("Kvadrat"));
node_1.add(new DefaultMutableTreeNode("Solar"));
node_1.add(new DefaultMutableTreeNode("Stockmann"));
node_1.add(new DefaultMutableTreeNode("Tine"));
node_1.add(new DefaultMutableTreeNode("Unicef"));
node_1.add(new DefaultMutableTreeNode("Vectura"));
add(node_1);
}
}
));
tree.setBounds(10, 71, 324, 540);
tree.setRootVisible(false);

JLabel bottomLabelTop = new JLabel(" Nyttige Links Version 1.0");
bottomLabelTop.setBounds(0, 590, 230, 14);
nyttigeLinks.getContentPane().add(bottomLabelTop);

JLabel bottomLabelBot = new JLabel(" Made by xxx");
bottomLabelBot.setBounds(0, 605, 230, 15);
nyttigeLinks.getContentPane().add(bottomLabelBot);

JButton btnNewButton = new JButton("Admin");
btnNewButton.setIcon(new ImageIcon(NyttigeLinks.class.getResource("/images/appIcon.ico")));
btnNewButton.setBounds(240, 590, 80, 20);
nyttigeLinks.getContentPane().add(btnNewButton);
nyttigeLinks.setPreferredSize(new Dimension(350, 650));
nyttigeLinks.setSize(new Dimension(350, 650));
nyttigeLinks.setResizable(false);
nyttigeLinks.setTitle("Nyttige Links");
nyttigeLinks.setIconImage(Toolkit.getDefaultToolkit().getImage(NyttigeLinks.class.getResource("/images/appIcon.ico")));
nyttigeLinks.setBackground(Color.YELLOW);
nyttigeLinks.setBounds(100, 100, 350, 650);
nyttigeLinks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nyttigeLinks.setVisible(true);
}
}

很抱歉我有点菜鸟,但你总得从某个地方开始,对吧?

如果有人能指出我正确的方向,我将非常感激!祝大家晚安!

最佳答案

我不相信有办法让节点本身成为一个 URL。但是,您可以通过使用监听器在单击节点时打开浏览器来完成相同的操作。要打开浏览器,您可以使用 Desktop#browse(URI)方法。

关于单击主题,我建议在单击节点时不要打开 URL。对于用户来说,这会非常烦人(IMO)。相反,我建议使用双击。 This answer提供了一种区分单击和双击的好方法,以及如何将其添加到中。

<小时/>

[...] but you gotta start somewhere, right?

正是如此!请允许我在这里提供更多细节,希望对学习更多内容有所帮助。

您将面临的一个挑战是如何知道为给定节点打开哪个 URL。目前您只知道显示的 String,并且我认为您不希望显示每个节点的完整 URL 是一个安全的假设。我建议创建一个对象来传递给 DefaultMutableTreeNode 构造函数,而不是使用 String 参数创建 DefaultMutableTreeNode 对象。该对象还可以具有 URI 属性,以便它知道要打开哪个 URL。

例如:

public class LeafNodeObject {
private final URI uri;

public LeafNodeObject(final String display, final URI uri) {
this.display = display;
this.uri = uri;
}

/**
* Override so that we control what is display on the Node
*/
@Override
public String toString() {
return display;
}

public void onDoubleClick() {
try {
Desktop.getDesktop().browse(uri);
} catch (final IOException e) {
e.printStackTrace();
}
}

}

可以像这样添加节点:

node_1.add(new DefaultMutableTreeNode(new LeafNodeObject("Test", new URI("www.eclipse.org"))));

在我们的监听器中(对上面的链接稍作修改):

final MouseListener ml = new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
final int selRow = tree.getRowForLocation(e.getX(), e.getY());
final TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if (selRow != -1) {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
if (e.getClickCount() == 1) {
// Single click
} else if (e.getClickCount() == 2) {
// Double click
((LeafNodeObject) node.getUserObject()).onDoubleClick();
}
}
}
};

现在,当出现双击事件时,我们会调用我们创建的 LeafNodeObject 上的 onDoubleClick() 方法。

这看起来很棒,只是我们会遇到非叶节点的节点的类转换问题!为了解决这个问题,我们可以为这些节点创建一个类似的类(例如ParentNodeObject)。为了良好的 OOP,我们应该认识到这两个类的共同行为(onDoubleClick()onSingleClick()),并为共享能力。

例如:

public interface NodeObject {
public void onSingleClick();
public void onDoubleClick();
}

现在我们可以让我们的两个类实现这个接口(interface):

public class ParentNodeObject implements NodeObject {
private final String display;

public ParentNodeObject(final String display) {
this.display = display;
}

/**
* Override so that we control what is display on the Node
*/
@Override
public String toString() {
return display;
}

@Override
public void onSingleClick() {
// Do nothing
}

@Override
public void onDoubleClick() {
// Do nothing
}
}

public class LeafNodeObject implements NodeObject {
private final URI uri;

public LeafNodeObject(final String display, final URI uri) {
this.display = display;
this.uri = uri;
}

/**
* Override so that we control what is display on the Node
*/
@Override
public String toString() {
return display;
}

@Override
public void onSingleClick() {
// Do nothing
}

@Override
public void onDoubleClick() {
try {
Desktop.getDesktop().browse(uri);
} catch (final IOException e) {
e.printStackTrace();
}
}

}

因此,在我们的监听器中,我们不再需要知道(也不再关心)该节点是父节点还是叶节点。我们可以简单地调用 onSingleClick()onDoubleClick(),实现会处理剩下的事情!

if (e.getClickCount() == 1) {
// Single click
((NodeObject) node.getUserObject()).onSingleClick();
} else if (e.getClickCount() == 2) {
// Double click
((NodeObject) node.getUserObject()).onDoubleClick();
}

回到代码中,可以像这样添加父节点和子节点:

node_1 = new DefaultMutableTreeNode(new ParentNodeObject("Helpdesk Norcic"));
node_1.add(new DefaultMutableTreeNode(new LeafNodeObject("Test", new URI("www.eclipse.org"))));

现在,当您单击或双击任何节点时,onSingleClick()onDoubleClick() 方法也将被调用。在本例中我们只关心双击叶子节点时会发生什么,所以我们只需要填写该方法即可。

关于Java JTree 节点是一个可点击的 URL 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267825/

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