- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试复制类似绘画的程序,但每当我单击屏幕时,菜单栏就会显示在面板区域中。除非我单击屏幕(进行绘制),否则它不会执行此操作。菜单栏添加在面板之前,任何帮助将不胜感激。
public class Main {
public static Frame frame;
public static Panel panel;
public static MenuBar menubar;
public static void main(String[] args) {
frame = new Frame();
panel = new Panel();
menubar = new MenuBar();
frame.setJMenuBar(menubar);
frame.addMouseMotionListener(panel);
frame.add(panel);
frame.setVisible(true);
}
}
public class Frame extends JFrame {
public Frame() {
setTitle("Paint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
public class Panel extends JPanel implements MouseMotionListener {
public boolean isMouseDown = false;
public int x1,y1, x2, y2;
public Color colour = Color.BLACK;
public int size = 3;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(colour);
g2.setStroke(new BasicStroke(size));
g2.drawLine(x1 - 10, y1 - 80, x2 - 10, y2 - 80);
}
public void mouseDragged(MouseEvent arg0) {
x1 = x2;
y1 = y2;
x2 = arg0.getX();
y2 = arg0.getY();
repaint();
}
public void mouseMoved(MouseEvent arg0) {
x1 = x2;
y1 = y2;
x2 = arg0.getX();
y2 = arg0.getY();
}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
}
public class MenuBar extends JMenuBar {
JMenu file = new JMenu("File");
JMenu brush = new JMenu("Pen");
JMenu colour = new JMenu("Colour");
Font font = new Font("Times New Romans", Font.PLAIN, 18);
public MenuBar() {
//JMenuBar Code here, left black as very long
}
}
最佳答案
一个问题:您需要在paintComponent方法中调用super.paintComponent(g);
。否则 JPanel 无法自行完成内务工作。
如果您希望绘图持续存在,则可以创建一个线条的 ArrayList,然后在 for 循环中在 PaintComponent 中绘制这些线条,或者更好的是,绘制到 BufferedImage 上,然后在 PaintComponent 方法中显示该图像。
例如,
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class MyMain {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
MyPainting mainPanel = new MyPainting();
MenuCreator menuCreator = new MenuCreator(mainPanel);
JFrame frame = new JFrame("MyPainting");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.setJMenuBar(menuCreator.getMenubar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class MyPainting extends JPanel {
private static final int IMG_W = 600;
private static final int IMG_H = 450;
private static final int STROKE_W = 3;
private static final Stroke STROKE = new BasicStroke(STROKE_W);
private BufferedImage img = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_ARGB);
private Color drawColor = Color.BLACK;
public MyPainting() {
setPreferredSize(new Dimension(IMG_W, IMG_H));
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}
private class MyMouse extends MouseAdapter {
private Graphics2D g2 = null;
private Point p0;
@Override
public void mousePressed(MouseEvent e) {
if (img == null) {
return;
}
g2 = img.createGraphics();
g2.setStroke(STROKE);
g2.setColor(drawColor);
p0 = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
if (p0 == null) {
return;
}
drawLine(e);
}
@Override
public void mouseReleased(MouseEvent e) {
if (p0 == null) {
return;
}
drawLine(e);
g2.dispose();
p0 = null;
}
private void drawLine(MouseEvent e) {
Point p1 = e.getPoint();
g2.drawLine(p0.x, p0.y, p1.x, p1.y);
repaint();
p0 = p1;
}
}
public void setDrawColor(Color drawColor) {
this.drawColor = drawColor;
}
public void clear() {
img = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_ARGB);
repaint();
}
}
class MenuCreator {
private JMenuBar menubar = new JMenuBar();
private MyPainting myPainting;
public MenuCreator(MyPainting myPainting) {
JMenuItem clearDrawing = new JMenuItem(new AbstractAction("Clear Drawing") {
@Override
public void actionPerformed(ActionEvent arg0) {
if (myPainting != null) {
myPainting.clear();
}
}
});
JMenu fileMenu = new JMenu("File");
fileMenu.add(clearDrawing);
JMenu colourMenu = new JMenu("Colour");
for (MyColor myColor : MyColor.values()) {
colourMenu.add(new JMenuItem(new ColorAction(myColor)));
}
menubar.add(fileMenu);
menubar.add(new JMenu("Pen"));
menubar.add(colourMenu);
this.myPainting = myPainting;
}
public JMenuBar getMenubar() {
return menubar;
}
private class ColorAction extends AbstractAction {
private MyColor myColor;
public ColorAction(MyColor myColor) {
super(myColor.getText());
this.myColor = myColor;
}
@Override
public void actionPerformed(ActionEvent e) {
if (myPainting != null) {
myPainting.setDrawColor(myColor.getColor());
}
}
}
}
enum MyColor {
BLACK("Black", Color.BLACK), RED("Red", Color.RED), ORANGE("Orange", Color.ORANGE), BLUE("Blue", Color.BLUE);
private String text;
private Color color;
private MyColor(String text, Color color) {
this.text = text;
this.color = color;
}
public String getText() {
return text;
}
public Color getColor() {
return color;
}
}
关于java - 为什么这个 JMenu 会显示在这个 JPanel 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855994/
我正在尝试创建自定义弹出菜单。 硬性要求如下: 1) 语音气泡形状 2)透明度 3) 菜单一直打开,直到通过关闭按钮关闭为止 我已经成功实现了上述所有三个目标,但是当我尝试添加普通的子菜单时,它就是行
下面的代码创建了一个简单的 GUI,中间有一个按钮,单击该按钮后,应更新 JMenuBar 组件的字体。为此,setMyFont 方法会在 JButton 上的 ActionListener 中触发。
我对 JMenu 的 arrowIcon 有问题:当我将鼠标悬停在它上面时,它会丢失我为其设置的颜色。 这是最小的例子 public class DemoLookAndFeel extends JFr
我试图将 2 个 JRadioButtonMenuItem 作为子菜单项添加到 JMenuItem,但子菜单项未正确显示。 这是我到目前为止尝试过的代码 private void setupEdit
在我的应用程序中,当我按 JMenuBar 中的“编辑”时,我会收到 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep
我的 JFrame 中有一个 JMenu 和 JPanel 设置代码: public Gui(String title) { super(title); createGUICompon
我在使用 JMenu 弹出窗口时遇到问题。它只是有时有效。有时我根本看不到 java 弹出窗口。有时我的文件和编辑选项完全丢失。这就是我的代码的样子。 import javax.swing.*; pu
我有一个带有 JMenu 的 JFrame。当我在某些情况下添加包含图像的 JPanel 时,程序首次启动时菜单会重复。调整大小时重复的菜单消失。任何建议都非常感激。谢谢。代码如下: public c
我想使用 Jmenu 作为按钮来更改框架中显示的内容,但是当选择菜单时,选择不会释放。下面是 Jmenu 实例的代码,它更改了两个字段和标签的位置。 mnAddUser.addMenuListener
我们获得了 Jmjrst 开源项目,应该对其进行一些配置和工作。现在我们从来没有任何 JMenu、JMenuItem 和 Listener 的东西,所以这对我来说非常困惑。 我已经实现了新的 Menu
这是一个非常奇怪的错误,也是一个非常大的程序,但我将发布它们相互交互的代码。 所以我有一个扫雷游戏,其中实现了“作弊”功能,它会显示棋盘上的所有地雷。 这是调用它的方法: public void sh
有人知道如何控制JMenu子菜单上出现的小箭头的显示吗? 我可以更改吗?我可以禁用它吗?我可以移动它吗? 此外,我注意到只有当顶级 JMenu 是其他 JMenu 的子菜单时,此箭头才不会出现在顶级
我正在开发一个 java 项目,其中使用了一些 swing 组件。我的 JMenuBar 有问题。我有一个扩展 JMenuBar 的类。它是功能性的,它作为一对 JMenu 对象和每个对象中的一对 J
我正在尝试在单击时动态构建一个 JMenu(我只得到一个空菜单),下面是我的代码。 final JMenu JMWindows = new JMenu("Opened Windows");
我一直在研究一个 html 文本编辑程序,我想知道是否有人可以帮助我解决这个小问题。我有两个 jmenus,但其中一个行为不端。我有两个问题。 1 菜单中的菜单项虽然不是子菜单,但有一个子菜单箭头 是
我对这段代码的问题是,当我运行它时,JMenuItem 没有出现。我很困惑!任何人都可以帮忙吗? import javax.swing.*; import java.awt.*; import jav
我正在使用 jmenu.js 并将子菜单元素设置为具有透明度。问题是具有透明度的子元素显示父元素(即主菜单栏)的背景。我想要的是在显示到页面背景的子菜单上具有透明度。 CSS 是: .jMenu {
我刚刚开始使用 JMenu。单击按钮时,我希望菜单显示子菜单类别,例如保管箱。 所以我的问题是,“为什么子菜单不可见?” 请查看下面的相关代码。 // Frame title fram
回应最近的slashdot post ,我决定添加一个选项,让菜单出现在左侧,这样在打开时它就不会遮挡整个应用程序。只是为了用户方便,有没有办法强制 jdesktop 上的 jmenu 位于左侧或右侧
我需要创建 JMenu ,它应该具有: 子菜单项 复选框。 像这样: 有没有简单的方法来创建它,或者我必须 首先学习核心合并JCheckBoxMenuItem和JMenu类? PS:是的,我知道我可以
我是一名优秀的程序员,十分优秀!