Formato
Plain text
Post date
2014-06-26 14:57
Publication Period
Unlimited
  1. // JFrameTest.java
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. class JFrameTest extends JFrame implements ActionListener {
  6. JFrameTest() {
  7. getContentPane().setLayout(new FlowLayout());
  8. // Button
  9. JButton b1 = new JButton("OK");
  10. b1.addActionListener(this);
  11. getContentPane().add(b1);
  12. // ComboBox
  13. JComboBox cb = new JComboBox();
  14. cb.addItem("ComboA");
  15. cb.addItem("ComboB");
  16. cb.addItem("ComboC");
  17. cb.addActionListener(this);
  18. getContentPane().add(cb);
  19. // ScrollBar
  20. JScrollBar sb = new JScrollBar(JScrollBar.HORIZONTAL, 30, 5, 0, 105);
  21. sb.setPreferredSize(new Dimension(150, 17));
  22. sb.addAdjustmentListener(this);
  23. getContentPane().add(sb);
  24. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25. setTitle("JFrameTest");
  26. setSize(200, 120);
  27. setVisible(true);
  28. }
  29. public static void main(String[] args) {
  30. new JFrameTest();
  31. }
  32. public void actionPerformed(ActionEvent e) {
  33. JComboBox cb = (JComboBox)e.getSource();
  34. System.out.println(cb.getSelectedItem());
  35. }
  36. public void adjustmentValueChanged(AdjustmentEvent e) {
  37. JScrollBar sb = (JScrollBar)e.getSource();
  38. System.out.println(sb.getValue());
  39. }
  40. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text