⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 用java写的简单的画图程序
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * Main.java * * Created on 2009-5-5, 8:31:41 */package pen;import java.awt.Color;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JColorChooser;/** * * @author Administrator */public class Main extends javax.swing.JFrame implements Runnable {    private Color c = Color.BLACK;    private int t = 1;    public Color getC() {        return c;    }    public int getT() {        return t;    }    /** Creates new form Main */    public Main() {        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">//GEN-BEGIN:initComponents    private void initComponents() {        jButton1 = new javax.swing.JButton();        jLabel1 = new javax.swing.JLabel();        jComboBox1 = new javax.swing.JComboBox();        jSeparator1 = new javax.swing.JSeparator();        drawPen1 = new DrawPen(this);        jLabel2 = new javax.swing.JLabel();        jButton2 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jButton1.setText("选择颜色");        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton1ActionPerformed(evt);            }        });        jLabel1.setText("选择线条粗细");        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));        jComboBox1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jComboBox1ActionPerformed(evt);            }        });        drawPen1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));        javax.swing.GroupLayout drawPen1Layout = new javax.swing.GroupLayout(drawPen1);        drawPen1.setLayout(drawPen1Layout);        drawPen1Layout.setHorizontalGroup(            drawPen1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 378, Short.MAX_VALUE)        );        drawPen1Layout.setVerticalGroup(            drawPen1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 189, Short.MAX_VALUE)        );        jLabel2.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N        jLabel2.setText("jLabel2");        jButton2.setText("开始");        jButton2.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton2ActionPerformed(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()                .addContainerGap()                .addComponent(jButton1)                .addGap(18, 18, 18)                .addComponent(jLabel1)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)                .addGap(18, 18, 18)                .addComponent(jButton2)                .addContainerGap(56, Short.MAX_VALUE))            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(drawPen1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                .addContainerGap())            .addGroup(layout.createSequentialGroup()                .addGap(104, 104, 104)                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)                .addContainerGap(171, Short.MAX_VALUE))        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jButton1)                    .addComponent(jLabel1)                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addComponent(jButton2))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jLabel2)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)                .addComponent(drawPen1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed        c = JColorChooser.showDialog(this, "Color", Color.BLACK);    }//GEN-LAST:event_jButton1ActionPerformed    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed        t = this.jComboBox1.getSelectedIndex() + 1;    }//GEN-LAST:event_jComboBox1ActionPerformed    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed//        new Thread(this).start();        this.run();    }//GEN-LAST:event_jButton2ActionPerformed    /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new Main().setVisible(true);            }        });    }    // Variables declaration - do not modify//GEN-BEGIN:variables    private pen.DrawPen drawPen1;    private javax.swing.JButton jButton1;    private javax.swing.JButton jButton2;    private javax.swing.JComboBox jComboBox1;    private javax.swing.JLabel jLabel1;    private javax.swing.JLabel jLabel2;    private javax.swing.JSeparator jSeparator1;    // End of variables declaration//GEN-END:variables    public void run() {         for (int i = 0; i < 10; i++) {            this.jLabel2.setText("" + i);            try {                Thread.sleep(300);            } catch (InterruptedException ex) {                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -