gpt4 book ai didi

java - 重绘()不工作

转载 作者:行者123 更新时间:2023-12-01 22:24:51 26 4
gpt4 key购买 nike

我想画圆并将其与中心对齐,但是当我调用repaint()时什么也没有发生。我几乎尝试了一切,我改变了布局、对齐方式,但总是一样的。这是我的代码:

public class Frame extends JFrame {

JButton button,dugme;
JLabel lab;

public Frame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(480,320);
setResizable(false);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
setVisible(true);

button = new JButton("Klikni me");
button.setSize(75,75);
add(button);
button.setHorizontalAlignment(SwingConstants.RIGHT);

dugme= new JButton("Klikni opet");
dugme.setSize(75,75);
add(dugme);
dugme.setHorizontalAlignment(SwingConstants.LEFT);

lab = new JLabel("Ovde je tekst koji se menja");
add(lab);
lab.setHorizontalAlignment(SwingConstants.CENTER);
Handler handler = new Handler();
Handler1 handler1= new Handler1();
repaint();

button.addActionListener(handler);
dugme.addActionListener(handler1);
}

public void paintComponent(Graphics g){
super.paintComponents(g);
g.fillOval(30, 30, 60, 75);
}

public class Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
lab.setText("New text");
}
}

public class Handler1 implements ActionListener{
public void actionPerformed(ActionEvent e){
lab.setText("Same text again ");
repaint();
}
}
}

最佳答案

public void paintComponent(Graphics g){
super.paintComponents(g);

打破油漆链,应该是:

public void paintComponent(Graphics g){
super.paintComponent(g); // no S in method name..

关于java - 重绘()不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29005615/

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