gpt4 book ai didi

java - 如何创建实例

转载 作者:行者123 更新时间:2023-12-01 11:56:00 25 4
gpt4 key购买 nike

我有两个类,一个主类和一个扩展 JPanel 并实现 Runnable 的类。我试图在 actionListener 中为 JPanel 类的同一个实例创建两个线程,但我只是不知道在哪里创建 JPanel1 对象...

//编辑:按钮 1 是应用程序的启动。之后,按钮 2 将出现并带有标签的快速动画,单击它(按钮 2)也会启动相同的动画。每当单击这些按钮之一来启动运行方法时,我该怎么办?

public void run() {

if(isTom){

repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {

}
panel.removeAll();
panel.add(tomLabel1);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel2);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel3);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {

}
panel.add(tomLabel4);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel5);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}


panel.removeAll();
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}

}



public Game(){

JFrame frame = new JFrame();
Panel1 key = new Panel1();
key.addKeyListener(key);
frame.add(key);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();

}

public class Panel1 extends JPanel implements KeyListener,Runnable{
JButton button1 = new JButton("BUTTON1");
JButton button2 = new JButton("BUTTON2");
add(button1);add(button2);

Thread t = new Thread(this); // This works, but i need it inside the actionListener.

button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button1");
Thread x = new Thread(j);//'j' is an JPanel1 object. I need something like this i guess
x.setName("Thread x");});

button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button2");
Thread y = new Thread(j);
y.setName("Thread y");
});

public void run(){
System.out.println(Thread.currentThread().getName());
}

最佳答案

首先,Swing 不是线程安全的!这意味着您永远不应该从事件调度线程的上下文之外创建或修改 UI!

其次,Swing 是单线程环境,这意味着您永远不应该在事件调度线程的上下文中阻塞或执行长时间运行的代码,这将导致 UI 卡住,直到阻塞被删除。

你的概念是正确的,你的实现是错误的,你应该使用 Swing Timer 代替。

不要删除和添加标签,而是使用单个标签并更改其属性(文本/图标,等等)

参见Concurrency in SwingHow to use Swing Timers了解更多详情

Splash

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

try {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}

public class TestPane extends JPanel {

private JButton button1;
private JButton button2;

private SplashScreen splashScreen;

public TestPane() throws IOException {
button1 = new JButton("Button One");
button2 = new JButton("Button Two");

JPanel buttons = new JPanel();
buttons.add(button1);
buttons.add(button2);

button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
splashScreen.run();
}
});

button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
splashScreen.run();
}
});

splashScreen = new SplashScreen();

setLayout(new BorderLayout());
add(splashScreen);
add(buttons, BorderLayout.SOUTH);
}

}

public static class SplashScreen extends JPanel {

protected static final int IMAGE_COUNT = 4;

private JLabel label;
private Timer timer;

private int delta;
private int count;
private Icon[] icons;

private Dimension preferredSize;

public SplashScreen() throws IOException {
String path = "/images/splash";
String ext = ".png";
icons = new Icon[IMAGE_COUNT];
int maxWidth = 0;
int maxHeight = 0;
for (int index = 0; index < IMAGE_COUNT; index++) {
String name = path + (index + 1) + ext;
System.out.println(name);
icons[index] = new ImageIcon(ImageIO.read(getClass().getResource(name)));
maxWidth = Math.max(maxWidth, icons[index].getIconWidth());
maxHeight = Math.max(maxHeight, icons[index].getIconHeight());
}
preferredSize = new Dimension(maxWidth, maxHeight);

timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (count >= IMAGE_COUNT) {
count = IMAGE_COUNT - 2;
delta = -1;
} else if (count < 0) {
((Timer)e.getSource()).stop();
} else {
label.setIcon(icons[count]);
count += delta;
}
}
});

setLayout(new BorderLayout());
label = new JLabel();
add(label);
}

@Override
public Dimension getPreferredSize() {
return preferredSize;
}

public void run() {

if (!timer.isRunning()) {

delta = 1;
count = 0;
timer.start();

}

}

}

}

关于java - 如何创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28442232/

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