jos*_*iel 19 java swing jpanel
你好,我是java编程的新手,我需要有人向我解释这些代码行:
public class drawpanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
...
}
}
Run Code Online (Sandbox Code Playgroud)
我不理解这一行public void paintComponent(Graphics g):为什么我必须声明这个函数,如果它是在JPanel中预定义的?
这一行super.paintComponent(g):我根本不明白.谢谢您的帮助.
The*_*eys 51
的extends关键字意味着DrawPanel从继承JPanel.换句话说,DrawPanel"是一个" JPanel.因此,它可以覆盖其方法(未标记的方法final).出于几个原因,您可能希望这样做.例如,您可能希望获得对面板Graphics类的访问权限,您可以使用该类在面板上绘制圆形,或条形图或文本字符串.
如果你没有覆盖任何方法,那么当你扩展时JPanel你得到这样的东西:
public class DrawPanel extends JPanel {
//TODO not much
}
Run Code Online (Sandbox Code Playgroud)
但是,这不是很有用......除非你只是不喜欢这个名字JPanel并且想要改称它AwesomePanel(注意:不要这样做).如果这就是你有,你就要去只是创建一个更好的实例中JPanel,像这样的:JPanel drawPanel = new JPanel();
扩展a的目的JPanel是覆盖该paintComponent方法.在JPanel你覆盖之前是不可见的paintComponent(注意:不可见是使它成为按钮和其他组件的有用容器).你是正确的,paintComponent方法是预定义的(JComponent如果你想知道在类中),但所有方法都是空的JPanel.如果你想在面板上绘制一些东西,那么你需要覆盖它,如下所示:
public class DrawPanel extends JPanel {
@Override public void paintComponent(Graphics g) { // <-- HERE!
//TODO draw stuff
}
}
Run Code Online (Sandbox Code Playgroud)
注意:该 @Override 部分不是严格必要的,但最好包含它,因为它减少了运行时错误的数量并提高了代码的可读性
您现在可以访问该面板的Graphics对象g.Graphics是一个帮助类,允许您在面板上绘制东西,如下所示:
public class DrawPanel extends JPanel {
@Override public void paintComponent(Graphics g) {
g.drawOval(50, 50, 50, 50); // <-- draws an oval on the panel
}
}
Run Code Online (Sandbox Code Playgroud)
有用的比喻(我刚刚编写): JPanel 是画布, Graphics 对象是你的画笔, super.paintComponent(g) 是你的橡皮擦.(另外, JFrame 是你的画架.)
因此super.paintComponent(g),paintComponent从JPanel(JComponent类)的超类调用方法来擦除当前在面板上绘制的任何内容.这对动画很有用.
例如,考虑在面板上绘制模拟时钟.你需要每秒刷新一次,所以每秒你必须擦除前一个时钟并重新绘制时钟,调整秒针.如果你super.paintComponent(g)在重新绘制时钟之前没有调用它,它将继续在旧时钟之上绘制新的时钟,并且在60秒内你将拥有的只是一个填充的圆圈,或多或少.
只需要记住一件事:总是super.paintComponent(g)先在paintComponent方法中调用,如下所示:
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g); // <-- HERE!
g.drawOval(50, 50, 50, 50);
}
}
Run Code Online (Sandbox Code Playgroud)
而已.随时联系我.
我创建了一个简单的示例,它使用这些概念在面板上显示一串文本(放在框架内).在IDE中保存为TestPanel.java.
import java.awt.*;
import java.util.*;
import javax.swing.*;
/**
* A frame containing a panel that is sometimes red and sometimes
* blue. Also, it displays the word to go with it.
*
* Inspired by www.sometimesredsometimesblue.com.
*
*/
public class TestPanel extends JPanel {
private Random random = new Random();
private boolean isRed;
private String s = "";
public TestPanel() {
//randomly determine whether the background should be red
isRed = random.nextBoolean();
//set the background to blue
setBackground(Color.BLUE);
s = "BLUE";
//if 'isRed' is true, set the background to red
if (isRed) {
setBackground(Color.RED);
s = "RED";
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//write either "RED" or "BLUE" using graphics
g.setColor(Color.WHITE);
g.setFont(new Font("serif", Font.BOLD, 60));
g.drawString(s, getWidth() / 2 - g.getFontMetrics().stringWidth(s) / 2,
getHeight() / 2 + g.getFontMetrics().getHeight() / 2);
}
//main method: create an instance of TestPanel and output it on a JFrame
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(500, 500);
f.setTitle("Sometimes Red, Sometimes Blue");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new TestPanel());
f.setVisible(true);
}
}Run Code Online (Sandbox Code Playgroud)