java – 如何使用超链接创建一个复选框
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何使用超链接创建一个复选框,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5579字,纯文字阅读大概需要8分钟。
内容图文
题 :
我尝试在Java中创建一个包含超链接的复选框.
但是,我无法使链接可点击,只能链接,而不是整个文本.
这是我的示例代码:
public class TestClickLinkInCheckBox implements MouseListener {
private JFrame frame1;
private JFrame frame2;
private String linkedText = "I have checked the data set I have selected, and agree to sign it following <a href=\"http://www.google.com\">the conditions of use of the service, defined in the policies of signature and certification</a> that I attest having read.";
private JLabel label;
public TestClickLinkInCheckBox() {
justCheckBox();
checkBoxWithLabel();
}
private void justCheckBox() throws HeadlessException {
frame1 = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
JCheckBox checkBox = new JCheckBox(prettyText(linkedText, 300, "left"));
panel.add(checkBox);
frame1.add(panel);
}
private void checkBoxWithLabel() {
frame2 = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
JCheckBox checkBox = new JCheckBox();
panel.add(checkBox);
label = new JLabel(prettyText(linkedText, 300, "left"));
label.addMouseListener(this);
panel.add(label);
frame2.add(panel);
}
private void display() {
frame1.setVisible(true);
frame1.pack();
frame2.setVisible(true);
frame2.pack();
}
public static String prettyText(String badText, int length, String textAlign) {
return "<html><body width='" + String.valueOf(length) + "px'><div style=\"text-align: " + textAlign + ";\">"+ badText.replace("|||", "<br>") + "</html>";
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == label) {
JOptionPane.showConfirmDialog(frame2, "Clicked");
}
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
TestClickLinkInCheckBox test = new TestClickLinkInCheckBox();
test.display();
}
}
frame1显示一个简单的复选框,整个文本是可点击的,然后选中/取消选中该复选框.
这就是我制作frame2的原因,它由复选框和JLabel组成.
它工作得很好,但它是可点击的所有文本.
有没有办法让链接可点击?
谢谢
路飞
[编辑]回复:
感谢@camickr给我一个回应.
这里的解决方案,最后不是那么容易做到的:
public class TestClickLinkInCheckBox implements HyperlinkListener {
private JFrame frame;
private String linkedText = "I have checked the data set I have selected, and agree to sign it following <b><u><a href=\"http://www.google.com\" style=\"color: #0000ff\">the conditions of use of the service, defined in the policies of signature and certification</a></u></b> that I attest having read.";
private JTextPane textPane;
public static void main(String[] args) {
TestClickLinkInCheckBox test = new TestClickLinkInCheckBox();
test.display();
}
public TestClickLinkInCheckBox() {
checkboxWithJEditorPanel();
}
private void checkboxWithJEditorPanel() {
frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
JCheckBox checkBox = new JCheckBox();
panel.add(checkBox);
textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setOpaque(false);
textPane.setDocument(new HTMLDocument());
textPane.setText(prettyText(linkedText, 300, "left"));
textPane.setEditable(false);
textPane.addHyperlinkListener(this);
setFontSize(16);
panel.add(textPane);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setFontSize(int size) {
MutableAttributeSet attrs = textPane.getInputAttributes();
StyleConstants.setFontSize(attrs, size);
StyledDocument document = textPane.getStyledDocument();
document.setCharacterAttributes(0, document.getLength() + 1, attrs, false);
}
private void display() {
frame.setVisible(true);
frame.pack();
}
public static String prettyText(String badText, int length, String textAlign) {
return "<html><body width='" + String.valueOf(length) + "px'><div style=\"text-align: " + textAlign + ";\">"+ badText.replace("|||", "<br>") + "</html>";
}
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println(e.getURL());
}
}
}
一些解释要理解:
>我使用JtextPane代替JEditorPane,因为文本没有包装在文本编辑器中,我可以使用我的html标签.
>我修改了提供的文字,以添加粗体和下划线标签< b>< u>而且我还将超链接的颜色更改为普通链接.在其他情况下,您将获得链接,但它将具有相同的文本颜色.
>注意每次鼠标悬停在链接上时生成事件的HyperlinkListener,这就是为什么我使用与“点击”对应的HyperlinkEvent.EventType.ACTIVATED过滤事件的原因
> Chang的字体大小/样式/颜色到全文它真的很棘手,你需要使用我的技术,方法setFont在这里不起作用
请享用 :)
[EDIT2]要知道的事情:
>如果您使用的是Nimbus Look& Feel:
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
有一个错误,你将有一个白色背景.所以你需要通过添加这段代码而不是setBackground()方法来修复它:
Color bgColor = panel.getBackground();
UIDefaults defaults = new UIDefaults();
defaults.put("TextPane[Enabled].backgroundPainter", bgColor);
textPane.putClientProperty("Nimbus.Overrides", defaults);
textPane.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
textPane.setBackground(bgColor);
对于JEditorPane也是如此,但更改了该行
defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
解决方法:
我知道如何在基本JDK中点击超链接的唯一方法是使用JEditorPane.
所以你可以用两个组件创建一个JPanel:
>一个JCheckBox(没有文字)
>带有超链接的JEditorPane
您可以轻松地将JEditorPane自定义为透明且没有Border,因此看起来这两个组件是单个组件.
内容总结
以上是互联网集市为您收集整理的java – 如何使用超链接创建一个复选框全部内容,希望文章能够帮你解决java – 如何使用超链接创建一个复选框所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。