public void paintComponent(Graphics g){
g.setColor(Color.red);
g.drawLine(100, 200, 100, 300);
System.out.println(123);
}在线程里面 repaint()
为什么不行 repaint() 没有反应
g.setColor(Color.red);
g.drawLine(100, 200, 100, 300);
System.out.println(123);
}在线程里面 repaint()
为什么不行 repaint() 没有反应
布局管理器,主要应用了一下几种。去看看吧
|BorderLayout
|FlowLayout
|GridLayout
|CardLayout
|GridBagLayout
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;public class PlayFrame extends JFrame{
private static final long serialVersionUID = 1L;
//游戏窗口的大小与位置 private static int locationX = 200; private static int locationY = 100;
private int sizeX = 1000,sizeY = 600;
// 定义面板
JPanel jl = new JPanel();
PlayPanel pp = new PlayPanel();// 构造函数
public PlayFrame(){
jl.setLayout(new FlowLayout());
add(pp,BorderLayout.CENTER);
add(jl,BorderLayout.SOUTH);
frame();
}
// 窗口的属性
public void frame(){
setTitle("僵尸大战");
setLocation(locationX, locationY);
setSize(sizeX, sizeY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true); pp.start();
}
}package com.plants.vs.zombies.Frame;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;// 游戏窗口的面板
public class PlayPanel extends JPanel {
private static final long serialVersionUID = 1L; private int locationX = 200, locationY = -100;
static JInternalFrame[] frameSon;
JDesktopPane jp = new JDesktopPane(); SonPanel sonPanel;
int index = 0; public PlayPanel() {
frameSon = new JInternalFrame[100];
setLayout(new BorderLayout());
add(jp, BorderLayout.CENTER);
} // 重画
public void paint(Graphics g){
System.out.println(123); }
// 添加子窗口
public void addInternalWindow() { if (index < 100) {
frameSon[index] = new JInternalFrame("选择战士");
frameSon[index].setSize(335, 100);
frameSon[index].setLocation(locationX, locationY);
frameSon[index].setResizable(true);
frameSon[index].add(new SonPanel(),BorderLayout.CENTER);
jp.add(frameSon[index], index);
frameSon[index].setVisible(true);
if (index >= 1) {
frameSon[index - 1].setVisible(false);
} ++index;
locationY++; }
} // 删除子窗口
public void deleteInternalWindow() {
if (index > 0) {
--index;
frameSon[index].setVisible(false);
jp.remove(frameSon[index]);
}
} public static JInternalFrame[] getFrameSon() {
return frameSon;
} public static void setFrameSon(JInternalFrame[] frameSon) {
PlayPanel.frameSon = frameSon;
} // 线程刷新
private class MyThread implements Runnable { public void run() { while (true) {
repaint();
addInternalWindow();
if (PlayPanel.frameSon[99] != null
&& PlayPanel.frameSon[99].getLocation().x != 200) {
PlayPanel.frameSon[99].setVisible(false);
PlayPanel.frameSon[99].setLocation(200, 0); }
if (PlayPanel.frameSon[99] != null
&& PlayPanel.frameSon[99].getLocation().x == 200) {
PlayPanel.frameSon[99].setVisible(true);
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} } public void start() {
new Thread(new MyThread()).start();
}
private class SonPanel extends JPanel{
public void paintComponent(Graphics g){
for(int x = 50; x<350 ; x += 55){
for(int y = 0;y<100;y += 5){
g.draw3DRect(x, y, 5, 5, true);
}
}
}
}
}main 在一边 没弄上来~~ main 就只调用了PlayFrame 看的时候 排除子窗口不看 就是一个paintComponent 不过不能 重画~~ 重画的 反而是 子窗口面板的 怎回事 怎样改~~~???