我使用JMenu,JToolBar和JPanel编写了一个小的绘图应用程序,问题是,当我开始在面板上绘制时,JMenu和JToolBar绘制在同一个面板上,有时候是面板的背景变为灰色而不是白色,这是它的样子:
这是我的代码: JFrame代码:
public class ArdoiseF extends JFrame {
private JMenuBar menu = new JMenuBar();
private JToolBar toolbar = new JToolBar();
private JMenu file = new JMenu("Fichier");
private JMenu edit = new JMenu("Edition");
private JMenu about = new JMenu("About");
private JMenu shape = new JMenu("Forme du curseur");
private JMenu color = new JMenu("Couleur du curseur");
private JMenuItem clear = new JMenuItem("Effacer");
private JMenuItem quit = new JMenuItem("Quitter");
private JMenuItem rond = new JMenuItem("Rond");
private JMenuItem carre = new JMenuItem("Carre");
private JMenuItem rouge = new JMenuItem("Rouge");
private JMenuItem bleu = new JMenuItem("Bleu");
private JMenuItem noir = new JMenuItem("Noir");
private JButton rougeButton = new JButton(new ImageIcon("rouge.jpg"));
private JButton bleuButton = new JButton(new ImageIcon("bleu.jpg"));
private JButton noirButton = new JButton(new ImageIcon("noir.jpg"));
private JButton formecarreeButton = new JButton(new ImageIcon("formecarree.png"));
private JButton formerondeButton = new JButton(new ImageIcon("formeronde.png"));
private JPanel container = new JPanel();
private PanneauF pan = new PanneauF();
private ColorListener cListener = new ColorListener();
private ShapeListener shapeListener = new ShapeListener();
public ArdoiseF(){
this.setTitle("Paint -_-");
this.setSize(700,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
initComposants();
this.setVisible(true);
}
private void initComposants(){
file.add(clear);
file.addSeparator();
file.add(quit);
file.setMnemonic('F');
clear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_DOWN_MASK));
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,KeyEvent.CTRL_DOWN_MASK));
shape.add(rond);
shape.add(carre);
color.add(rouge);
color.add(bleu);
color.add(noir);
edit.add(shape);
edit.add(color);
edit.setMnemonic('E');
menu.add(file);
menu.add(edit);
toolbar.add(formecarreeButton);
toolbar.add(formerondeButton);
toolbar.addSeparator();
toolbar.add(noirButton);
toolbar.add(rougeButton);
toolbar.add(bleuButton);
clear.addActionListener(new ClearListener());
rougeButton.addActionListener(cListener);
bleuButton.addActionListener(cListener);
noirButton.addActionListener(cListener);
rouge.addActionListener(cListener);
bleu.addActionListener(cListener);
noir.addActionListener(cListener);
formecarreeButton.addActionListener(shapeListener);
formerondeButton.addActionListener(shapeListener);
carre.addActionListener(shapeListener);
rond.addActionListener(shapeListener);
container.setLayout(new BorderLayout());
container.add(toolbar,BorderLayout.NORTH);
container.add(pan,BorderLayout.CENTER);
this.setContentPane(container);
this.setJMenuBar(menu);
}
class ClearListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
pan.setClean(true);
pan.repaint();
}
}
class ColorListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource() == rougeButton || e.getSource() == rouge)
pan.setColor(Color.red);
if(e.getSource() == bleuButton || e.getSource() == bleu)
pan.setColor(Color.blue);
if(e.getSource() == noirButton || e.getSource() == noir)
pan.setColor(Color.black);
}
}
class ShapeListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource() == formecarreeButton || e.getSource()== carre)
pan.setShape("carre");
if(e.getSource() == formerondeButton || e.getSource()== rond)
pan.setShape("rond");
}
}
}
JPanel代码:
public class PanneauF extends JPanel{
private int i=0;
private int mousex=0,mousey=0;
private boolean clean=true;;
private Color color= Color.black;
private String shape = "rond";
ArrayList<Point> points = new ArrayList<Point>();
public PanneauF(){
this.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e) {
points.add(new Point(e.getX()-7,e.getY()-7,15,color,shape));
repaint();
}
public void mouseMoved(MouseEvent arg0) {
}
});
this.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
points.add(new Point(e.getX()-7,e.getY()-7,15,color,shape));
repaint();
}
public void mouseReleased(MouseEvent e) {
}
});
}
public void paintComponent(Graphics g){
draw(g);
if(clean){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
clean=false;
points = new ArrayList<Point>();
}
}
private void draw(Graphics g){
for (Point p: this.points){
g.setColor(p.getColor());
if(p.getType()=="rond")
g.fillOval(p.getPosx(), p.getPosy(), 10, 10);
else{
g.fillRect(p.getPosx(), p.getPosy(), 10, 10);
}
}
}
public void setClean(boolean c){
this.clean = c;
}
public void setColor(Color c){
this.color = c;
}
public void setShape(String S){
this.shape=S;
}
}
答案 0 :(得分:3)
只需在super.paintComponent(g);
public void paintComponent(Graphics g)
方法的开头添加PanneauF
即可。
答案 1 :(得分:1)
重新绘制面板时,你真的应该重新绘制整个面板。首先清除它,然后通过并重新添加所有点。
现在看起来很奇怪,因为你画了所有的点,然后你有时候但有时只清楚它?
只要总是清楚它然后绘制点 - 否则你可能会从之前的窗口涂料中得到一些永远不会被写过的东西。如果Swing组件标记为不透明,则只允许不绘制每个像素。
答案 2 :(得分:0)
我遇到了同样的问题。将JMenuBar放入JFrame中。仅将JPanel用于绘制。 希望它有所帮助。