按下按钮运行课程?

时间:2015-12-07 21:31:25

标签: java swing class object

我有一个Jframe,上面有一个按钮(我刚用netbeans中的设计工具制作了这个)。当我按下按钮时,我想在文件夹中运行另一个类。该类显示一个带边框的透明JWindow。每次按下按钮,我都希望出现另一个JWindow。这些部件是单独工作的,但我正在努力将这两个部分结合在一起。

以下是带有按钮的JFrame的代码:

public class Test4 extends javax.swing.JFrame {

public Test4() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    newbox = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    newbox.setText("New Box");
    newbox.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            newboxActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(79, Short.MAX_VALUE)
            .addComponent(newbox)
            .addGap(75, 75, 75))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(32, 32, 32)
            .addComponent(newbox)
            .addContainerGap(36, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void newboxActionPerformed(java.awt.event.ActionEvent evt) {                                       
    new Test4box();
}                                      


public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {

                    Test4 x = new Test4();
                    x.setVisible(true);

        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton newbox;
// End of variables declaration                   

}

以下是该类的代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.border.LineBorder;


public class Test4box extends JWindow {

    JPanel p=new JPanel();    
    public Test4box()            
    {

        this.setAlwaysOnTop(true);  
        this.setBackground(new Color(0, 0, 0, 0));
        setContentPane(p);   
        setSize(200,200);
        p.setBorder(new LineBorder(Color.blue));
        p.setLayout(new FlowLayout());
        p.setBackground(new Color(0, 0, 0, 0));
        p.addMouseListener(adapter);
        p.addMouseMotionListener(adapter);
    }

    MouseAdapter adapter= new MouseAdapter()
    {

      int x,y; 
      public void mousePressed(MouseEvent e)
      {   
          if(e.getButton()==MouseEvent.BUTTON1)
        {            
            x = e.getX();
            y = e.getY();            
        }          
      }

      public void mouseDragged(MouseEvent e)
      {
          if( (e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0)
          {
            setLocation(e.getXOnScreen()-x,e.getYOnScreen()-y);
          }
      }             

    };

}

0 个答案:

没有答案