为什么Jpanel不能显示画线?高手请解答一下
importjava.awt.FlowLayout;importjava.awt.Graphics;importjava.awt.Point;importjava.awt...
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JieMian extends JFrame {
Mypanel p = null;
public JieMian(String title) {
super(title);
p = new Mypanel();
p.setBounds(30, 30, 220, 220);
this.setLayout(null);
this.add(p);
this.setBounds(300, 300, 300, 300);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
}
class Mypanel extends JPanel {
private DrawGrid dg = null;
JButton b = new JButton("画");
public Mypanel() {
super();
dg = new DrawGrid(new Point(10, 10), new Point(80, 80));
this.setLayout(new FlowLayout());
this.add(b);
b.addActionListener(new Monitor());
}
class Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
dg = new DrawGrid(new Point(50, 50), new Point(80, 80));
repaint();
}
}
public void Paint(Graphics g) {
dg.drawline(g);
}
}
public class DrawGrid {
private Point startP;
private Point endP;
public DrawGrid(Point startP,Point endP){
this.startP = startP;
this.endP = endP;
}
public void drawline(Graphics g){
g.drawLine(startP.x, startP.y, endP.x, endP.y);
}
}
public class Test {
public static void main(String[] args) {
new JieMian("TETRIS");
}
}
新手,请大哥们帮帮忙。谢谢 展开
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JieMian extends JFrame {
Mypanel p = null;
public JieMian(String title) {
super(title);
p = new Mypanel();
p.setBounds(30, 30, 220, 220);
this.setLayout(null);
this.add(p);
this.setBounds(300, 300, 300, 300);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
}
class Mypanel extends JPanel {
private DrawGrid dg = null;
JButton b = new JButton("画");
public Mypanel() {
super();
dg = new DrawGrid(new Point(10, 10), new Point(80, 80));
this.setLayout(new FlowLayout());
this.add(b);
b.addActionListener(new Monitor());
}
class Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
dg = new DrawGrid(new Point(50, 50), new Point(80, 80));
repaint();
}
}
public void Paint(Graphics g) {
dg.drawline(g);
}
}
public class DrawGrid {
private Point startP;
private Point endP;
public DrawGrid(Point startP,Point endP){
this.startP = startP;
this.endP = endP;
}
public void drawline(Graphics g){
g.drawLine(startP.x, startP.y, endP.x, endP.y);
}
}
public class Test {
public static void main(String[] args) {
new JieMian("TETRIS");
}
}
新手,请大哥们帮帮忙。谢谢 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询