الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكله في(link botton) لايعمل

بدأه computer2008 في 18 يونيو 2010 · 4 رد · 442 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

أعضاء ومشرفين المنتدى الأكثر من

رائع

واجهتني مشكله في الكود

الذي يفتح لي رابط لصفحة الأنترنت

بحيث الكود ليس فيه أخطاء

ولكن المشكلة

عند الضغط على الرابط

لايستجيب

لي ولايفتح لي صفحة الأنترنت

والكود موجود أدناه

 

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.net.URL;

import javax.swing.Action;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalButtonUI;

public class JLinkButton extends JButton {
  private static final String uiString = "LinkButtonUI";

  public static final int ALWAYS_UNDERLINE = 0;

  public static final int HOVER_UNDERLINE = 1;

  public static final int NEVER_UNDERLINE = 2;

  public static final int SYSTEM_DEFAULT = 3;

  private int linkBehavior;

  private Color linkColor;

  private Color colorPressed;

  private Color visitedLinkColor;

  private Color disabledLinkColor;

  private URL buttonURL;

  private Action defaultAction;

  private boolean isLinkVisited;

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.getContentPane().setLayout(new GridLayout(0,2));
    f.getContentPane().add(new JLinkButton("www.java2s.com"));
    f.getContentPane().add(new JLinkButton("www.java2s.com/ExampleCode/CatalogExampleCode.htm"));
    f.setSize(600, 200);
    f.setVisible(true);
  }

  public JLinkButton() {
    this(null, null, null);
  }

  public JLinkButton(Action action) {
    this();
    setAction(action);
  }

  public JLinkButton(Icon icon) {
    this(null, icon, null);
  }

  public JLinkButton(String s) {
    this(s, null, null);
  }

  public JLinkButton(URL url) {
    this(null, null, url);
  }

  public JLinkButton(String s, URL url) {
    this(s, null, url);
  }

  public JLinkButton(Icon icon, URL url) {
    this(null, icon, url);
  }

  public JLinkButton(String text, Icon icon, URL url) {
    super(text, icon);
    linkBehavior = SYSTEM_DEFAULT;
    linkColor = Color.blue;
    colorPressed = Color.red;
    visitedLinkColor = new Color(128, 0, 128);
    if (text == null && url != null)
      setText(url.toExternalForm());
    setLinkURL(url);
    setCursor(Cursor.getPredefinedCursor(12));
    setBorderPainted(false);
    setContentAreaFilled(false);
    setRolloverEnabled(true);
    addActionListener(defaultAction);
  }

  public void updateUI() {
    setUI(BasicLinkButtonUI.createUI(this));
  }

  private void setDefault() {
    UIManager.getDefaults().put("LinkButtonUI", "BasicLinkButtonUI");
  }

  public String getUIClassID() {
    return "LinkButtonUI";
  }

  protected void setupToolTipText() {
    String tip = null;
    if (buttonURL != null)
      tip = buttonURL.toExternalForm();
    setToolTipText(tip);
  }

  public void setLinkBehavior(int bnew) {
    checkLinkBehaviour(bnew);
    int old = linkBehavior;
    linkBehavior = bnew;
    firePropertyChange("linkBehavior", old, bnew);
    repaint();
  }

  private void checkLinkBehaviour(int beha) {
    if (beha != ALWAYS_UNDERLINE && beha != HOVER_UNDERLINE
        && beha != NEVER_UNDERLINE && beha != SYSTEM_DEFAULT)
      throw new IllegalArgumentException("Not a legal LinkBehavior");
    else
      return;
  }

  public int getLinkBehavior() {
    return linkBehavior;
  }

  public void setLinkColor(Color color) {
    Color colorOld = linkColor;
    linkColor = color;
    firePropertyChange("linkColor", colorOld, color);
    repaint();
  }

  public Color getLinkColor() {
    return linkColor;
  }

  public void setActiveLinkColor(Color colorNew) {
    Color colorOld = colorPressed;
    colorPressed = colorNew;
    firePropertyChange("activeLinkColor", colorOld, colorNew);
    repaint();
  }

  public Color getActiveLinkColor() {
    return colorPressed;
  }

  public void setDisabledLinkColor(Color color) {
    Color colorOld = disabledLinkColor;
    disabledLinkColor = color;
    firePropertyChange("disabledLinkColor", colorOld, color);
    if (!isEnabled())
      repaint();
  }

  public Color getDisabledLinkColor() {
    return disabledLinkColor;
  }

  public void setVisitedLinkColor(Color colorNew) {
    Color colorOld = visitedLinkColor;
    visitedLinkColor = colorNew;
    firePropertyChange("visitedLinkColor", colorOld, colorNew);
    repaint();
  }

  public Color getVisitedLinkColor() {
    return visitedLinkColor;
  }

  public URL getLinkURL() {
    return buttonURL;
  }

  public void setLinkURL(URL url) {
    URL urlOld = buttonURL;
    buttonURL = url;
    setupToolTipText();
    firePropertyChange("linkURL", urlOld, url);
    revalidate();
    repaint();
  }

  public void setLinkVisited(boolean flagNew) {
    boolean flagOld = isLinkVisited;
    isLinkVisited = flagNew;
    firePropertyChange("linkVisited", flagOld, flagNew);
    repaint();
  }

  public boolean isLinkVisited() {
    return isLinkVisited;
  }

  public void setDefaultAction(Action actionNew) {
    Action actionOld = defaultAction;
    defaultAction = actionNew;
    firePropertyChange("defaultAction", actionOld, actionNew);
  }

  public Action getDefaultAction() {
    return defaultAction;
  }

  protected String paramString() {
    String str;
    if (linkBehavior == ALWAYS_UNDERLINE)
      str = "ALWAYS_UNDERLINE";
    else if (linkBehavior == HOVER_UNDERLINE)
      str = "HOVER_UNDERLINE";
    else if (linkBehavior == NEVER_UNDERLINE)
      str = "NEVER_UNDERLINE";
    else
      str = "SYSTEM_DEFAULT";
    String colorStr = linkColor == null ? "" : linkColor.toString();
    String colorPressStr = colorPressed == null ? "" : colorPressed
        .toString();
    String disabledLinkColorStr = disabledLinkColor == null ? ""
        : disabledLinkColor.toString();
    String visitedLinkColorStr = visitedLinkColor == null ? ""
        : visitedLinkColor.toString();
    String buttonURLStr = buttonURL == null ? "" : buttonURL.toString();
    String isLinkVisitedStr = isLinkVisited ? "true" : "false";
    return super.paramString() + ",linkBehavior=" + str + ",linkURL="
        + buttonURLStr + ",linkColor=" + colorStr + ",activeLinkColor="
        + colorPressStr + ",disabledLinkColor=" + disabledLinkColorStr
        + ",visitedLinkColor=" + visitedLinkColorStr
        + ",linkvisitedString=" + isLinkVisitedStr;
  }
}

class BasicLinkButtonUI extends MetalButtonUI {
  private static final BasicLinkButtonUI ui = new BasicLinkButtonUI();

  public BasicLinkButtonUI() {
  }

  public static ComponentUI createUI(JComponent jcomponent) {
    return ui;
  }

  protected void paintText(Graphics g, JComponent com, Rectangle rect,
      String s) {
    JLinkButton bn = (JLinkButton) com;
    ButtonModel bnModel = bn.getModel();
    Color color = bn.getForeground();
    Object obj = null;
    if (bnModel.isEnabled()) {
      if (bnModel.isPressed())
        bn.setForeground(bn.getActiveLinkColor());
      else if (bn.isLinkVisited())
        bn.setForeground(bn.getVisitedLinkColor());

      else
        bn.setForeground(bn.getLinkColor());
    } else {
      if (bn.getDisabledLinkColor() != null)
        bn.setForeground(bn.getDisabledLinkColor());
    }
    super.paintText(g, com, rect, s);
    int behaviour = bn.getLinkBehavior();
    boolean drawLine = false;
    if (behaviour == JLinkButton.HOVER_UNDERLINE) {
      if (bnModel.isRollover())
        drawLine = true;
    } else if (behaviour == JLinkButton.ALWAYS_UNDERLINE || behaviour == JLinkButton.SYSTEM_DEFAULT)
      drawLine = true;
    if (!drawLine)
      return;
    FontMetrics fm = g.getFontMetrics();
    int x = rect.x + getTextShiftOffset();
    int y = (rect.y + fm.getAscent() + fm.getDescent() + getTextShiftOffset()) - 1;
    if (bnModel.isEnabled()) {
      g.setColor(bn.getForeground());
      g.drawLine(x, y, (x + rect.width) - 1, y);
    } else {
      g.setColor(bn.getBackground().brighter());
      g.drawLine(x, y, (x + rect.width) - 1, y);
    }
  }
}

فأرجو منكم أن تساعدوني في حل المشكله

أو تزويدي بكود أخر يعمل

ولكم من جزيل الشكر

دمتم جمعيا في حفظ الرحمن ورعايته

وجزاكم الله خير

#2

هل قمتي بكتابة هذه الشيفرة؟

لا أعتقد لأنها شيفرة معقدة جداً

على كل لأسهل عليك الموضوع

في شيفرتك ما يسمى Action من هنا يجب أن تبدئي

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3
علاء الصالحي كتب:

هل قمتي بكتابة هذه الشيفرة؟

لا أعتقد لأنها شيفرة معقدة جداً

على كل لأسهل عليك الموضوع

في شيفرتك ما يسمى Action من هنا يجب أن تبدئي

تحياتي

شكرا

أستاذ

علاء الصالحي

لقد قمت بعمل ماأقترحته لي

ولكن لايعمل

وبالنسبه للكود لقد بحث في الانترنت ولم

أجد سوى هذا الكود وأذا كان لديك كود أسهل

من هذا الكود فزودني به

لان المهم لدي فقط هو طريقة فتح الرابط

ولك مني جزيل الشكر

#4

يا أختي أرني محاولاتك للذي قلته لك؟

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#5

لم اجرب الكود ولكنه واضحه انه يقوم بعمل custom JButton ، ولكن عليك وضع الAction .. ربما ب action.setActionListener وتقوم بتعريف الدالة actionPerformed كما في أي JButton عادي.. ثم تضع الكود الذي يقوم بفتح الصفحة ..

اليس هذا الموضوع مشابه لهذا:

/index.php?showtopic=223308&st=10

بالتوفيق،

http://informatic-ar.com منصة تعليمية عربية في علوم الحاسب والبرمجة

https://moalfat.com  للكتب الالكترونية والكورسات التعليمية

Everything we see now is just an engineering solution based on old science

مواضيع مشابهة