如何解决如何使java swings GUI应用程序动态化?
有人可以帮助我处理事件调度线程吗,我是这个 GUI 编程的初学者,并尝试构建一个 GUI 应用程序。在这里,我设法构建了一个应用程序,它为我提供了解决方案,但它只是静态的,我不想要那样。我的代码有两个类,一个是 GUI 的后端代码
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class KnightsV1{
// BufferedWriter bw;
int a[][];
int cnt1;
boolean flag;
// int[][] pos ={{2,1},{-2,{2,-1},{1,2},{-1,-2},-2}};
int xMove[] = { 2,1,-1,-2,2 };
int yMove[] = { 1,2,-1 };
int cnt;
KnightsV1()throws IOException{
flag=false;
a= new int[8][8];
new BufferedWriter(new FileWriter("entry.txt"));
cnt=0;
cnt1=0;
disp();
writer(0,0);
disp();
// bw.close();
}
public void disp1() throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("entry.txt",true));
if(cnt1==0)
bw.write("");
cnt1++;
bw.append(String.valueOf(cnt1)).append(" )\n");
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
bw.append(String.valueOf(a[i][j])).append("\t");
bw.append("\n");
}
bw.append("\n");
bw.close();
}
public void disp(){
// System.out.println();
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println();
}
public boolean writer(int row,int col)throws IOException{
cnt++;
for(int i=0;i<8;i++)
{
int r;
a[row][col]=cnt;
if(cnt==64)
return true;
row += xMove[i];
col += yMove[i];
if(is_valid(row,col))
{
if(writer(row,col))
return true;
else{
a[row][col] = 0;
cnt--;
}
}
row -= xMove[i];
col -= yMove[i];
}
return false;
}
private boolean is_valid(int row,int col) {
if(row>=0&&row<8&&col>=0&&col<8)
{
if(a[row][col]==0)
return true;
}
return false;
}
public static void main(String[] args)throws IOException {
new KnightsV1();
}
}
而下面这段代码就是这部分需要帮助的前端代码
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class KniGui extends JFrame{
KnightsV1 kt;
KniGui() throws IOException {
kt = new KnightsV1();
setSize(800,800);
setTitle("Knight's Tour");
setVisible(true);
MyJPanel j = new MyJPanel();
j.setSize(getWidth(),getHeight());
add(j);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MyJPanel extends JPanel{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.black);
Graphics2D g1= (Graphics2D) g;
g1.setStroke(new BasicStroke(5));
g1.setColor(Color.PINK);
for(int i=0;i<getHeight();i+=getHeight()/8)
g1.drawLine(0,i,getWidth(),i);
for(int i=0;i<getWidth();i+=getWidth()/8)
g1.drawLine(i,getHeight());
g1.setColor(Color.cyan);
int xnow=1,ynow=1;
g1.setFont(new Font("TimesRoman",Font.PLAIN,20));
g1.setColor(Color.cyan);
g1.drawString("1",getWidth()/16,getHeight()/16);
for(int i=2;i<65;i++){
for(int j=0;j<8;j++)
{
for(int k=0;k<8;k++ )
{
if(kt.a[j][k]==i){
g1.drawString(String.valueOf(i),getWidth()*(k+1)/8- getWidth()/16,getHeight()*(j+1)/8-getHeight()/16);
g1.drawLine(getWidth()*ynow/8-getWidth()/16,getHeight()*xnow/8-getHeight()/16,getWidth()*(k+1)/8-getWidth()/16,getHeight()*(j+1)/8-getHeight()/16);
//after this the code should sleep for a second
xnow=j+1;
ynow=k+1;
break;
}
}
}
}
}
}
public static void main(String[] args) throws IOException {
new KniGui();
}
}
在 GUI 中绘制每一行后,我希望它休眠一秒钟,然后继续以这种方式在视觉上更令人愉悦。这是我想要的方式,但 Swings 中的线程系统完全不同,所以任何了解这些东西的人都请帮助我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。