各位大虾:
    急需要实现点击Java Applet上的按扭,弹出对话框,具体代码请各位大虾指点一二!
    不胜感激!

解决方案 »

  1.   

    class MyApplet extends JApplet implements ActionListener{
    private JButton button
    public void init(){
    ....
    button = new JButton("Test");
    button.addActionListener(this);
    getContentPane().add(button, BorderLayout.SOUTH);
    }public void actionPerformed(ActionEvent e){
    if(e.getSource() == button){
    JDialog dialog = new JDialog();
    dialog.setVisible(true);
    }
    }
    }
      

  2.   

    \
    如果把
    public void actionPerformed(ActionEvent e){ 
    if(e.getSource() == button){ 
    JDialog dialog = new JDialog(); 
    dialog.setVisible(true); 

    中间事件鼠标响应的部分,改成弹出一个Frame,不是对话框,而是一个新的界面是不是也是可以的啊?就是新new一个对象,这个对象就是一个Frame,是否可行呢?
      

  3.   

    JAVA APPLET部分的代码是
    public class LXICMultiSwitch extends Applet {
      public Frame1 SCPIFrame;
      private boolean  isStandalone = false;
      BorderLayout borderLayout1 = new BorderLayout();
      Panel panel1 = new Panel();
      Panel panel2 = new Panel();
      Panel panel3 = new Panel();
      GridBagLayout gridBagLayout1 = new GridBagLayout();
      GridBagLayout gridBagLayout2 = new GridBagLayout();
      JList jList1 = new JList();
      JScrollPane jScrollPane1=new JScrollPane(jList1);
      JLabel jLabel1 = new JLabel();
      JButton jButton1 = new JButton();
      //Get a parameter value
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
          (getParameter(key) != null ? getParameter(key) : def);
      }  //Construct the applet
      public LXICMultiSwitch() {
      }
      //Initialize the applet
      public void init() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception {
        this.setVisible(true);
        this.setLayout(borderLayout1);
        panel1.setVisible(true);
        panel2.setLayout(gridBagLayout1);
        panel3.setLayout(gridBagLayout2);
        jLabel1.setText("jLabel1");
        jButton1.setText("jButton1");
        jButton1.addActionListener(new LXICMultiSwitch_jButton1_actionAdapter(this));
        this.add(panel1, BorderLayout.CENTER);
        this.add(panel2,  BorderLayout.EAST);
        panel2.add(jScrollPane1,                                      new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 3, 3, 4), 115, 37));
        panel2.add(jLabel1,             new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 27, 8, 18), 79, -2));
        panel2.add(jButton1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        this.add(panel3,  BorderLayout.SOUTH);
        this.add(jList1, BorderLayout.NORTH);
      }
      //Get Applet information
      public String getAppletInfo() {
        return "Applet Information";
      }
      //Get parameter info
      public String[][] getParameterInfo() {
        return null;
      }  void jButton1_actionPerformed(ActionEvent e) {
        Frame1 SCPIFrame=new Frame1();
      }
    }class LXICMultiSwitch_jButton1_actionAdapter implements java.awt.event.ActionListener {
      LXICMultiSwitch adaptee;  LXICMultiSwitch_jButton1_actionAdapter(LXICMultiSwitch adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
      }
    }JFrame 部分的代码是
    public class Frame1 extends JFrame {
      JPanel contentPane;
      GridBagLayout gridBagLayout1 = new GridBagLayout();
      JButton jButton1 = new JButton();
      JButton jButton2 = new JButton();
      JButton jButton3 = new JButton();
      JButton jButton4 = new JButton();
      JButton jButton5 = new JButton();  //Construct the frame
      public Frame1() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        System.out.println("###########Frame1#########################");
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception  {
        System.out.println("#############Jbinit#######################");
        contentPane = (JPanel) this.getContentPane();
        jButton1.setText("jButton1");
        contentPane.setLayout(gridBagLayout1);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        jButton2.setText("jButton2");
        jButton3.setText("jButton3");
        jButton4.setText("jButton4");
        jButton5.setText("jButton5");
        contentPane.add(jButton1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        contentPane.add(jButton2,  new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        contentPane.add(jButton3,  new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        contentPane.add(jButton4,  new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        contentPane.add(jButton5,  new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
      }
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
    }
    我是用浏览器打开的JAVA APPLET,点击按扭就是没有弹出JFrame界面
    不知道什么原因啊?
      

  4.   

      void jButton1_actionPerformed(ActionEvent e) { 
        Frame1 SCPIFrame=new Frame1(); 
        SCPIFrame.setVisible(true);
      }晕,不显示怎么能出来啊。
      

  5.   

    已经可以弹出JFrame界面,现在需要设置界面的大小固定,并且能够在中间弹出,现在的位置在左上角,不美观。
    另外,我觉得使用gridBagLayout的布局很复杂,不是很好控制,我现在需要显示一些输入输出框,不知道使用什么样的布局方式?
    不胜感谢!!
      

  6.   

    //指定居中显示
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension ds = tk.getScreenSize();
    int x = (ds.width - frame.getWidth()) / 2;
    int y = (ds.height - frame.getHeight()) / 2;
    this.setLocation(x, y);//左上角位置GridBagLayout很好用的,实在不行就用FlowLayout也可以,自己看看吧。
    PS:问题解决就结贴,有其他问题请另开
      

  7.   

    居中不用这么复杂了。你找找有个方法好像是setLocate...(Componnet c){}的可以直接居中显示组件