Как обновить мой JPanel в простом многопоточном состоянии? - PullRequest
0 голосов
/ 11 ноября 2019

Есть 3 потока, каждый поток добавит JButton в JPanel, а в последнем 3 окне должно быть 3 Jbutton соответственно, но 1 jbutton и 2,3 соответственно, я пытаюсь использовать методы wait () и notifyAll () дляобновить JPanel до 3 кнопок J, но не удалось

(Кстати, я новичок в этом, проблема возникла из-за сложного вопроса списка контактов Server_Client, я сильно упростил его, как показано ниже)

Пример JFrame shot

import javax.swing.*;

class TestPanel implements Runnable {
//    the common Jpanel of 3 thread
static JPanel SharedPanel = new JPanel();

//    the common JFrame of 3 thread
static JFrame SharedFrame = new JFrame();


//    the JFrame window x,y position
static int Position = 200;

JButton Button1;
String ButtonName;


public TestPanel(String name) {

//        pass a name to JButton
    this.ButtonName = name;


}

public static void main(String[] args) throws InterruptedException {

//      initializing a "A" named JButton to the common Jpanel
    new Thread(new TestPanel("A")).start();


//      initializing a "B" named JButton to the common Jpanel

    new Thread(new TestPanel("B")).start();

//      initializing a "C" named JButton to the common Jpanel

    new Thread(new TestPanel("C")).start();

}


@Override
public void run() {
//      initializing jbutton 
    Button1 = new JButton(ButtonName);

//        add Jbutton to the static common jpanel
    SharedPanel.add(Button1);

//create a new JFrame ,cause 3 window need 3 different jframe (with the same content)
    JFrame jf = new JFrame();

 //      add that common shared japnel the Jframe
    jf.add(SharedPanel);

//        default initializing of window
    jf.setSize(500, 500);

//        to prevent overlap window , offset a little bit for better observation
    jf.setLocation(Position += 50, Position += 50);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);


}


}

Как обновить каждое окно после добавления новой кнопки J в jpanel?

(я также пытаюсь назначить функцию while в концеЗапустите (), но я обнаружил, что это бесполезно, возможно, мой вопрос легок для вас, спасибо за вашу помощь!)

1 Ответ

1 голос
/ 11 ноября 2019

вызов:

revalidate();

, а затем

repaint();

на вашем общем JPanel обновит его.

Если вы хотите обновить все кадры, вы можете вызвать этиметоды для фреймов с "notifyAll".

...