Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 喷漆组件未在JPanel上喷漆_Java_Swing_Jpanel_Awt_Paintcomponent - Fatal编程技术网

Java 喷漆组件未在JPanel上喷漆

Java 喷漆组件未在JPanel上喷漆,java,swing,jpanel,awt,paintcomponent,Java,Swing,Jpanel,Awt,Paintcomponent,我正在做一个家庭作业,我应该做一个程序,让你画自定义的形状和线条,并在屏幕上移动它们 最初,我使用public voidpaint(g)进行绘制,但当我调用repaint时,图形会闪烁 因此,我切换到paintComponent(g)。然而,当我试图画一个形状时,什么也看不出来。我相信这是因为它不是在JPanel之上绘制的 框架在3行BorderLayout中有3个面板 [Button JPanel] [Draw Box JPanel] [Coordinate Jpanel] 我想画的面板自然

我正在做一个家庭作业,我应该做一个程序,让你画自定义的形状和线条,并在屏幕上移动它们

最初,我使用public void
paint(g)
进行绘制,但当我调用repaint时,图形会闪烁

因此,我切换到
paintComponent(g)
。然而,当我试图画一个形状时,什么也看不出来。我相信这是因为它不是在
JPanel
之上绘制的

框架在3行
BorderLayout
中有3个面板

[Button JPanel]
[Draw Box JPanel]
[Coordinate Jpanel]
我想画的面板自然是画框面板

以下是我目前拥有的代码:

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

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class Main extends JPanel implements ActionListener, MouseListener, MouseMotionListener {

private String colors[] = { "Black", "Red", "Orange", "Yellow", "Green", "Blue" };
private String shapes[] = { "Ellipse", "Line", "Rectangle" };
private JMenuBar toolBar; // flow layout? OR box layout x-Axis
private JPanel drawBox;
private JPanel coordBox;
private JLabel coords;
private JButton undo = new JButton("Undo");
private JButton clear = new JButton("Clear");
private JComboBox color = new JComboBox(colors);
private JComboBox shape = new JComboBox(shapes);
private JCheckBox fill = new JCheckBox("Filled");

public Main() {
JFrame frame = new JFrame("Paint");
frame.setSize(400, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new BorderLayout());

/*
 * SETUP TOOLBAR
 */
toolBar = new JMenuBar();
toolBar.setLayout(new GridLayout(1, 5));
toolBar.add(undo);
toolBar.add(clear);
toolBar.add(color);
toolBar.add(shape);
toolBar.add(fill);

/*
 * ADD ACTION LISTENERS TO BUTTONS
 */
undo.addActionListener(this);
clear.addActionListener(this);
color.addActionListener(this);
shape.addActionListener(this);
fill.addActionListener(this);

/*
 * SETUP DRAW BOX
 */
drawBox = new JPanel();
drawBox.setOpaque(true);
drawBox.setPreferredSize(new Dimension(600, 400));
drawBox.addMouseListener(this);
drawBox.addMouseMotionListener(this);

/*
 * SETUP COORDINATES
 */
coords = new JLabel();
coordBox = new JPanel();

coordBox.setBackground(new Color(211, 211, 211));
coordBox.setPreferredSize(new Dimension(drawBox.getWidth(), 25));

coords.setText("Coords: [0,0]");
coords.addMouseMotionListener(this);

coordBox.add(coords);

/*
 * ADD TO FRAME
 */

frame.add(toolBar, BorderLayout.NORTH);
frame.add(drawBox, BorderLayout.CENTER);
frame.add(coordBox, BorderLayout.SOUTH);

frame.pack();

}

public static void main(String[] args) {
Main m = new Main();
}

public void paintComponent(Graphics g) {
// super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(300, 300, 100, 100);

}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == undo) {
    System.out.println("Undo Last");
}

if (e.getSource() == clear) {
    System.out.println("clearing screen");
}
// COLOR DROP DOWN BOX
if (e.getSource() == color) {
    JComboBox cb = (JComboBox) e.getSource();
    String selected = (String) cb.getSelectedItem();
    switch (selected) {

    case "Red":
    System.out.println("switching to color red");
    //ellipse.setColor(Color.RED);

    break;

    case "Orange":
    System.out.println("switch to coor orange");
    //ellipse.setColor(Color.ORANGE);
    break;
    case "Yellow":
    System.out.println("switching to color yellow");
    break;
    case "Green":
    System.out.println("Switching to green");
    //ellipse.setColor(Color.GREEN);

    break;
    case "Blue":
    System.out.println("Switching to blue");
    break;
    default:
    System.out.println("NOthing selected");
    break;
    }
}
// SHAPE DROP DOWN BOX
if (e.getSource() == shape) {
    JComboBox cb = (JComboBox) e.getSource();
    String selected = (String) cb.getSelectedItem();
    switch (selected) {
    case "Ellipse":
    System.out.println("switching to ellipse");
    break;
    case "Line":
    System.out.println("switch to line");
    break;
    case "Rectangle":
    System.out.println("switching to rectangle");
    break;

    default:
    System.out.println("NOthing selected");
    break;
    }
}

if (e.getSource() == fill) {
    JCheckBox cb = (JCheckBox) e.getSource();
    if (cb.isSelected()) {
    System.out.println("Fill shape");
    //ellipse.setFilled(true);
    } else {
    System.out.println("Empty shape");
    //ellipse.setFilled(false);
    }
}

}

@Override
public void mouseClicked(MouseEvent arg0) {
// System.out.println("Mouse clicked");

}

@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered");
}

@Override
public void mouseExited(MouseEvent arg0) {
coords.setText("Coords: N/A");

}

@Override
public void mousePressed(MouseEvent arg0) {
System.out.println("Mouse pressed");

}

@Override
public void mouseReleased(MouseEvent arg0) {
System.out.println("Mouse Released");

}

@Override
public void mouseDragged(MouseEvent e) {
coords.setText("Coords: [" + e.getX() + "," + e.getY() + "]");
}

@Override
public void mouseMoved(MouseEvent e) {
coords.setText("Coords: [" + e.getX() + "," + e.getY() + "]");
// repaint();
// revalidate();

}
}

您的Main类扩展了JPanel,有一个paintComponent方法,但是您从未将Main的实例添加到GUI中,而是将普通的JPanel、drawBox添加到GUI中,因此当然不会调用paintComponent


解决方案:将Main或
这个
添加到GUI中,而不是一个普通的JPanel。

您的主类扩展了JPanel,有一个paintComponent方法——但是您从不将Main的实例添加到GUI中,而是将普通的JPanel、drawBox添加到GUI中,因此当然永远不会调用paintComponent

解决方案:将Main或
添加到GUI,而不是普通的JPanel

我想画的面板是draw Box JPanel

您没有覆盖“drawBox”面板的
paintComponent(…)
方法,因此不会进行绘制

另外,每当您总是需要在paintComponent(…)方法的开始处调用super.paintComponent(…)时

查看定制绘画的两种常用方法的工作示例

我想画的面板是draw Box JPanel

您没有覆盖“drawBox”面板的
paintComponent(…)
方法,因此不会进行绘制

另外,每当您总是需要在paintComponent(…)方法的开始处调用super.paintComponent(…)时


查看定制绘画的两种常用方法的工作示例。

谢谢,这似乎很有效。我把drawBox JPanel都扔掉了,取而代之的是在相框上加上了“这个”。谢谢你,这似乎很管用。我把drawBox JPanel都扔掉了,取而代之的是在相框上加上了“this”。
drawBox = new JPanel();