为什么setBackground没有起作用的呢。

本文介绍了一个使用Java Swing的JApplet绘制雪人的示例程序。程序通过设置背景色和利用fillRect、fillOval等方法绘制地面、太阳、雪人身体各部位及帽子等元素,展示了基本的绘图技巧。

//下面是java程序设计教程中的例子程序。不过运行这个applet小程序背景颜色没有改变。

import javax.swing.JApplet;
import java.awt.*;

public class Snowman extends JApplet
{
 public void paint(Graphics page)
 {
  final int MID = 150;
  final int TOP = 50;
 
  setBackground(Color.cyan);

  page.setColor (Color.blue);
  page.fillRect(0,175,300,50);//ground

  page.setColor (Color.yellow);
  page.fillOval (-40,-40,80,80);//sun

  page.setColor (Color.white);
  page.fillOval (MID-20,TOP,40,40);//head
  page.fillOval (MID-35,TOP+35,70,50);//upper torso
  page.fillOval (MID-50,TOP+80,100,60);//lower torso

  page.setColor (Color.black);
  page.fillOval (MID-10,TOP+10,5,5);//left eye
  page.fillOval (MID+5,TOP+10,5,5);//right eye

  page.drawArc (MID-10,TOP+20,20,10,190,160); //smile
  page.drawLine (MID-25,TOP+60,MID-50,TOP+40); //left arm
  page.drawLine (MID+25,TOP+60,MID+55,TOP+60); //right arm

  page.drawLine (MID-20,TOP+5,MID+20,TOP+5); //brim of hat
  page.drawRect (MID-15,TOP-20,30,25); //top of hat
 }
}

如果WindowStyle属性设置为None没有隐藏窗口的边框和标题栏,可能是因为应用程序的框架不允许窗口样式被覆盖。在这种情况下,可以尝试使用以下方法: 1. 在App.xaml文件中添加以下样式: ```xaml <Style TargetType="Window"> <Setter Property="WindowStyle" Value="None"/> <Setter Property="AllowsTransparency" Value="True"/> <Setter Property="Background" Value="Transparent"/> </Style> ``` 这个样式将应用于所有的窗口,它将WindowStyle属性设置为None,AllowsTransparency属性设置为True,以及Background属性设置为Transparent。这将导致窗口被渲染为透明的,从而隐藏窗口的边框和标题栏。 2. 在Window的Loaded事件处理程序中使用Win32 API来隐藏窗口的边框和标题栏。示例代码如下: ```c# using System.Runtime.InteropServices; using System.Windows; namespace WpfApp1 { public partial class MainWindow : Window { [DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); private const int GWL_STYLE = -16; private const int WS_SYSMENU = 0x80000; private const int WS_MINIMIZEBOX = 0x20000; private const int WS_MAXIMIZEBOX = 0x10000; private const int WS_THICKFRAME = 0x40000; private const uint SWP_NOSIZE = 0x0001; private const uint SWP_NOMOVE = 0x0002; private const uint SWP_NOACTIVATE = 0x0010; private readonly IntPtr HWND_TOPMOST = new IntPtr(-1); public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { // Hide the window border and title bar IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle; int style = GetWindowLong(hwnd, GWL_STYLE); SetWindowLong(hwnd, GWL_STYLE, style & ~(WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME)); // Set the window position to cover the entire screen SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, (int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE); } } } ``` 这个方法使用SetWindowLong函数来移除窗口的菜单、最小化和最大化按钮、改变窗口大小的边框等,并使用SetWindowPos函数将窗口移动到整个屏幕的位置。这将导致窗口的边框和标题栏被隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值