Java 创建新按钮时,按钮不会显示,形状会一直消失
谢谢,我终于让按钮正常工作了,但现在我被困在制作我刚刚创建的形状以保持在屏幕上。每次单击创建新形状时,我都会选择刚刚创建的其他形状。我看了看定制的油漆,但仍然感到困惑Java 创建新按钮时,按钮不会显示,形状会一直消失,java,swing,Java,Swing,谢谢,我终于让按钮正常工作了,但现在我被困在制作我刚刚创建的形状以保持在屏幕上。每次单击创建新形状时,我都会选择刚刚创建的其他形状。我看了看定制的油漆,但仍然感到困惑 public class ShapeStamps extends JFrame { Random numGen = new Random(); public int x; public int y; private JPanel mousePanel, Bpanel; private JB
public class ShapeStamps extends JFrame {
Random numGen = new Random();
public int x;
public int y;
private JPanel mousePanel, Bpanel;
private JButton circle, square, rectangle, oval;
private int choice = 0;
public ShapeStamps() {
super("Shape Stamps");
Bpanel = new JPanel();
circle = new JButton("Circle");
square = new JButton("Square");
rectangle = new JButton("Rectangle");
oval = new JButton("Oval");
circle.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
choice = 1;
}
});
square.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
choice = 2;
}
});
rectangle.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
choice = 3;
}
});
oval.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
choice = 4;
}
});
mousePanel = new JPanel();
mousePanel.setBackground(Color.WHITE);
MouseHandler handler = new MouseHandler();
setVisible(true);
addMouseListener(handler);
addMouseMotionListener(handler);
add(mousePanel);
Bpanel.add(circle);
Bpanel.add(square);
Bpanel.add(rectangle);
Bpanel.add(oval);
add(Bpanel, BorderLayout.SOUTH);
setSize(500, 500);
setVisible(true);
}
private class MouseHandler extends MouseAdapter implements
MouseMotionListener {
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
}
public void paint(Graphics g) {
super.paintComponents(g);
setBounds(0, 0, 500, 500);
Graphics2D g2d = (Graphics2D) g;
if (choice == 0) {
g2d.setFont(new Font("Serif", Font.BOLD, 32));
g2d.drawString("Shape Stamper!", 150, 220);
g2d.setFont(new Font("Serif", Font.ITALIC, 16));
g2d.drawString("Programmed by: None", 150, 245);
}
if (choice == 1) {
Color randomColor1 = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
g2d.setPaint(randomColor1);
g2d.drawOval(x - 50, y - 50, 100, 100);
}
if (choice == 2) {
Color randomColor2 = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
g2d.setPaint(randomColor2);
g2d.drawRect(x - 50, y - 50, 100, 100);
}
if (choice == 3) {
Color randomColor2 = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
g2d.setPaint(randomColor2);
g2d.draw(new Rectangle2D.Double(x - 75, y - 50, 150, 100));
}
if (choice == 4) {
Color randomColor2 = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
g2d.setPaint(randomColor2);
g2d.draw(new Ellipse2D.Double(x - 50, y - 25, 100, 50));
}
}
}
每次重新绘制JFrame时,它都会经过绘图过程,因此如果在屏幕上标记某些内容,则必须有一个数据结构来保存标记的对象。这样,每次调用绘图过程时,它都会将对象重新绘制到正确的位置,并且可能会随您的决定而改变颜色请在发布时正确设置代码格式,这将有助于其他人帮助您。不要使用空布局和收进框。Swing设计用于布局管理器。有关详细信息,请阅读上的Swing教程。修复您的代码,如果您仍然有问题,然后张贴正确格式化的更新代码。另外,不要覆盖油漆。教程中还有一个关于自定义绘制的部分,给出了正确绘制方法的示例。