首页上一页 1 下一页尾页 8 条记录 1/1页
Graphics2D g = (Graphics2D) g1;强制转换错误,这个要怎么弄
发表在Java图书答疑
2020-04-12
悬赏:2 学分
《零基础学Java》第11章 Swing程序设计
是否精华
是
否
版块置顶:
是
否
package text;
import java.awt.*;
import javax.swing.*;
// 带背景的面板组件
class ShadePanel extends JPanel {
public ShadePanel() { // 构造方法
super();
setLayout(null); // 无布局
}
protected void paintComponent(Graphics g1) { // 重写绘制组件外观
Graphics2D g = (Graphics2D) g1;
super.paintComponent(g); // 执行超类方法
int width = getWidth(); // 获取组件大小
int height = getHeight(); // 获取组件大小
// 创建填充模式对象
GradientPaint paint = new GradientPaint(0, 0, Color.cyan, 0, height, Color.magenta);
g.setPaint(paint); // 设置绘图对象的填充模式
g.fillRect(0, 0, width, height); // 绘制矩形填充控制界面
}
}
public class ShadeBackgroundImage extends JFrame {
private JPanel contentPane;
public ShadeBackgroundImage() {
setTitle("背景为渐变色的主界面"); // 设置窗体标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel(); // 创建内容面板
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
ShadePanel shadePanel = new ShadePanel(); // 创建内容面板
contentPane.add(shadePanel, BorderLayout.CENTER); // 添加面板到窗体内容面板
}
public static void main(String[] args) {
new ShadeBackgroundImage().setVisible(true);
}
}于2020-04-12 19:32:38编辑

购物车
发表新帖
立即签到












