import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Draw extends JFrame{
JPanel jPanel=new JPanel();
public Draw() {
jPanel.setBackground(Color.red);
add(jPanel);
Drawation drawaction=new Drawation();//添加画图,把上面jpanel的设置给覆盖了;要是先添加画图再添加
add(drawaction); //jpanel则把画图覆盖了
}
public static void main(String[] args){
Draw draw=new Draw();
draw.setTitle("abc");
draw.setSize(300,300);
draw.setVisible(true);
draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
class Drawation extends JPanel{
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawString("agagh", 50, 45);
}
}
这只是一段简单的验证代码,没什么意义。就是想了解怎么才能在动态画图的同时可以保留原有jpanel上的各种组件,而且可以在其上画图
import javax.swing.*;
import javax.swing.plaf.LayerUI;
import java.awt.*; public class JLayerSample { private static JLayer<JComponent> createLayer() {
// This custom layerUI will fill the layer with translucent green
// and print out all mouseMotion events generated within its borders
LayerUI<JComponent> layerUI = new LayerUI<JComponent>() { public void paint(Graphics g, JComponent c) {
// paint the layer as is
super.paint(g, c);
// fill it with the translucent green
g.setColor(new Color(0, 128, 0, 128));
g.fillRect(0, 0, c.getWidth(), c.getHeight());
} public void installUI(JComponent c) {
super.installUI(c);
// enable mouse motion events for the layer's subcomponents
((JLayer) c).setLayerEventMask(AWTEvent.MOUSE_MOTION_EVENT_MASK);
} public void uninstallUI(JComponent c) {
super.uninstallUI(c);
// reset the layer event mask
((JLayer) c).setLayerEventMask(0);
} // overridden method which catches MouseMotion events
public void eventDispatched(AWTEvent e, JLayer<? extends JComponent> l) {
System.out.println("AWTEvent detected: " + e);
}
};
// create a component to be decorated with the layer
JPanel panel = new JPanel();
panel.add(new JButton("JButton")); // create the layer for the panel using our custom layerUI
return new JLayer<JComponent>(panel, layerUI);
} private static void createAndShowGUI() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // work with the layer as with any other Swing component
frame.add(createLayer()); frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
是像下面这样吗?http://happy.host898.net/java/album/index.html
第一句调用 super.paintComponent(g) 就行
这个方法实现了双缓冲,不会覆盖 JPanel 上原有的控件
我说的和你写的那个意思有点区别,把你的类改了下,你看看效果import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Draw extends JFrame {
MainPane mainPane; public Draw() {
setTitle("abc");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setContentPane(getMainPane());
setVisible(true);
}
/**
* 初始化并返回底层面板,
* 你所有的其他控件都应该添加在这个底层面板上
* @return
*/
protected JPanel getMainPane() {
if (mainPane == null) {
mainPane = new MainPane();
mainPane.add(new JButton("1"));
mainPane.add(new JButton("2"));
mainPane.add(new JButton("3"));
mainPane.add(new JButton("4"));
mainPane.add(new JButton("5"));
mainPane.add(new JButton("6"));
}
return mainPane;
} public static void main(String[] args) {
new Draw();
}
class MainPane extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
int width = this.getWidth();
int height = this.getHeight();
g2d.setPaint(new GradientPaint(0, 0, Color.white,
width, height, Color.cyan));
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.black);
g.drawString("agagh", 50, 45);
}
}
}
firstly before you posted the thread!!
i could not catch on what you said at all...the intent of the comments is not to beat you
just beat someone who regard others as a fool
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Draw extends JFrame{ private JPanel jPanel=new JPanel();
Graphics g=jPanel.getGraphics();
public Draw() {
g.drawString("agagh", 50, 45);
jPanel.add(new JButton("d"));
setContentPane(jPanel);
}
public static void main(String[] args){
Draw draw=new Draw();
draw.setTitle("赵威制作");
draw.setSize(300,300);
draw.setVisible(true);
draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
}