📄 toolbardemo.java
字号:
/* * ToolBarDemo.java relies on having the Java Look and Feel Graphics * Repository (jlfgr-1_0.jar) in the class path. You can download it * from http://developer.java.sun.com/developer/techDocs/hi/repository/. * When running ToolBarDemo from the command line (rather than Java Web * Start) put jlfgr-1_0.jar in the class path using one of the following * commands (assuming jlfgr-1_0.jar is in a subdirectory named jars): * * java -cp .;jars/jlfgr-1_0.jar ToolBarDemo [Microsoft Windows] * java -cp .:jars/jlfgr-1_0.jar ToolBarDemo [UNIX] * * If that doesn't work, try putting quotation marks around the path: * * java -cp ".;jars/jlfgr-1_0.jar" ToolBarDemo [UNIX shell on Win32] */import javax.swing.JToolBar;import javax.swing.JButton;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JTextArea;import javax.swing.JScrollPane;import javax.swing.JPanel;import java.net.URL;import java.awt.*;import java.awt.event.*;public class ToolBarDemo extends JPanel implements ActionListener { protected JTextArea textArea; protected String newline = "\n"; static final private String PREVIOUS = "previous"; static final private String UP = "up"; static final private String NEXT = "next"; public ToolBarDemo() { //Create the toolbar. JToolBar toolBar = new JToolBar(); addButtons(toolBar); //Create the text area used for output. textArea = new JTextArea(5, 30); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea); //Lay out the content pane. setLayout(new BorderLayout()); setPreferredSize(new Dimension(450, 150)); add(toolBar, BorderLayout.PAGE_START); add(scrollPane, BorderLayout.CENTER); } protected void addButtons(JToolBar toolBar) { JButton button = null; //first button button = makeNavigationButton("Back24", PREVIOUS, "Back to previous something-or-other", "Previous"); toolBar.add(button); //second button button = makeNavigationButton("Up24", UP, "Up to something-or-other", "Up"); toolBar.add(button); //third button button = makeNavigationButton("Forward24", NEXT, "Forward to something-or-other", "Next"); toolBar.add(button); } protected JButton makeNavigationButton(String imageName, String actionCommand, String toolTipText, String altText) { //Look for the image. String imgLocation = "toolbarButtonGraphics/navigation/" + imageName + ".gif"; URL imageURL = ToolBarDemo.class.getResource(imgLocation); //Create and initialize the button. JButton button = new JButton(); button.setActionCommand(actionCommand); button.setToolTipText(toolTipText); button.addActionListener(this); if (imageURL != null) { //image found button.setIcon(new ImageIcon(imageURL)); } else { //no image found button.setText(altText); System.err.println("Resource not found: " + imgLocation); } return button; } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); String description = null; // Handle each button. if (PREVIOUS.equals(cmd)) { //first button clicked description = "taken you to the previous <something>."; } else if (UP.equals(cmd)) { // second button clicked description = "taken you up one level to <something>."; } else if (NEXT.equals(cmd)) { // third button clicked description = "taken you to the next <something>."; } displayResult("If this were a real app, it would have " + description); } protected void displayResult(String actionDescription) { textArea.append(actionDescription + newline); } public static void main(String[] args) { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("ToolBarDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new ToolBarDemo()); //Display the window. frame.pack(); frame.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -