Java7_code

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

63 εμφανίσεις


1

Приложение 1. Реализиране на “слушател” чрез анонимен клас
, дефиниран в метод.


import java.awt.event.*;

import javax.swing.*;


public class SimpleFrame extends JFrame{




private JButton button=new JButton("Press me!");


private JLabel label=new JLabel("G
o on. Press the button");


private JPanel panel=new JPanel();




public SimpleFrame(){//constructor




button.addActionListener(new ActionListener(){//implement the interface



public void actionPerformed(ActionEvent e){



label.setText("Olele boli");}



}
);




panel.add(button);



panel.add(label);


getContentPane().add(panel);


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


pack();

}

}


Приложение 2. Смесител на цветове. Реализиране на “слушател” по два начина:



ч
рез
вграден
клас и чрез анонимен клас


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;


public class ColorMixer extends JFrame{//c
reates window


JButton exit = new JButton("exit");//exit button


JButton reset = new JButton("reset");//reset button




JPanel background = new JPanel(new BorderLayout());//layout manager in
constructor


JPanel colorBox =new JPanel();//create panel object


JPanel bottomPanel = new JPanel(new FlowLayout());//panel object


Color theColor = new Color(137,45,167);//declare and initialize color


//add sliders


JSlider redSlider = new JSlider(JSlider.HORIZONTAL,0,255,135);


JSlider greenSlider = new JSlider(JSli
der.HORIZONTAL,0,255,35);


JSlider blueSlider = new JSlider(JSlider.HORIZONTAL,0,255,165);


//specify 3 raws


2


JPanel slidePanel = new JPanel(new GridLayout(3,0));


//add 3 labels


JLabel redLabel = new JLabel("Red");


JLabel greenLabel = new JLabel("Green"
);


JLabel blueLabel = new JLabel("Blue");


//specify 3 raws


JPanel labelPanel = new JPanel(new GridLayout(3,0));


JPanel topPanel = new JPanel(new BorderLayout());




public ColorMixer(){//the constructor

implement slider listeners



redSlider.addChangeL
istener(new SliderListener());



greenSlider.addChangeListener(new SliderListener());



blueSlider.addChangeListener(new SliderListener());









//add sliders



slidePanel.add(redSlider);



slidePanel.add(greenSlider);



slidePanel.add(blueSlider);



//
add slidePanel to topPanel



topPanel.add(slidePanel, BorderLayout.CENTER);



//add labels



labelPanel.add(redLabel);



labelPanel.add(greenLabel);



labelPanel.add(blueLabel);



//add labelPanel to topPanel



topPanel.add(labelPanel, BorderLayout.WEST);



//add topPanel to background



background.add(topPanel, BorderLayout.NORTH);








colorBox.setBackground(theColor);


colorBox.setPreferredSize(new Dimension(180,180));


bottomPanel.add(exit);//add button
-
component


bottomPanel.add(reset);//add button
-
c
omponent


background.add(colorBox, B
orderLayout.CENTER);//add pane
l
component


background.add(bottomPanel, BorderLayout.SOUTH);//add panel
-
component



//implement button ActionListener by anonimous class



exit.addActionListener(new ActionListener()//anon
imous class



{public void actionPerformed(ActionEvent e){




System.exit(0);}});






reset.addActionListener(new ActionListener()//anonimous class



{public void actionPerformed(ActionEvent e){




redSlider.setValue(0);




greenSlider.setValue(0);


3




blu
eSlider.setValue(0);}});






getContentPane().add(background);



setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



pack();


}

//class implements slider listeners


class SliderListener implements ChangeListener{



public void stateChanged(ChangeEvent e){




Color newcol=new
Color(redSlider.getValue(),greenSlider.getValue(),




blueSlider.getValue());




colorBox.setBackground(newcol);



}


}


public static void main(String[] args) {



ColorMixer appl = new ColorMixer();



appl.setVisible(true);


}

}