/*
   * Gets the user choice for font, style and size and redraws the text
   *     accordingly.
   */
  public void setSampleFont() {
    // Get the font name from the JComboBox
    fontName = (String) facenameCombo.getSelectedItem();

    sampleField.setText(textField.getText());

    // Get the font style from the JCheckBoxes
    fontStyle = 0;
    if (italicCheckBox.isSelected()) fontStyle += Font.ITALIC;
    if (boldCheckBox.isSelected()) fontStyle += Font.BOLD;

    // Get the font size
    fontSize = 0;

    fontSize = Integer.parseInt((String) sizeCombo.getSelectedItem());

    // THE FOLLOWING IS NO LONGER NEEDED
    //            if(smallButton.isSelected())
    //                  fontSize=SMALL;
    //            else if(mediumButton.isSelected())
    //                  fontSize=MEDIUM;
    //            else if(largeButton.isSelected())
    //                  fontSize=LARGE;

    // Set the font of the text field
    sampleField.setFont(new Font(fontName, fontStyle, fontSize));
    sampleField.setForeground(fontColor);
    sampleField.repaint();

    pack();
  } // end setSampleFont method
  /*
   * The following method resets the window to its original status.
   */
  public void init() {
    sampleField.setText(new String("Big Java"));
    textField.setText(new String("Big Java"));

    facenameCombo.setSelectedIndex(0);
    sizeCombo.setSelectedIndex(2);

    italicCheckBox.setSelected(false);
    boldCheckBox.setSelected(false);

    //            largeButton.setSelected(true);

    fontColor = Color.BLACK;

    setSampleFont();
  } // end init method
예제 #3
0
  public ColorChooserDemo() {
    super(new BorderLayout());

    // 设置一个标签,做广告的。也用来显示用户选择的颜色。
    banner = new JLabel("欢迎使用颜色选择器!", JLabel.CENTER);
    banner.setForeground(Color.yellow);
    banner.setBackground(Color.blue);
    banner.setOpaque(true);
    banner.setFont(new Font("SansSerif", Font.BOLD, 24));
    banner.setPreferredSize(new Dimension(100, 65));

    JPanel bannerPanel = new JPanel(new BorderLayout());
    bannerPanel.add(banner, BorderLayout.CENTER);
    bannerPanel.setBorder(BorderFactory.createTitledBorder("广告"));

    // 设置选择颜色选择器
    tcc = new JColorChooser(banner.getForeground()); // 设置初始颜色
    tcc.getSelectionModel().addChangeListener(this); // 给所有模式添加监听
    tcc.setBorder(BorderFactory.createTitledBorder("选择颜色"));

    add(bannerPanel, BorderLayout.CENTER);
    add(tcc, BorderLayout.PAGE_END);
  }
예제 #4
0
 public void loadImage(String filename) {
   theImage.setIcon(new MyImageIcon(filename));
 }
예제 #5
0
 /**
  * <br>
  * 方法说明:事件监听。用户选择颜色触发 <br>
  * 输入参数:ChangeEvent e 用户选择事件 <br>
  * 返回类型:
  */
 public void stateChanged(ChangeEvent e) {
   Color newColor = tcc.getColor(); // 获取用户选择的颜色
   banner.setForeground(newColor);
 }