gpt4 book ai didi

java - 为什么这个油漆成分不起作用?

转载 作者:行者123 更新时间:2023-12-02 06:05:04 25 4
gpt4 key购买 nike

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;


public class Main {
public static void main(String[]args){
@SuppressWarnings("unused")
Gui g = new Gui();
}
}
@SuppressWarnings("serial")
class Gui extends JFrame{
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
double width = screen.getWidth();
double height = screen.getHeight();
JPanel canvas = new JPanel();
Point mloc = new Point();

JButton br = new JButton("Red");
JButton bb = new JButton("Blue");
JButton bg = new JButton("Green");
JButton wipe = new JButton("Wipe");

JLabel brushwidth = new JLabel("Width = ",JLabel.CENTER);

public JSlider s = new JSlider();

JButton image = new JButton("Image");

Point start = null;
Point current = null;
boolean entered = false;
Color c = Color.red;
public double bwidth = 3;

Gui(){
super("PaintPot");
setSize((int)width/4,(int)height/2);
setVisible(true);
setResizable(false);
setLayout(null);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

add(s);
s.setSize(getWidth()-20, 20);
s.setLocation(10, 450);
s.setBackground(Color.gray);
s.setForeground(Color.orange);
s.setMajorTickSpacing(50);
s.setMinorTickSpacing(1);
s.setValue((int)bwidth);
s.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
bwidth = s.getValue();
brushwidth.setText("Width = "+s.getValue());
}
});
brushwidth.setText("Width = "+s.getValue());

add(brushwidth);
brushwidth.setSize(70,30);
brushwidth.setLocation(90,410);
brushwidth.setBackground(Color.gray);
brushwidth.setForeground(Color.orange);
brushwidth.setOpaque(true);
brushwidth.setVisible(true);

add(wipe);
wipe.setSize(70,30);
wipe.setLocation(10, 410);
wipe.setBackground(Color.gray);
wipe.setForeground(Color.orange);
wipe.setVisible(true);

add(br);
br.setSize(60,30);
br.setLocation(10, 10);
br.setBackground(Color.red);
br.setForeground(Color.white);
br.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
c = Color.red;
}
});
br.setVisible(true);

add(bb);
bb.setSize(60,30);
bb.setLocation(80, 10);
bb.setBackground(Color.blue);
bb.setForeground(Color.white);
bb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
c = Color.blue;
}
});
bb.setVisible(true);


add(bg);
bg.setSize(70,30);
bg.setLocation(150, 10);
bg.setBackground(Color.green);
bg.setForeground(Color.white);
bg.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
c = Color.green;
}
});
bg.setVisible(true);

add(image);
image.setSize(70,30);
image.setLocation(230, 10);
image.setBackground(Color.gray);
image.setForeground(Color.orange);
wipe.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){

}

});
image.setVisible(true);
canvas(this,canvas);

}
public void canvas(JFrame f, JPanel p){
p.setSize(425,350);
p.setBorder(BorderFactory.createLineBorder(Color.black, 3));
p.setLocation(10, 50);
p.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent arg0) {entered = true;}
@Override
public void mouseExited(MouseEvent arg0) {entered = false;}
@Override
public void mousePressed(MouseEvent arg0) {}
@Override
public void mouseReleased(MouseEvent arg0) {}
});
p.addMouseMotionListener(new MouseAdapter(){
public void mouseDragged(MouseEvent e){
mloc = e.getLocationOnScreen();
if(entered = true){
paintComponent(getGraphics());
}
}
});


f.add(p);
}
public void paintComponent(Graphics g){
g.drawOval(mloc.x, mloc.y, (int)bwidth, (int)bwidth);
}
}

我试图让这个应用程序在 JPanel 内绘画,但我似乎无法让它工作,我希望每当我移动鼠标时它都会画一条线。这只是公共(public)空白油漆位,我无法掌握似乎没有任何作用。谢谢

最佳答案

JFrame 没有 PaintComponent() 方法。

自定义绘制是通过重写 JPanel(或 JComponent)的 PaintComponent() 方法来完成的,然后将面板添加到 JFrame。

阅读 Swing 教程中关于 Custom Painting 的部分了解更多信息和示例。您还需要重写 getPreferredSize() 方法。

另外,不要使用空布局。 Swing 被设计为与布局管理器一起使用。

关于java - 为什么这个油漆成分不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22358416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com