Java 写了一个面板jpanel加到add。jframe 的对象的上方。能显示。后来用绘图

2025-01-02 18:30:56
推荐回答(4个)
回答1:

可以是用add方法添加其他组件,也可以重写JPanel的下面的方法画
@Override
public void paint(Graphics g) {
super.paint(g);
//你的代码
}

回答2:

public class DrawFrame {

public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.RED);
JTable table = new JTable(4, 4) {

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
Composite com = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
g2d.setColor(Color.RED);
g2d.fill(new Rectangle(0, 0, this.getWidth(), getHeight()));
g2d.setComposite(com);
g2d.setColor(Color.BLACK);
}
};
frame.getContentPane().add(table, "Center");
frame.setVisible(true);
}
}
Table上有一层半透明的红色层。

回答3:

只要建 一个类就可以了,类名是Demo
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo {
static JFrame jf = new JFrame("画圆");
static JPanel jp;
public static void main(String[] args) {
jp = new MyPanel();
jp.setBounds(0,0,500,500);
jf.add(jp);
jf.setSize(500, 500);
jf.setLayout(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class MyPanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
//画
g.drawOval(10, 10, 100, 100);
}
}

回答4:

看说明书吧