ここに私のコードがあります:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class Main {
// code main
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(460, 500);
frame.setTitle("Circles generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});
String input = JOptionPane.showInputDialog("Enter n:");
CustomComponents0 component = new CustomComponents0();
frame.add(component);
frame.getContentPane().validate();
System.out.println("work before");
frame.getContentPane().repaint();
System.out.println("work");
frame.getContentPane().repaint();
System.out.println("work after");
}
// why is not JComponent
static class CustomComponents0 extends JLabel {
private static final long serialVersionUID = 1L;
@Override
public Dimension getMinimumSize() {
return new Dimension(200, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
@Override
public void paintComponent(Graphics g) {
System.out.println("paint");
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}
ここにコードがあり、作業前に再描画を実行し、作業後に再描画を実行します。実行すると印刷されるはずです
work before
paint
work
paint
work after
でも塗装は一点しかなくて作業後なのに、どうしてこうなったの?どうすれば修正できますか?
ありがとう。
Swing GUI オブジェクトは、イベント ディスパッチ スレッドでのみ構築および操作する必要があります。プログラムが正しく同期されていないため、どのような結果も発生する可能性があります。これは、最初のスレッドが を開始する前にどの程度到達するかに部分的に依存します。また、それ自体が同期することもあり、「に投稿されているイベントを合体させることができます」。EventQueue
println()
EventQueue
以下のバリエーションは、イベントが「エンキューされたのと同じ順序で」ディスパッチされるため、次の出力を確実に示しています。への呼び出しがどのように結合さrepaint()
れるかに特に注意してください。このアプローチは実例に過ぎませんが、目標を達成するためには不必要に面倒です。代わりに、 を使用javax.swing.Timer
して、ここに示すようにアニメーションのペースを調整します。
コンソール:
paint
work before
work
work after
paint
コード:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
/** @see https://stackoverflow.com/a/44212328/230513 */
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new JFrame();
frame.setTitle("Circles generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CustomComponent component = new CustomComponent();
frame.add(component);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
EventQueue.invokeLater(() -> { System.out.println("work before"); });
EventQueue.invokeLater(() -> { frame.repaint(); });
EventQueue.invokeLater(() -> { System.out.println("work"); });
EventQueue.invokeLater(() -> { frame.repaint(); });
EventQueue.invokeLater(() -> { System.out.println("work after"); });
});
}
static class CustomComponent extends JLabel {
private static final int N = 10;
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("paint");
g.setColor(Color.red);
g.fillRect(N, N, getWidth() - N * 2, getHeight() - N * 2);
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加