JComboBox涵盖其他内容?

时间:2016-10-01 08:51:30

标签: java jcombobox paintcomponent

点击JComboBox后,JComboBox覆盖了灰色矩形形状的绘画的某些部分,代码有问题,我该如何解决?谢谢!

Here's the image.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MouseButtonTester extends JFrame implements     MouseMotionListener{
    int x,y,r;
    JComboBox colorChooser;
    Color color;
    JTextField red = new JTextField();
    JTextField green = new JTextField();
    JTextField blue = new JTextField();
    JPanel topPanel = new JPanel();
    JComboBox pen;
    int fillKind;

    Object[] chooseRGB = {
        "Red: ", red, 
        "Green: ", green,
        "Blue: ", blue
    };


    public MouseButtonTester(){
    super();
    this.addMouseMotionListener(this);
    setResizable(true);

    setLayout(new BorderLayout());
    add(topPanel,BorderLayout.NORTH);
    topPanel.setLayout(new GridLayout(1,2));
    colorChooser = new JComboBox();
    pen = new JComboBox();
    topPanel.add(pen);
    topPanel.add(colorChooser);
    colorChooser.setBackground(Color.WHITE);
    pen.setBackground(Color.WHITE);

    pen.addItem("Pen");
    pen.addItem("Marker");
    pen.addItem("Highlighter");
    pen.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(pen.getSelectedItem().toString().equals("Pen")){
                fillKind = 0;
                r = 8;
            }else if(pen.getSelectedItem().toString().equals("Marker")){
                fillKind = 0;
                r = 15;
            }else if(pen.getSelectedItem().toString().equals("Highlighter")){
                fillKind = 1;

            }
        }
    });

    colorChooser.setFont(new Font("Serif",Font.PLAIN,14));
    colorChooser.addItem("Red");
    colorChooser.addItem("Orange");
    colorChooser.addItem("Yellow");
    colorChooser.addItem("Green");
    colorChooser.addItem("Blue");
    colorChooser.addItem("Violet");
    colorChooser.addItem("Purple");
    colorChooser.addItem("Choose RGB");
    colorChooser.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if("Red" == colorChooser.getSelectedItem().toString()){
                color = Color.RED;
            }else if("Orange" == colorChooser.getSelectedItem().toString()){
                color = Color.ORANGE;
            }else if("Yellow" == colorChooser.getSelectedItem().toString()){
                color = Color.YELLOW;
            }
            else if("Green" == colorChooser.getSelectedItem().toString()){
                color = Color.GREEN;
            }
            else if("Blue" == colorChooser.getSelectedItem().toString()){
                color = Color.BLUE;
            }
            else if("Violet" == colorChooser.getSelectedItem().toString()){
                color = new Color(180,0,200);
            }
            else if("Purple" == colorChooser.getSelectedItem().toString()){
                color = new Color(150,0,200);
            }
            else if("Purple" == colorChooser.getSelectedItem().toString()){

            }
            else if("Choose RGB" ==     colorChooser.getSelectedItem().toString()){
                int option = JOptionPane.showConfirmDialog(null, chooseRGB,        "Choose RGB", JOptionPane.OK_CANCEL_OPTION);
                if(option == JOptionPane.OK_OPTION){
                        int redValue = Integer.parseInt(red.getText());
                        int greenValue = Integer.parseInt(green.getText());
                        int blueValue = Integer.parseInt(blue.getText());
                        color = new Color(redValue,greenValue,blueValue);
                    }
                }
            }
        });
    } 

    Graphics graphics;

    public void paint(Graphics g){
        graphics = g.create();
    }

    public void mouseDragged(MouseEvent e) {
        x = e.getX();
        y = e.getY();

        graphics.setColor(color);
        if(fillKind == 0){
            graphics.fillOval(x, y, r, r);
        }else if(fillKind == 1){
            graphics.fillRect(x, y, 10, 25);
        }
        repaint();
    }

    public void mouseMoved(MouseEvent arg0) {
        //No actions
    }   
}

1 个答案:

答案 0 :(得分:0)

如果仔细查看代码,在JPanel上绘制的JComboBox就意味着在绘图区域上绘制。修复此问题的简单方法创建两个面板;第一个用于工具(组合框)并添加其他工具,另一个面板用于绘图区域。

另一种方法是选择项目后重新绘制面板。