java问题,JPanel放入JFrame显示问题
packagegraphics;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjav...
package graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
@SuppressWarnings("serial")
public class GraphicsPanel extends JPanel {
public GraphicsPanel() {
this.setLayout(null);
this.setBackground(Color.white);
this.setBorder(new LineBorder(Color.BLUE));
this.setBounds(50, 20, 501, 501);
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
});
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// g2.setColor(Color.BLUE);
// g2.drawRect(0, 0, 500, 500);
Stroke dash = new BasicStroke(1f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL, 3f, new float[] {1,2}, 0f);
g2.setStroke(dash);
g2.setColor(new Color(188, 188, 188));
g2.drawLine(0, 100, 500, 100);
g2.drawLine(0, 200, 500, 200);
g2.drawLine(0, 300, 500, 300);
g2.drawLine(0, 400, 500, 400);
g2.drawLine(100, 0, 100, 500);
g2.drawLine(200, 0, 200, 500);
g2.drawLine(300, 0, 300, 500);
g2.drawLine(400, 0, 400, 500);
}
}
这是我写的一个类继承了JPanel,然后把该类的布局设置为null,最后把该类放入JFrame中,JFrame的布局也设置成了null,问题是:我对该类设置了边界,即this.setBorder(new LineBorder(Color.BLUE)); 但是该边界在JFrame中显示不出来,就是在JFrame中对该类设置边界还是显示不出来,不知道是怎么回事??? 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
@SuppressWarnings("serial")
public class GraphicsPanel extends JPanel {
public GraphicsPanel() {
this.setLayout(null);
this.setBackground(Color.white);
this.setBorder(new LineBorder(Color.BLUE));
this.setBounds(50, 20, 501, 501);
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
});
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// g2.setColor(Color.BLUE);
// g2.drawRect(0, 0, 500, 500);
Stroke dash = new BasicStroke(1f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL, 3f, new float[] {1,2}, 0f);
g2.setStroke(dash);
g2.setColor(new Color(188, 188, 188));
g2.drawLine(0, 100, 500, 100);
g2.drawLine(0, 200, 500, 200);
g2.drawLine(0, 300, 500, 300);
g2.drawLine(0, 400, 500, 400);
g2.drawLine(100, 0, 100, 500);
g2.drawLine(200, 0, 200, 500);
g2.drawLine(300, 0, 300, 500);
g2.drawLine(400, 0, 400, 500);
}
}
这是我写的一个类继承了JPanel,然后把该类的布局设置为null,最后把该类放入JFrame中,JFrame的布局也设置成了null,问题是:我对该类设置了边界,即this.setBorder(new LineBorder(Color.BLUE)); 但是该边界在JFrame中显示不出来,就是在JFrame中对该类设置边界还是显示不出来,不知道是怎么回事??? 展开
展开全部
有两种改法:
第一种你这样的覆盖 paint 方法的话,需要自己来画边框,甚至还要处理皮肤效果相关的参数,否则在其它像 Linux 操作系统上看起来就很怪异的。
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Stroke dash = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 3f, new float[] { 1, 2 }, 0f);
g2.setStroke(dash);
g2.setColor(new Color(188, 188, 188));
g2.drawLine(0, 100, 500, 100);
g2.drawLine(0, 200, 500, 200);
g2.drawLine(0, 300, 500, 300);
g2.drawLine(0, 400, 500, 400);
g2.drawLine(100, 0, 100, 500);
g2.drawLine(200, 0, 200, 500);
g2.drawLine(300, 0, 300, 500);
g2.drawLine(400, 0, 400, 500);
getBorder().paintBorder(this, g2, 0, 0, this.getWidth(), this.getHeight());
}
还有一种也是推荐的做法是覆盖 paintComponent 这样边框就不是我们的职责了。
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Stroke dash = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 3f, new float[] { 1, 2 }, 0f);
g2.setStroke(dash);
g2.setColor(new Color(188, 188, 188));
g2.drawLine(0, 100, 500, 100);
g2.drawLine(0, 200, 500, 200);
g2.drawLine(0, 300, 500, 300);
g2.drawLine(0, 400, 500, 400);
g2.drawLine(100, 0, 100, 500);
g2.drawLine(200, 0, 200, 500);
g2.drawLine(300, 0, 300, 500);
g2.drawLine(400, 0, 400, 500);
}
追问
谢谢,已经解决了,在paint方法中加上super.paint(g);调用一下父类的paint方法就行了,不过你说的第二种方法也很好!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询