界面
- Jpanle中setBounds()方法不起作用
原因:JPanel的setLayout布局与setBounds方法相冲突
解决措施:
取消JPanel的布局
setLayout(null);
动态登录界面可以用一个线程来实行图片移动
@Override public void run() { //图片循环 for (int j = 1; j <=8 ; j++) { JLabel jLabel=new JLabel(new ImageIcon("Image\\背景界面\\MARVEL"+j+".jpg")); if (j==8){ j=0; } int k=1+j; JLabel jLabel1=new JLabel(new ImageIcon("Image\\背景界面\\MARVEL"+k+".jpg")); for (int i = 0; i < 1000; i++) { jLabel.setBounds(i,0,1000,500); jLabel1.setBounds(-1000+i,0,1000,500); this.getContentPane().add(jLabel); this.getContentPane().add(jLabel1); this.getContentPane().repaint(); try { Thread.sleep(8); } catch (InterruptedException e) { e.printStackTrace(); } this.getContentPane().remove(jLabel); this.getContentPane().remove(jLabel1); if (!y){ System.out.println("哈哈哈哈哈"); this.getContentPane().remove(jLabel13); this.getContentPane().remove(jLabel7); jLabel13=jbl13; jLabel13.setBounds(225,50,50,50); jLabel13.setSize(50,50); jLabel13.addMouseListener(this); this.getContentPane().add(jLabel13); this.getContentPane().add(jLabel7); this.getContentPane().repaint(); t=true; y=true; } } } }
- Tread.sleep是对于一个线程来暂停的
- 在Jframe中用添加Jpanel类型的东西,在使用setbounds函数时,将Jpanel使用remove去除时,再重新添加时如果调用repaint函数是无法刷新的,需要用到setvisible(true)函数才能刷新出来。
(4)对于文本框的setBorder()函数可以来写自定义一下
jTextField1.setBorder(new My(c));
public class My extends AbstractBorder { private Color color; public My(Color color){ this.color=color; } public void paintBorder(Component c,Graphics g,int x,int y,int width,int heigh){ Graphics2D g2=(Graphics2D) g; g2.setColor(color); //文本框水平 垂直 还有宽 高 还有 弧度宽 高 g2.drawRoundRect(2,0,c.getWidth() - 4,c.getHeight() - 4,30,30); } public Insets getBorderInsets(Component c){ //设置文本框文字位置 return new Insets(0,10,4,0); } }
心得
- 对象是类的实例化,所以我们在写一个要用到的类的时候可以把基本骨架搭好,然后再需要生成对象时把需要的东西添加上就可以了
- 而创造这个要用到类就可以用到构造方法。
#include <stdio.h>
#include <stdbool.h>
#define MAX 100
// 定义迷宫的行数和列数为全局变量
int m, n;
// 判断当前位置是否合法
bool isValid(int maze[][MAX], int x, int y)
{
if (x < 0 || y < 0 || x >= m || y >= n || maze[x][y] == 1)
return false;
return true;
}
// DFS遍历迷宫寻找路径
bool dfs(int maze[][MAX], int path[][2], int x, int y, int end_x, int end_y)
{
// 如果当前位置是终点,则找到路径,终止递归
if (x == end_x && y == end_y)
return true;
// 如果当前位置合法,则标记该位置已经访问过,并保存该位置的坐标到路径中
if (isValid(maze, x, y))
{
maze[x][y] = 1;
path[x][y] = 1;
// 递归尝试向上下左右四个方向移动
if (dfs(maze, path, x-1, y, end_x, end_y) // 上
|| dfs(maze, path, x+1, y, end_x, end_y) // 下
|| dfs(maze, path, x, y-1, end_x, end_y) // 左
|| dfs(maze, path, x, y+1, end_x, end_y)) // 右
{
return true;
}
// 如果四个方向都不可行,则将当前位置从路径中移除,并返回上一步
path[x][y] = 0;
}
return false;
}
int main()
{
int m