02-05-2015, 13:17
|
|
|
חבר מתאריך: 02.05.15
הודעות: 1
|
|
עזרה בנושא Java BorderLayout JButton event
שלום לכולם!
אני סטודנטית טרייה לחלוטין ללימוד שפת תכנות ג'אווה ויש לי פרוייקט הגשה שבו
אני צריכה ליצור חלון על 4 כפתורים BorderLayout
ימין שמאל למעלה למטה
בכל לחיצת כפתור החלון יזוז 20 פיקסל לכיוון המתאים נגיד כפתור שמאל יזיז את החלון 20 פיקסל שמאלה.
עד לנקודה זו ייצרתי את החלון ואת הכפתורים אבל אני לא יודעת איך להפעיל את הכפתורים
in Event Listener
שחייב להיות ב
Inner Class
חובה שזה יהיה עם שיטה שבודקת את גודל הסדקטופ ושלא יהיה אפשר שהחלונית תצא מתווך הדסקטופ
הקוד מצורף בקובץ:
אני אעריך כל עזרה מאוד! https://2015-uploaded.fresh.co.il/2...02/15651593.txt
קוד:
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class WindowBorder extends JFrame {
JFrame frame = new JFrame ("Move Window");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private static final long serialVersionUID = 1L;
public WindowBorder (String titel){
super (titel);
//create the buttons and the layout and add the buttons
JButton right = new JButton ("Right");
JButton left = new JButton ("Left");
JButton up = new JButton ("Up");
JButton down = new JButton ("Down");
//JButton center = new JButton ("Default"); hide the middle button
frame.setLayout (new BorderLayout (75,75));
frame.add(BorderLayout.EAST,right);
frame.add(BorderLayout.WEST,left);
frame.add(BorderLayout.NORTH,up);
frame.add(BorderLayout.SOUTH,down);
//frame.add(BorderLayout.CENTER,default); hide the middle button
//I must create the inner class with the constructors for the task project for school
class WindowBorderInner implements ActionListener {
@Override
public void actionPerformed (ActionEvent e){
if(e.getActionCommand().equals("right"))
frame.setLocation ((screenSize.width - getSize().width) + 20, screenSize.height - getSize().height / 0);
right.addActionListener(new WindowBorderInner());
if (e.getActionCommand().equals("left"))
frame.setLocation ((screenSize.width - getSize().width) - 20, screenSize.height - getSize().height / 0);
left.addActionListener(new WindowBorderInner());
if(e.getActionCommand().equals("up"))
frame.setLocation ((screenSize.width - getSize().width) / 0, screenSize.height - getSize().height +20);
up.addActionListener(new WindowBorderInner());
if (e.getActionCommand().equals("down"))
frame.setLocation ((screenSize.width - getSize().width) - 20, screenSize.height - getSize().height - 20);
down.addActionListener(new WindowBorderInner());
}
}
//configuration the size and the location of the Border layout
frame.setSize (400,400);
//Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocationRelativeTo(null);
//frame.setLocation ((screenSize.width - getSize().width)/2, screenSize.height - getSize().height / 2);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
}
public static void main(String [] arg){ //the test method
new WindowBorder("Move Window");
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
System.out.println("Screen width = " + d.width);
System.out.println("Screen height = " + d.height);
}
}
|