toolbartest.java

来自「Java the UML Way 书中所有源码」· Java 代码 · 共 56 行

JAVA
56
字号
/*
 * ToolbarTest.java  E.L. 2001-08-23
 *
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class WindowWithToolbar extends JFrame {
  private Container guiContainer;
  private JButton yellowButton;
  private JButton redButton;
  private JButton blueButton;

  public WindowWithToolbar(){
    setTitle("Toolbar Test");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiContainer = getContentPane();

    ButtonListener theListener = new ButtonListener();
    JToolBar toolbar = new JToolBar();
    Icon icon = new ImageIcon("yellow.gif");
    yellowButton = new JButton(icon);
    yellowButton.addActionListener(theListener);
    toolbar.add(yellowButton);

    icon = new ImageIcon("red.gif");
    redButton = new JButton(icon);
    redButton.addActionListener(theListener);
    toolbar.add(redButton);

    icon = new ImageIcon("blue.gif");
    blueButton = new JButton(icon);
    blueButton.addActionListener(theListener);
    toolbar.add(blueButton);

    guiContainer.add(toolbar, BorderLayout.NORTH);
  }

  private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      JButton button = (JButton) event.getSource();
      if (button == yellowButton) guiContainer.setBackground(Color.yellow);
      else if (button == redButton) guiContainer.setBackground(Color.red);
      else guiContainer.setBackground(Color.blue);
    }
  }
}

class ToolbarTest {
  public static void main(String[] args) {
    WindowWithToolbar window = new WindowWithToolbar();
    window.setSize(300, 200); 
    window.setVisible(true);
  }
}

⌨️ 快捷键说明

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