Guest User

Untitled

a guest
Oct 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. JavaList
  2.  
  3. package JavaList;
  4. import java.awt.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7.  
  8. public class JavaList extends JFrame{
  9. private JList list;
  10. private static String [] colornames = {"Black","Blue","Red","White"};
  11. private static Color[] color = {Color.BLACK,Color.BLUE,Color.RED,Color.WHITE};
  12. public JavaList(){
  13. super("List");
  14. setLayout(new FlowLayout());
  15. list = new JList(colornames);
  16. list.setVisibleRowCount(4);
  17. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  18. add(new JScrollPane(list));
  19. list.addListSelectionListener(
  20. new ListSelectionListener(){
  21. public void valueChanged(ListSelectionEvent event){
  22. getContentPane().setBackground(color[list.getSelectedIndex()]);
  23. }
  24. }
  25. );
  26. }
  27.  
  28. }
  29. ============================================================================================
  30. JavaList TEst
  31.  
  32.  
  33. package JavaList;
  34. import javax.swing.JFrame;
  35.  
  36. public class JavaListTest {
  37. public static void main(String[] args){
  38. JavaList myJavaList = new JavaList();
  39. myJavaList.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. myJavaList.setSize(300,120);
  41. myJavaList.setVisible(true);
  42. }
  43.  
  44. }
  45.  
  46. ============================================================================================
  47. JavaMouse Event
  48.  
  49. package JavaMouseEvent;
  50. import java.awt.*;
  51. import java.awt.event.*;
  52. import javax.swing.*;
  53.  
  54. public class JavaMouseEvent extends JFrame{
  55. private JPanel mousePanel;
  56. private JLabel statusbar;
  57. public JavaMouseEvent(){
  58. super("Java Mouse");
  59. mousePanel = new JPanel();
  60. mousePanel.setBackground(Color.WHITE);
  61. add(mousePanel,BorderLayout.CENTER);
  62. statusbar = new JLabel("default");
  63. add(statusbar,BorderLayout.SOUTH);
  64. HandlerClass handler = new HandlerClass();
  65. mousePanel.addMouseListener(handler);
  66. mousePanel.addMouseMotionListener(handler);
  67. }
  68. private class HandlerClass implements MouseListener, MouseMotionListener{
  69. public void mouseClicked(MouseEvent event){
  70. statusbar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
  71. }
  72. public void mousePressed(MouseEvent event){
  73. statusbar.setText("You pressed mouse down");
  74.  
  75. }
  76. public void mouseReleased(MouseEvent event){
  77. statusbar.setText("You released the button");
  78.  
  79. }
  80. public void mouseEntered(MouseEvent event){
  81. statusbar.setText("You entered the area");
  82. mousePanel.setBackground(Color.RED);
  83. }
  84. public void mouseExited(MouseEvent event){
  85. statusbar.setText("You left tht area");
  86. mousePanel.setBackground(Color.WHITE);
  87. }
  88. //mouse motion event
  89. public void mouseDragged(MouseEvent event){
  90. statusbar.setText("You are dragging the mouse");
  91.  
  92. }
  93. public void mouseMoved(MouseEvent event){
  94. statusbar.setText("You move the mouse");
  95.  
  96. }
  97. }
  98.  
  99. }
  100. ========================================================================================
  101. JavaMouseEventTest
  102.  
  103. package JavaMouseEvent;
  104. import javax.swing.JFrame;
  105.  
  106.  
  107.  
  108. public class JavaMouseEventTest {
  109. public static void main(String[] args){
  110. JavaMouseEvent myJavaMouse = new JavaMouseEvent();
  111. myJavaMouse.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112. myJavaMouse.setSize(300,120);
  113. myJavaMouse.setVisible(true);
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment