我想在界面里添加一个swing的工具栏,谁能给我说说如何实现?

解决方案 »

  1.   

    在JToolBar上面添加JButton就可以啊
      

  2.   

    帮你找了点资料,自己进去看看吧.希望能对你有所帮助
    http://www.kecourser.com/free/java/java_full.php?rid=33
      

  3.   

    swing有一点比较重要,就是先new组件,再要自己new的组件加到容器里
    给你一个实例package swing;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;public class TableFrame extends JFrame {
    JTable jTable = new JTable(); public TableFrame(String title) {
    super(title);
    // create tool bar
    JToolBar toolBar = new JToolBar("test");
    toolBar.setVisible(true); JButton jbtn1 = new JButton();
    jbtn1.setText("open");
    jbtn1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {
    System.out.println("you hava clicked "
    + ((JButton) e.getSource()).getText());
    }
    });
    JButton jbtn2 = new JButton();
    jbtn2.setText("close");
    jbtn2.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {
    System.out.println("you hava clicked "
    + ((JButton) e.getSource()).getText());
    }
    }); JButton jbtn3 = new JButton();
    jbtn3.setText("save");
    jbtn3.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {
    System.out.println("you hava clicked "
    + ((JButton) e.getSource()).getText());
    }
    }); JButton jbtn4 = new JButton();
    jbtn4.setText("delete");
    jbtn4.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {
    System.out.println("you hava clicked "
    + ((JButton) e.getSource()).getText());
    }
    }); JButton jbtn5 = new JButton();
    jbtn5.setText("export");
    jbtn5.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {
    System.out.println("you hava clicked "
    + ((JButton) e.getSource()).getText());
    }
    }); // add action button on tool bar
    toolBar.add(jbtn1);
    toolBar.add(jbtn2);
    toolBar.add(jbtn3);
    toolBar.add(jbtn4);
    toolBar.add(jbtn5); //add tool bar
    this.getContentPane().add(toolBar, BorderLayout.NORTH);
    } public static void main(String args[]) {
    TableFrame t = new TableFrame("tool bar");
    t.setSize(500, 500);
    t.setBackground(new Color(200, 219, 250));
    t.setVisible(true);
    }
    }
      

  4.   

    Thanks!希望更多的人给点意见,分还可以加的!!!
      

  5.   

    http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html/zh_CN/api/javax/swing/class-use/JToolBar.html
      

  6.   

    Swing中JToolBar的使用
    在JAVA的SWING 编程中,经常要用到工具条。Swing中JToolBar类提供了工具条的属性和方法,便于我们使用。下面是偶写的JToolbar,用于实现一个简单的TextEdit功能。package com.yuyun.Demo;import javax.swing.*;import java.awt.*;
    import java.awt.event.*;
    import java.io.*;public class JToolBarDemo extends JApplet implements SwingConstants { JPanel jpane1 = new JPanel();
     JButton jbuttonOpen = new JButton("Open");
     JButton jbuttonSave = new JButton("Save");
     JButton jbuttonExit = new JButton("Exit");
     JTextArea jTextArea = new JTextArea("");
     File file = null; public void init() {
      Container contentPane = getContentPane();
      contentPane.setLayout(new BorderLayout());
      JToolBar jToolBar = new JToolBar("Still draggable");
      jbuttonOpen.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e)  {
        // TODO Auto-generated method stub
        JFileChooser jc = new JFileChooser(); 
        jc.setSelectedFile(file); 
        int iResult = jc.showOpenDialog(null);
           file = jc.getSelectedFile();
        BufferedReader in=null;
        if (file != null) {
         int readbyte = 0;
         
         try {
          in = new BufferedReader(new FileReader(file.getAbsolutePath().toString()));
         } catch (FileNotFoundException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
         }
         try {
          while((readbyte=in.read())!=-1)
          {
           jTextArea.append(String.valueOf((char) readbyte));
           
          }
          in.close();
         } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
         }
         
         showStatus("文件读取成功!");
         jc.show(false);
        }   }  });
      jToolBar.add(jbuttonOpen);
      
      jbuttonSave.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
         JFileChooser jc=new JFileChooser();
         jc.setSelectedFile(file); 
         int iResult=jc.showSaveDialog(null);
            file=jc.getSelectedFile();
          
         if (file!=null)
         {
          if (!file.exists())
           file=new File(jc.getSelectedFile().getAbsolutePath());
         
          FileOutputStream fileOutStream = null;
         try {
          fileOutStream = new FileOutputStream(  file );
          fileOutStream.write(jTextArea.getText().getBytes());
          
          fileOutStream.close();
         } catch ( Exception e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
          
         }
        
         }
         
         
         
       }  }); 
      jToolBar.add(jbuttonSave); 
      
      jbuttonExit.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e)  {
        // TODO Auto-generated method stub
         
        System.exit(0);
       }  });
      jToolBar.add(jbuttonExit);
      jToolBar.setPreferredSize(new Dimension(200, 40));
      contentPane.add(jToolBar, "North");  jTextArea.setRows(10);
      jTextArea.setColumns(20);
      jTextArea.setAutoscrolls(true);
      contentPane.add(new JScrollPane(jTextArea), "Center");
      show();
     }
    }