我正在尝试创建一个jframe并设置背景颜色,但也要设置一个抽绳。似乎每当我使用.add时,文本都会出现,但不会出现背景。没有.add,背景会改变,但不会改变文本。 `
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JPanel{
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 500);
g.setFont(font);
g.setColor(Color.red);
g.drawString("Hello", 300, 900);
}public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame();
jf.getContentPane().setBackground(Color.YELLOW);
jf.getContentPane().add(new Test());
jf.setSize(1920,1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
`
答案 0 :(得分:2)
设置测试JPanel的背景颜色,不 JFrame。
此外:
例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test3 {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(new MyTest("Hello", Color.RED));
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
}
class MyTest extends JPanel {
private static final int PREF_W = 1600;
private static final int PREF_H = 900;
private static final Color BG = Color.YELLOW;
private static final Font FONT = new Font("Serif", Font.PLAIN, 500);;
private String text;
private Color color;
MyTest(String text, Color color) {
this.text = text;
setBackground(BG);
this.color = color;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(FONT);
g.setColor(color);
g.drawString("Hello", 300, 600);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
答案 1 :(得分:2)
有几种方法可以做到这一点......
将Test
面板设为框架的内容面板...
JFrame jf = new JFrame();
jf.setContentPane(new Test());
jf.getContentPane().setBackground(Color.YELLOW);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
使Test
面板透明
JFrame jf = new JFrame();
Test test = new Test();
test.setOpaque(false);
jf.getContentPane().setBackground(Color.YELLOW);
jf.add(test);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
只需设置Test
面板的背景颜色
JFrame jf = new JFrame();
Test test = new Test();
test.setBackground(Color.YELLOW);
jf.add(test);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
无论你做什么......
super
方法paint
方法
paintComponent
paint
- 它更安全例如......
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 500);
g.setFont(font);
g.setColor(Color.red);
g.drawString("Hello", 300, 900);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame jf = new JFrame();
jf.setContentPane(new Test());
jf.getContentPane().setBackground(Color.YELLOW);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}