⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise13_15.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise13_15.java: Use scroll bar to choose colors
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Exercise13_15 extends JFrame implements AdjustmentListener {
  // Declare scrollbars
  private JScrollBar jscbRed, jscbGreen, jscbBlue;

  // Create a label
  private JLabel jlbl = new JLabel("Show Colors", JLabel.CENTER);

  // Declare color component values
  private int redValue, greenValue, blueValue;

  // Main method
  public static void main(String[] args) {
    Exercise13_15 frame = new Exercise13_15();
    frame.setSize(300, 200);
    frame.setTitle("Exercise13_15");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  public Exercise13_15() {
    // Create a panel to hold three scrollbars
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(3,2));

    p.add(new JLabel("Red"));
    p.add(jscbRed = new JScrollBar());
    jscbRed.setOrientation(JScrollBar.HORIZONTAL);
    jscbRed.setMaximum(255);

    p.add(new JLabel("Green"));
    p.add(jscbGreen = new JScrollBar());
    jscbGreen.setOrientation(JScrollBar.HORIZONTAL);
    jscbGreen.setMaximum(255);

    p.add(new JLabel("Blue"));
    p.add(jscbBlue = new JScrollBar());
    jscbBlue.setOrientation(JScrollBar.HORIZONTAL);
    jscbBlue.setMaximum(255);

    // Set border title
    p.setBorder(new TitledBorder("Choose colors"));

    JPanel t = (JPanel)getContentPane();

    getContentPane().add(jlbl, BorderLayout.CENTER);
    getContentPane().add(p, BorderLayout.SOUTH);

    // Register listener for the scroll bars
    jscbRed.addAdjustmentListener(this);
    jscbGreen.addAdjustmentListener(this);
    jscbBlue.addAdjustmentListener(this);
  }

  public void adjustmentValueChanged(AdjustmentEvent e) {
    if (e.getSource() == jscbRed)
      redValue = jscbRed.getValue();
    else if (e.getSource() == jscbGreen)
      greenValue = jscbGreen.getValue();
    else
      blueValue = jscbBlue.getValue();

    Color color = new Color(redValue, greenValue, blueValue);
    jlbl.setForeground(color);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -