PDA

View Full Version : سوال: فوری!!!!!!!!!!!!



maryam.eng
دوشنبه 14 مرداد 1392, 13:14 عصر
سلام من می خواستم با کلاس URIلینک درست کنم و این که وقتی رو اون لینک کلیک می کنم بره با سایت اون لینک...همون آدرسی که به سازه URIمی دم..می خواستم این کارو واسه چند تا انجام بدم یعنی من تو برنامه چند تا bottomدارم که روی هر کدوم یه آدرس نوشتس می خوام رو هر دوم کلیک می کنم سایت مربوطه باز بشه ولی مشکل اینجاست که این کار فقط برای آخرین buttomکه که اد کردم رو فریم فقط اتفاق می افته ..به نظر شما مشکل کجاست؟؟؟؟

cups_of_java
دوشنبه 14 مرداد 1392, 15:48 عصر
حتمن اشتباهی توی وصل کردن ActionListenerهاتون به دکمه ها دارید!

Ma_Y_R
دوشنبه 14 مرداد 1392, 17:20 عصر
سلام من می خواستم با کلاس URIلینک درست کنم و این که وقتی رو اون لینک کلیک می کنم بره با سایت اون لینک...همون آدرسی که به سازه URIمی دم..می خواستم این کارو واسه چند تا انجام بدم یعنی من تو برنامه چند تا bottomدارم که روی هر کدوم یه آدرس نوشتس می خوام رو هر دوم کلیک می کنم سایت مربوطه باز بشه ولی مشکل اینجاست که این کار فقط برای آخرین buttomکه که اد کردم رو فریم فقط اتفاق می افته ..به نظر شما مشکل کجاست؟؟؟؟

سلام.منم یه مشکلی مشابه همین دارم.........من تو یه حلقه for چندتا botton میسازم بعد که میام تو همون حلقه واسه هرکدوم یه لینک اختصاص بدم فقط آخرین لینکو ذخیره میکنه و همه ی دکمه ها آخرین سایتو باز میکنن......میخواستم بدونم علتش چیه.................

cups_of_java
دوشنبه 14 مرداد 1392, 18:47 عصر
توی حلقه متغیری که به آدرس هر دکمه نسبت میدین درست نیست و اون ActionListenerتون که یه کلاس بی نام احتمالن هست همیشه داره اون آخرین مقداری که تو آخرین اجرای حلقه روی متغیر گذاشتین استفاده میکنه. درحالی که باید هر کدوم آدرس خودشون رو توی متغیر جدای خودشون داشته باشن!
اگه متوجه منظورم نمیشید کدتون رو بزارید تا اصلاحش کنیم.

Ma_Y_R
دوشنبه 14 مرداد 1392, 23:29 عصر
توی حلقه متغیری که به آدرس هر دکمه نسبت میدین درست نیست و اون ActionListenerتون که یه کلاس بی نام احتمالن هست همیشه داره اون آخرین مقداری که تو آخرین اجرای حلقه روی متغیر گذاشتین استفاده میکنه. درحالی که باید هر کدوم آدرس خودشون رو توی متغیر جدای خودشون داشته باشن!
اگه متوجه منظورم نمیشید کدتون رو بزارید تا اصلاحش کنیم.

این کد ها تو سازنده ی کلاس نوشته شده که آرایه ی link رو به صورت آرگومان دریافت میکنه و از نوع رشته هست و آدرس ها رو داره.و lnk یه آرایه ی سراسری تو این کلاسه.
()OpenUrlAction op=new OpenUrlAction

} (++for(int j=0;j<i;j
()jb1=new JButton
([jb1.setText(title[j
( jb1.setHorizontalAlignment(SwingConstants.HORIZONT AL
(jb1.setVerticalAlignment(SwingConstants.TOP
(jb1.setBorderPainted(false
(jb1.setOpaque(true
(jb1.setBackground(Color.pink
(jb1.setSize(100, 3000
t=j
[lnk[t]=link[j
}try
([URI uri=new URI(link[j]);
lnk1[j]=uri;
}(catch(URISyntaxException e

}

(add(jb1
(jb1.addActionListener(op
{


} public class OpenUrlAction implements ActionListener


(public void actionPerformed(ActionEvent a
}

([open(lnk1[t

}

}(private void open(URI uri
( ()if (Desktop.isDesktopSupported
}
}try
(Desktop.getDesktop().browse(uri
{

{} (catch (IOException e

else
{ }

{{

cups_of_java
دوشنبه 14 مرداد 1392, 23:45 عصر
کدت رو توی تگ کد جاوا بزار تا خوانا باشه بتونم بخونمش

cups_of_java
دوشنبه 14 مرداد 1392, 23:50 عصر
مشکلی که بهت گفتم اینجاس:
([open(lnk1[t
شما زمانی که هندلرت رو اتچ میکنی درسته اما زمانی که میخواد هندلر صدا بشه t مقدارش چیه؟ حلقه تموم شده و آخرین حلقه دیگه درسته !؟
پس همیشه آخرین لینک باز میشه.
حالا ببین خودت میتونی درسش کنی؟
باید یه متغیر لوکال توی اون کلاس هندلر بزاری و مقدار link[t] رو بریزی توش توی همون حلقه تا بمونه برات

Ma_Y_R
دوشنبه 14 مرداد 1392, 23:55 عصر
هر دکمه ای لینک مخصوص به خودش رو داره....j مشخص میکنه که کدوم دکمه فشار داده شده و اون رو توی t ذخیره میکنه تا لینک همون دکمه رو چاپ کنه

Ma_Y_R
سه شنبه 15 مرداد 1392, 00:24 صبح
مشکلی که بهت گفتم اینجاس:
([open(lnk1[t
شما زمانی که هندلرت رو اتچ میکنی درسته اما زمانی که میخواد هندلر صدا بشه t مقدارش چیه؟ حلقه تموم شده و آخرین حلقه دیگه درسته !؟
پس همیشه آخرین لینک باز میشه.
حالا ببین خودت میتونی درسش کنی؟
باید یه متغیر لوکال توی اون کلاس هندلر بزاری و مقدار link[t] رو بریزی توش توی همون حلقه تا بمونه برات

مشکلم دقیقا اینه که [lnk[t]=link[j اجرا نمیشه.....الان که رانش میکنم دیگه حتی دکمه ها هم چاپ نمیشه......مثل این که تو همین خط متوقف میشه.......

javaphantom
سه شنبه 15 مرداد 1392, 10:35 صبح
هر چند با این نوع برنامه نویسی موافق نسیتم و کلا آرایه ای از کامپونتها رو غلط می دونم ولی این نمونه کد شاید به دردتون بخوره



package com;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
* Created with IntelliJ IDEA.
* User: Babak
* Date: 8/6/13
* Time: 11:06 AM
* To change this template use File | Settings | File Templates.
*/
public class MyFrame extends JFrame {

private JButton[] buttons = new JButton[3];

public final static String LINK_ONE = "Link One";
public final static String LINK_TWO = "Link TWO";
public final static String LINK_THREE = "Link Three";


private JPanel myPanel;
private JLabel myLabel;


/**
* Creates three buttons with their button mouse event handlers
*/
private void doInitializeButtons() {
for (int buttonCounter = 0; buttonCounter < buttons.length; buttonCounter++) {
buttons[buttonCounter] = new JButton(buttonCounter + "");
buttons[buttonCounter].addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(MouseEvent e) {
JButton clickedButton = (JButton) e.getSource();
if (clickedButton.getText().equals("0")) {
getMyLabel().setText(LINK_ONE);
} else if (clickedButton.getText().equals("1")) {
getMyLabel().setText(LINK_TWO);
} else {
getMyLabel().setText(LINK_THREE);
}
}
});
}
}

public JLabel getMyLabel() {
if (myLabel == null) {
this.myLabel = new JLabel("Hello");
}
return myLabel;
}

public void setMyLabel(JLabel myLabel) {
this.myLabel = myLabel;
}

public JPanel getMyPanel() {
if (this.myPanel == null) {
this.myPanel = new JPanel(new FlowLayout());
this.doInitializeButtons();
for (JButton button : buttons) {
myPanel.add(button);
}
myPanel.add(getMyLabel());
}
return myPanel;
}

public MyFrame() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setBounds(new Rectangle(300, 300));
this.getContentPane().add(this.getMyPanel());
this.setVisible(true);
}


public static void main(String[] s) {
new MyFrame();
}
}

javaphantom
سه شنبه 15 مرداد 1392, 10:58 صبح
با اندکی تغییر نیاز شما رو هم نوشتم از بیرون براش تعیین کنی که چی کار کنه




package com;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Babak
* Date: 8/6/13
* Time: 11:06 AM
* To change this template use File | Settings | File Templates.
*/
public class MyFrame extends JFrame {

private JButton[] buttons = new JButton[3];
private JButton[] myButtons;
private List<ButtonDetails> buttonDetails;

public final static String LINK_ONE = "Link One";
public final static String LINK_TWO = "Link TWO";
public final static String LINK_THREE = "Link Three";


private JPanel myPanel;
private JLabel myLabel;


public void buttonsGenerator(List<ButtonDetails> buttonDetails) {

this.myButtons = new JButton[buttonDetails.size()];

for (int buttonCounter = 0; buttonCounter < this.myButtons.length; buttonCounter++) {
this.myButtons[buttonCounter] = new JButton(buttonDetails.get(buttonCounter).getButton Name());
this.myButtons[buttonCounter].setName(buttonDetails.get(buttonCounter).getButto nLink());
this.myButtons[buttonCounter].addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(MouseEvent e) {
JButton clickedButton = (JButton) e.getSource();
getMyLabel().setText(clickedButton.getName());
}
});
}

}

/**
* Creates three buttons with their button mouse event handlers
*/
private void doInitializeButtons() {
for (int buttonCounter = 0; buttonCounter < buttons.length; buttonCounter++) {
buttons[buttonCounter] = new JButton(buttonCounter + "");
buttons[buttonCounter].addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(MouseEvent e) {
JButton clickedButton = (JButton) e.getSource();
if (clickedButton.getText().equals("0")) {
getMyLabel().setText(LINK_ONE);
} else if (clickedButton.getText().equals("1")) {
getMyLabel().setText(LINK_TWO);
} else {
getMyLabel().setText(LINK_THREE);
}
}
});
}
}

public JLabel getMyLabel() {
if (myLabel == null) {
this.myLabel = new JLabel("Hello");
}
return myLabel;
}

public void setMyLabel(JLabel myLabel) {
this.myLabel = myLabel;
}

public JPanel getMyPanel() {
if (this.myPanel == null) {
this.myPanel = new JPanel(new FlowLayout());
if (this.buttonDetails == null) {
this.doInitializeButtons();
for (JButton button : buttons) {
myPanel.add(button);
}
} else {
this.buttonsGenerator(this.buttonDetails);
for (JButton button : myButtons) {
myPanel.add(button);
}
}
myPanel.add(getMyLabel());
}
return myPanel;
}

public MyFrame(List<ButtonDetails> buttonDetails) {
this.buttonDetails = buttonDetails;
this.buttonsGenerator(this.buttonDetails);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setBounds(new Rectangle(300, 300));
this.getContentPane().add(this.getMyPanel());
this.setVisible(true);
}

public MyFrame() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setBounds(new Rectangle(300, 300));
this.getContentPane().add(this.getMyPanel());
this.setVisible(true);
}


public static void main(String[] s) {


ButtonDetails b1 = new ButtonDetails();
b1.setButtonLink("link one");
b1.setButtonName("one");

ButtonDetails b2 = new ButtonDetails();
b2.setButtonLink("link two");
b2.setButtonName("two");

List<ButtonDetails> buttonDetailsList = new ArrayList<ButtonDetails>();
buttonDetailsList.add(b1);
buttonDetailsList.add(b2);

new MyFrame(buttonDetailsList);


}
}

class ButtonDetails {

private String buttonName;

private String buttonLink;

public String getButtonLink() {
return buttonLink;
}

public void setButtonLink(String buttonLink) {
this.buttonLink = buttonLink;
}

public String getButtonName() {
return buttonName;
}

public void setButtonName(String buttonName) {
this.buttonName = buttonName;
}
}

maryam.eng
چهارشنبه 16 مرداد 1392, 18:09 عصر
هر چند با این نوع برنامه نویسی موافق نسیتم و کلا آرایه ای از کامپونتها رو غلط می دونم ولی این نمونه کد شاید به دردتون بخوره



package com;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
* Created with IntelliJ IDEA.
* User: Babak
* Date: 8/6/13
* Time: 11:06 AM
* To change this template use File | Settings | File Templates.
*/
public class MyFrame extends JFrame {

private JButton[] buttons = new JButton[3];

public final static String LINK_ONE = "Link One";
public final static String LINK_TWO = "Link TWO";
public final static String LINK_THREE = "Link Three";


private JPanel myPanel;
private JLabel myLabel;


/**
* Creates three buttons with their button mouse event handlers
*/
private void doInitializeButtons() {
for (int buttonCounter = 0; buttonCounter < buttons.length; buttonCounter++) {
buttons[buttonCounter] = new JButton(buttonCounter + "");
buttons[buttonCounter].addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(MouseEvent e) {
JButton clickedButton = (JButton) e.getSource();
if (clickedButton.getText().equals("0")) {
getMyLabel().setText(LINK_ONE);
} else if (clickedButton.getText().equals("1")) {
getMyLabel().setText(LINK_TWO);
} else {
getMyLabel().setText(LINK_THREE);
}
}
});
}
}

public JLabel getMyLabel() {
if (myLabel == null) {
this.myLabel = new JLabel("Hello");
}
return myLabel;
}

public void setMyLabel(JLabel myLabel) {
this.myLabel = myLabel;
}

public JPanel getMyPanel() {
if (this.myPanel == null) {
this.myPanel = new JPanel(new FlowLayout());
this.doInitializeButtons();
for (JButton button : buttons) {
myPanel.add(button);
}
myPanel.add(getMyLabel());
}
return myPanel;
}

public MyFrame() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setBounds(new Rectangle(300, 300));
this.getContentPane().add(this.getMyPanel());
this.setVisible(true);
}


public static void main(String[] s) {
new MyFrame();
}
}


یه سوال در مورد این کد نمیشه آرایه ای از کامپونت ها با طول متغیر تعریف کنیم؟؟؟
اگه منبعی در مورد این موضوع است معرفی کنید.....زیاد با این موضوع آشنا نیستم.....

javaphantom
چهارشنبه 16 مرداد 1392, 18:40 عصر
یه سوال در مورد این کد نمیشه آرایه ای از کامپونت ها با طول متغیر تعریف کنیم؟؟؟
اگه منبعی در مورد این موضوع است معرفی کنید.....زیاد با این موضوع آشنا نیستم.....

در مثال دوم اگر دقت کرده باشی با طول متغیر فرستادم به این قسمت کد نگاه کن


public static void main(String[] s) {


ButtonDetails b1 = new ButtonDetails();
b1.setButtonLink("link one");
b1.setButtonName("one");

ButtonDetails b2 = new ButtonDetails();
b2.setButtonLink("link two");
b2.setButtonName("two");

List<ButtonDetails> buttonDetailsList = new ArrayList<ButtonDetails>();
buttonDetailsList.add(b1);
buttonDetailsList.add(b2);

new MyFrame(buttonDetailsList);


}

اگرمشخصات یک دکمه دیگه ای تعریف کنی و به لیست اضافه کنی بجای دوتا دکمه ۳ تا یا هرچی بیشتر اضافه کنی به همون تعداد می تونی داشته باشی

maryam.eng
چهارشنبه 16 مرداد 1392, 19:24 عصر
در مثال دوم اگر دقت کرده باشی با طول متغیر فرستادم به این قسمت کد نگاه کن


public static void main(String[] s) {


ButtonDetails b1 = new ButtonDetails();
b1.setButtonLink("link one");
b1.setButtonName("one");

ButtonDetails b2 = new ButtonDetails();
b2.setButtonLink("link two");
b2.setButtonName("two");

List<ButtonDetails> buttonDetailsList = new ArrayList<ButtonDetails>();
buttonDetailsList.add(b1);
buttonDetailsList.add(b2);

new MyFrame(buttonDetailsList);


}

اگرمشخصات یک دکمه دیگه ای تعریف کنی و به لیست اضافه کنی بجای دوتا دکمه ۳ تا یا هرچی بیشتر اضافه کنی به همون تعداد می تونی داشته باشی


ببیخشید این قدر سوال می پرسم ولی اینbuttomDetailsجز کتاب خانه جاواست؟؟هر چی سرچ کردم چیزی پیدا نکردم...

javaphantom
چهارشنبه 16 مرداد 1392, 20:20 عصر
ببیخشید این قدر سوال می پرسم ولی اینbuttomDetailsجز کتاب خانه جاواست؟؟هر چی سرچ کردم چیزی پیدا نکردم...

نه اون یک کلاسی هست که بعنوان مدل استفاده کردم که به کمک اون بتونم اطلاعاتی که از بیرون می فرسن رو به این کلاس بچسبونم.

این کلاس کلاسی هست که ماله خودمه