- // JFrameTest.java
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- class JFrameTest extends JFrame implements ActionListener {
- JFrameTest() {
- getContentPane().setLayout(new FlowLayout());
- // Button
- JButton b1 = new JButton("OK");
- b1.addActionListener(this);
- getContentPane().add(b1);
- // ComboBox
- JComboBox cb = new JComboBox();
- cb.addItem("ComboA");
- cb.addItem("ComboB");
- cb.addItem("ComboC");
- cb.addActionListener(this);
- getContentPane().add(cb);
- // ScrollBar
- JScrollBar sb = new JScrollBar(JScrollBar.HORIZONTAL, 30, 5, 0, 105);
- sb.setPreferredSize(new Dimension(150, 17));
- sb.addAdjustmentListener(this);
- getContentPane().add(sb);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setTitle("JFrameTest");
- setSize(200, 120);
- setVisible(true);
- }
- public static void main(String[] args) {
- new JFrameTest();
- }
- public void actionPerformed(ActionEvent e) {
- JComboBox cb = (JComboBox)e.getSource();
- System.out.println(cb.getSelectedItem());
- }
- public void adjustmentValueChanged(AdjustmentEvent e) {
- JScrollBar sb = (JScrollBar)e.getSource();
- System.out.println(sb.getValue());
- }
- }