如上所述是不正确的,没有这样的方法,(我打字速度很快)并考虑了JPanels。
您可以做的是创建自己的方法Container
并覆盖该paint()
方法,然后将其用作您ContentPane
的方法frame.setContentPane(Container con)
:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SimpleAttributes extends JFrame {
SimpleAttributes() {
super("Simple Attributes");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
//setUndecorated(true);
setContentPane(new MyContainer());
getContentPane().setBackground(new Color(0, 200, 0, 0));
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SimpleAttributes();
}
});
}
}
class MyContainer extends Container {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.RED);
g2.drawString("One", 10, 10);
g.drawString("Two", 10, 40);
}
}
如对一个答案的评论中所述,您可以使用 的paint()
来JFrame
补偿对话框标题的偏移量:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SimpleAttributes extends JFrame {
SimpleAttributes() {
super("Simple Attributes");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
//setUndecorated(true);
getContentPane().setBackground(new Color(0, 200, 0, 0));
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.RED);
g2.drawString("One", 10, 10);//wont show
g2.drawString("One", 50, 50);//will show
g.drawString("Two", 40, 40);//will show
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SimpleAttributes();
}
});
}
}
but all of thats just going to give you more headaches why not just do it the preferred way? A JPanel
and override paintComponent(Graphics g);