如何在不取消Java中的其他内容的情况下设置背景颜色和drawString?

时间:2017-04-19 01:56:29

标签: java swing jframe jpanel

我正在尝试创建一个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);
    }
}

`

2 个答案:

答案 0 :(得分:2)

设置测试JPanel的背景颜色, JFrame。

此外:

  • 覆盖paintComponent,绘制
  • 别忘了在你的覆盖中调用超级绘画方法
  • 学习并遵循Java命名和代码格式约定。您的代码很难阅读和理解,因为它是当前编写的。

例如:

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 - 它更安全
  • 仅从Event Dispatching Thead
  • 的上下文中操纵UI

例如......

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);
            }
        });
    }
}