JToolBar在运行时是可以用拖动使其停靠在左(右)边框的,请问怎样在设计时就使它停靠在Frame的左(右)边框?不胜感激

解决方案 »

  1.   

    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.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.SwingConstants;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() {
            super(new BorderLayout());        // 创建工具条 
            JToolBar toolBar = new JToolBar("Still draggable");
            addButtons(toolBar);

    // 设定显示
    toolBar.setOrientation(SwingConstants.VERTICAL);

            textArea = new JTextArea(5, 30);
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea);        //Lay out the main panel.
            setPreferredSize(new Dimension(450, 130));
            
            // 显示在左边
            add(toolBar, BorderLayout.LINE_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, altText));
            } 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);
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }    /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //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);        //Create and set up the content pane.
            ToolBarDemo newContentPane = new ToolBarDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);        //Display the window.
            frame.pack();
            frame.setVisible(true);
        }    public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }