Java Gui开发,先谢谢了

解决方案 »

  1.   


    package javaapplication11;
    public class NewJFrame extends javax.swing.JFrame {//第一个窗体    public NewJFrame() {
            initComponents();
        }    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {        jButton1 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jButton1.setText("弹出面板测试");
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton1MouseClicked(evt);
                }
            });        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(145, 145, 145)
                    .addComponent(jButton1)
                    .addContainerGap(150, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(122, 122, 122)
                    .addComponent(jButton1)
                    .addContainerGap(155, Short.MAX_VALUE))
            );        pack();
        }// </editor-fold>    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//*关键*一个处理鼠标单击按钮事件的方法  通过鼠标单击调用下面一个类的窗体
             new NewJFrame1().setVisible(true);
        }   
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }    // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        // End of variables declaration}
    class NewJFrame1 extends javax.swing.JFrame {//测试弹出的窗体    /** Creates new form NewJFrame1 */
        public NewJFrame1() {
            initComponents();
        }    /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {        jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(55, 55, 55)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(74, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(61, 61, 61)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(68, Short.MAX_VALUE))
            );        pack();
        }// </editor-fold>    /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame1().setVisible(true);
                }
            });
        }    // Variables declaration - do not modify
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        // End of variables declaration}就是一个鼠标单击的事件,很简单的.
    我用NetBeans生成的代码,如果开发gui界面,推荐NetBeans.拖拖拽拽就能写出界面,很方便.
    希望能帮到你!
      

  2.   

       private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//*关键*一个处理鼠标单击按钮事件的方法  通过鼠标单击调用下面一个类的窗体
             new NewJFrame1().setVisible(true);
        }
    主要就是这句了,button的点击事件里写下弹出窗体的代码
    窗体一般继承jframe这个类
      

  3.   

    一楼的大哥,那个代码好像不能运行耶。我们老师叫我们先用记事本编JAVA GUI开发。所以就还没有用NetBeans.我是个新手,谢谢大家了
      

  4.   

    把你的面板放到一个JDialog里,按钮放到JFrame里或者其他你想放的地方,然后在按钮ActionListener里加入代码显示你面板所在的JDialog就行了。这个我熟,还有什么不懂的加我qq301104739。