Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. // FanControlPanel.java: Control a fan
  2. import javax.swing.*;
  3. import javax.swing.border.LineBorder;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class FanControl extends JPanel implements ActionListener, AdjustmentListener {
  8. private JButton jbtStart, jbtStop, jbtReverse;
  9. private JScrollBar jscb;
  10. private Fan fan;
  11.  
  12. public static void main(String args[]) {
  13. }
  14.  
  15.  
  16. public FanControl() {
  17. FanControl applet = new FanControl();
  18.  
  19. JFrame frame = new JFrame();
  20. frame.setTitle("Loan Compare");
  21. frame.setSize(600, 200);
  22. frame.setLocationRelativeTo(null); // Center the frame
  23. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. frame.setVisible(true);
  25. frame.add(applet);
  26.  
  27.  
  28. JPanel p1 = new JPanel();
  29. p1.setLayout(new GridLayout(1, 3));
  30. p1.add(jbtStart = new JButton("Start"));
  31. p1.add(jbtStop = new JButton("Stop"));
  32. p1.add(jbtReverse = new JButton("Reverse"));
  33. p1.setBorder(new LineBorder(Color.black, 1));
  34.  
  35. JPanel p2 = new JPanel();
  36. setLayout(new BorderLayout());
  37. add("North", p1);
  38. add("Center", fan = new Fan());
  39. add("South", jscb = new JScrollBar(
  40. JScrollBar.HORIZONTAL, 100, 100, 0, 800));
  41. jscb.setMaximum(1000);
  42.  
  43. jbtStart.addActionListener(this);
  44. jbtStop.addActionListener(this);
  45. jbtReverse.addActionListener(this);
  46. jscb.addAdjustmentListener(this);
  47. }
  48.  
  49. public void actionPerformed(ActionEvent e) {
  50. String arg = e.getActionCommand();
  51. if (e.getSource() instanceof JButton)
  52. if ("Start".equals(arg))
  53. start();
  54. else if ("Stop".equals(arg))
  55. stop();
  56. else if ("Reverse".equals(arg))
  57. reverse();
  58. }
  59.  
  60. // Start the fan
  61. public void start() {
  62. fan.start();
  63. }
  64.  
  65. // Stop the fan
  66. public void stop() {
  67. fan.stop();
  68. }
  69.  
  70. // Reverse the fan
  71. public void reverse() {
  72. fan.reverse();
  73. }
  74.  
  75. public void adjustmentValueChanged(AdjustmentEvent e) {
  76. fan.setSpeed((jscb.getMaximum() - jscb.getValue())/10);
  77. }
  78.  
  79. public Dimension getPreferredSize() {
  80. return new Dimension(200, 200);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement