解决方案:

//通过改写 原始对象的paint 方法,来设置对象的border颜色

1.

package test1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class MyFrame {
 public static void main(String[] args) {
 JFrame frame1 = new JFrame();
 frame1.setBounds(400, 300, 200, 200);
 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 JPanel panel = new JPanel();
 panel.setBorder(new LineBorder(Color.red));
 frame1.add(panel);
 frame1.setVisible(true);

 JFrame frame2 = new JFrame() {
    
// public void paint(Graphics g) {
// super.paint(g);
// Rectangle rect = this.getBounds();
// int width = (int) rect.getWidth() - 1;
// int height = (int) rect.getHeight() - 1;
// g.setColor(Color.red);
// g.drawRect(0, 0, width, height);
// }
 //通过改写 原始对象的paint 方法,来设置对象的border颜色
 public void paint(Graphics g) {
 super.paint(g);
 Rectangle rect = this.getBounds();
 int width = (int) rect.getWidth() - 1;
 int height = (int) rect.getHeight() - 1;
 g.setColor(Color.red);
 g.drawRect(0, 0, width, height);
 }
 };
 frame2.setBounds(650, 300, 200, 200);
 frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame2.setUndecorated(true);//隐藏标题栏 按钮
 frame2.setVisible(true);
 }
}

2.

package test1;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.FileWriter;

import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

public class Pic {

    private JFrame frmDrag;
    private JTextField textField;
    private final ButtonGroup buttonGroup = new ButtonGroup();
    String str1 = null, str2 = null;
//    private JTextField textField_1;
//    private final Action action = new SwingAction();
    private Font font1 =new Font("Microsoft YaHei", Font.CENTER_BASELINE, 12);
    private Font font2 =new Font("Microsoft YaHei", Font.BOLD, 16);
    static FileWriter fw ;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Pic window = new  Pic();
                    window.frmDrag.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the application.
     */
    public Pic() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmDrag = new JFrame();
//        frmDrag.setResizable(false);
        frmDrag.setResizable(true);
//        frmDrag.getContentPane().setBackground(new Color(0, 255, 0));
        frmDrag.getContentPane().setBackground(Color.GREEN);
        //windows green background
        //? error 被panel 设置的颜色覆盖
        
//        frmDrag.setBackground(new Color(0, 0, 255));
//        frmDrag.setForeground(Color.BLUE);
        frmDrag.setTitle("拼图游戏:Made by Xgqfrms");
        frmDrag.setFont(font1);
        frmDrag.setBounds(10, 10, 800, 600);
        frmDrag.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmDrag.getContentPane().setLayout(null);

        JPanel panel1 = new JPanel();
//        panel1.setBackground(new Color(125, 125, 125));
        panel1.setBackground(Color.PINK);
        panel1.setBounds(0, 0, 600, 75);
        frmDrag.getContentPane().add(panel1);
        panel1.setLayout(null);
        
        JLabel label_0 = new JLabel("按钮区");
        label_0.setHorizontalAlignment(SwingConstants.CENTER);
        label_0.setFont(font2);
        label_0.setBounds(0, 0, 50, 30);
        label_0.setForeground(Color.BLACK);
        panel1.add(label_0);
        
//        JLabel label = new JLabel("\u59D3\u540D:");
        JLabel label = new JLabel("数字提示");
        label.setHorizontalAlignment(SwingConstants.CENTER);
//        label.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        label.setFont(font2);        
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBounds(41, 35, 80, 30);//坐标(x,y,width,length)
        label.setForeground(Color.BLACK);
        panel1.add(label);

        JLabel label_1 = new JLabel("清除提示");
        label_1.setHorizontalAlignment(SwingConstants.CENTER);
        label_1.setFont(font2);
        label_1.setBounds(141, 35, 80, 30);
        label_1.setForeground(Color.BLACK);
        panel1.add(label_1);
        
        JLabel label_2 = new JLabel("选择图片:");
        label_2.setHorizontalAlignment(SwingConstants.CENTER);
        label_2.setFont(font2);
        label_2.setBounds(250, 35, 80, 30);
        label_2.setForeground(Color.BLACK);
        panel1.add(label_2);
        
        JRadioButton radioButton = new JRadioButton("");
        buttonGroup.add(radioButton);// 按钮组
        radioButton.setSelected(true);
//        radioButton.setSelected(false);
        radioButton.setBounds(130, 38, 20, 20);
        radioButton.setBackground(Color.PINK);
        panel1.add(radioButton);

        JRadioButton radioButton_1 = new JRadioButton("");
        buttonGroup.add(radioButton_1);
        radioButton_1.setSelected(false);
        radioButton_1.setBounds(30, 38, 20, 20);
        radioButton_1.setBackground(Color.PINK);
        panel1.add(radioButton_1);
        

        textField = new JTextField();
        textField.setBounds(330, 38, 100, 30);
        textField.setForeground(Color.red);
        textField.setBackground(Color.green);
        panel1.add(textField);
        textField.setColumns(8);
//通过改写 原始对象的paint 方法,来设置对象的border颜色      

        JButton button = new JButton("Start"){
            public void paint(Graphics g) {
                 super.paint(g);
                 Rectangle rect = this.getBounds();
                 int width = (int) rect.getWidth() - 1;
                 int height = (int) rect.getHeight() - 1;
                 g.setColor(Color.red);
                 g.drawRect(0, 0, width, height);
                 }
        };
//        button.setAction(action);
        button.setBounds(435, 38, 70, 30);
        button.setBorderPainted(true);//没什么效果呀
//        button.setName("Start");
        button.setFont(font1);
//        Border border1;
//        button.setBorder(border1.paintBorder(c, g, 0, 0, 70, 30));
//        void javax.swing.border.Border.paintBorder(Component c, Graphics g, int x, int y, int width, int height)
        panel1.add(button);
    
        Cursor cursor = new Cursor(12);// 光标样式
    }
  

}

通过改写 原始对象的paint 方法,来设置对象的border颜色的更多相关文章

  1. AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到

    AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到

  2. 使用 jQuery 基本选择器获取页面元素,然后利用 jQuery 对象的 css() 方法动态设置 <span> 和 <a> 标签的样式

    查看本章节 查看作业目录 需求说明: 使用 jQuery 基本选择器获取页面元素,然后利用 jQuery 对象的 css() 方法动态设置 <span> 和 <a> 标签的样式 ...

  3. ADO.NET笔记——使用Connection连接数据库,使用Command对象的ExecuteReader()方法创建DataReader对象返回多行数据

    使用Connection连接数据库,使用DataReader访问数据库,并返回多行数据. 相关步骤: 需要引入两个命名空间 using System.Data; using System.Data.S ...

  4. Effective java笔记(二),所有对象的通用方法

    Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类( ...

  5. Javascript对象属性与方法汇总

    Javascript对象属性与方法汇总 发布时间:2015-03-06 编辑:www.jquerycn.cn 详细介绍下,javascript对象属性与对象方法的相关知识,包括javascript字符 ...

  6. JAVA-JSP内置对象之request对象的其他方法

    相关资料:<21天学通Java Web开发> request对象的其他方法1.request对象除了可以用来获得请求参数,还可以用来获得HTTP标头及其他信息. 方法           ...

  7. JavaScript RegExp对象的exec()方法

    JavaScript RegExp对象的exec()方法用来匹配字符串,它的行为与match()有些不同. 对于RegExpObject.exec(),w3school上面是这样介绍的: exec() ...

  8. 解决vuejs 创建数据后设置对象的属性实现不了双向绑定问题

    抛出踩坑:vue创建后的数据,自定义设置对象的属性,实现不了双向绑定 当业务场景,需要在请求接口数据新增自定义的属性 let foodList = [ {title: '回锅肉', price: 99 ...

  9. ES6学习笔记(七)对象的新增方法

    1.Object.is() ES5 比较两个值是否相等,只有两个运算符:相等运算符(==)和严格相等运算符(===).它们都有缺点,前者会自动转换数据类型,后者的NaN不等于自身,以及+0等于-0.J ...

随机推荐

  1. Linux定时任务crontab通俗易懂简单扼要地解析

    1.安装crontab 在配置好yum源的情况下,直接执行如下命令即可: yum install crontab 2.查看当前环境上已经有的定时任务有哪些? 执行如下命令即可 crontab -l 如 ...

  2. 树莓派做私有云盘-极简版(owncloud)

    这里直接给出配置好私有云的镜像,只需烧录镜像后微改配置后即可使用 链接:https://pan.baidu.com/s/1EOQaSQso-0wmnuWgZKknZg提取码:q26h 1.直接将此镜像 ...

  3. 手把手做一个基于vue-cli的组件库(上篇)

    基于vue-cli4的ui组件库,先贴个最终效果吧,步骤有点多,准备分上下篇,上篇:如何做一个初步的组件.下篇:编写说明文档及页面优化.开工. GitHub源码地址:https://github.co ...

  4. (转载)微软数据挖掘算法:Microsoft顺序分析和聚类分析算法(8)

    前言 本篇文章继续我们的微软挖掘系列算法总结,前几篇文章已经将相关的主要算法做了详细的介绍,我为了展示方便,特地的整理了一个目录提纲篇:大数据时代:深入浅出微软数据挖掘算法总结连载,有兴趣的童鞋可以点 ...

  5. oracle创建表并加索引

    一个语句创建Oracle所有表的序列 -- 动态创建序列 2 declare 3 cursor c_job is 4 select TABLE_NAME from user_tables; 5 6 c ...

  6. list中map 的value值时间排序

    public static void main(String[] args) { String sys=DateUtil.getTime().substring(0,5); System.out.pr ...

  7. innodb和myisam原理

    MyISAM索引实现 MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.如图:  这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索引 ...

  8. Flash 终将谢幕:微软将于年底停止对 Flash 的支持

    近日,微软宣布将于今年 12 月终止对 Adobe Flash Player 的支持,届时,微软旗下所有浏览器都将无法使用 Flash,Adobe 也不会在今年 12 月后发布安全更新.早在 2017 ...

  9. ASP.NET Core 5.0 MVC中的 Razor 页面 介绍

    Razor 是一个用于将基于服务器的代码嵌入到网页中的标记语法. Razor语法由 Razor 标记.c # 和 HTML 组成. 通常包含 Razor 的文件的扩展名 cshtml Razor 语法 ...

  10. 客户端负载均衡Ribbon

    客户端负载均衡Ribbon 一.Ribbon是什么 二.Ribbon实现客户端负载均衡 三.Ribbon负载均衡策略 四.Rest请求模板类解读 4.1 RestTemplate的GET请求 第一种: ...