Charles_Su 2017-04-11 12:26 采纳率: 21.4%
浏览 1279

java window制作简单绘图工具 为什么没有显示啊?

package Drawing;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import java.awt.event.MouseMotionAdapter;
import java.awt.Color;
import javax.swing.border.LineBorder;
import java.awt.Window.Type;

public class DrawingBoard extends JPanel{

private JFrame frmDrawingboard;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DrawingBoard window = new DrawingBoard();
                window.frmDrawingboard.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public DrawingBoard() {
    initialize();

}

/**
 * Initialize the contents of the frame.
 */
int x1, y1, x2, y2;  
Vector<Shapes> vectorShapes = new Vector<Shapes>();
private void initialize() {
    frmDrawingboard = new JFrame();
    frmDrawingboard.setResizable(false);
    frmDrawingboard.setTitle("DrawingBoard\r\n");
    frmDrawingboard.getContentPane().setBackground(Color.DARK_GRAY);
    frmDrawingboard.setBounds(100, 100, 464, 350);
    frmDrawingboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmDrawingboard.getContentPane().setLayout(null);

    JComboBox comboBox = new JComboBox();
    comboBox.setForeground(Color.RED);
    comboBox.setBackground(Color.BLACK);
    comboBox.setModel(new DefaultComboBoxModel(new String[] {"Line", "Rectangle", "Oval"}));
    comboBox.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
    comboBox.setBounds(345, 280, 102, 21);
    frmDrawingboard.getContentPane().add(comboBox);

    JPanel panel = new JPanel();
    panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
    panel.setBackground(Color.WHITE);

    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
               x1 = e.getX();  
               y1 = e.getY(); 
               Shapes c =new Shapes();
               switch(comboBox.getSelectedIndex()){
               case -1:c = new Line(x1,y1,x1,y1);break;
               case 0:c = new Rectangle(x1,y1,x1,y1);break;
               case 1:c = new Oval(x1,y1,x1,y1);break;
                } //end switch
                vectorShapes.add(c);
        }
    });
    panel.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
             vectorShapes.get(vectorShapes.size()-1).x2 = e.getX();  
             vectorShapes.get(vectorShapes.size()-1).y2 = e.getY();  
             repaint(); 
        }
    });
    panel.setBounds(10, 10, 437, 260);
    frmDrawingboard.getContentPane().add(panel);
}
 protected void paintComponent(Graphics g){  
        g.clearRect(0, 0, getWidth(), getHeight());  
        for(int i = 0; i < vectorShapes.size(); i++)   
        {  
            vectorShapes.get(i).draw(g);  
        }  
    }

}
图片说明

  • 写回答

1条回答 默认 最新

  • 白萝卜。 2017-04-12 05:10
    关注

    http://www.yiibai.com/swing/swing_jpanel.html

    自己看吧,现在用swing,awt的人太少了。

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序