请问:
怎么实现一个JFrame,上面两个JButton,点击一个JButton可以新建一个word文档,点击另一个JButton弹出一文件选择框,可以自由选择文件并打开进行修改编辑(以文件本身类型打开)?
最好能以代码示例!
谢谢!

解决方案 »

  1.   

    button的代码我有,至于点击的效果,你自己实现去吧。import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
       
    public class JButtonExample extends JFrame implements ActionListener
    {
        public static void main(String[] argv)
        {
            JButtonExample mainApp = new JButtonExample();
        }
        
        public JButtonExample()
        {
            super("JButton Example");
            setBounds(0, 0, 300, 300);
            getContentPane().setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
                   
            // Create the label...
            label = new JLabel("No button pressed");
            label.setLocation(10, 10);
            label.setSize(label.getPreferredSize());
            
            // Create the three buttons...
            button1 = new JButton("Button 1");
            button1.setLocation(10, 40);
            button1.setSize(button1.getPreferredSize());
            
            button2 = new JButton("Button 2");
            button2.setBounds(10, 80, 270, 40);
     
            button3 = new JButton("Button 3");
            button3.setBounds(60, 140, 160, 100);
            button3.setBackground(new Color(255, 0, 0));
            button3.setForeground(new Color(0, 255, 0));
            
            // Add the action listeners
            button1.addActionListener(this);
            button2.addActionListener(this);
            button3.addActionListener(this);
                   
            // Add the objects to the content pane...
            getContentPane().add(label);
            getContentPane().add(button1);
            getContentPane().add(button2);
            getContentPane().add(button3);
            
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == button1)
            {
                label.setText("Button 1 was pressed last");
                label.setSize(label.getPreferredSize());
            }
            else if(e.getSource() == button2)
            {
                label.setText("Button 2 was pressed last");
                label.setSize(label.getPreferredSize());
            }
            else if(e.getSource() == button3)
            {
                label.setText("Button 3 was pressed last");
                label.setSize(label.getPreferredSize());
            }
        }
        
        JLabel label;
        JButton button1;
        JButton button2;
        JButton button3;
    }