javagetforeground的简单介绍

用java设计和编写一个指针式时钟程序,应用线程实现时钟的走动,有的发我邮箱,361390264@qq.com,谢啦!!

package 时钟;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.util.Calendar;

import java.util.Date;

import javax.swing.JApplet;

/**

*

* @author Jiang

*/

public class Clock2 extends JApplet implements Runnable{

double PI=Math.PI;//圆周率

double hourlen=50;//时针长度

double minutelen=70;//分针长度

double secondlen=90;//秒针长度

int yuanxinx=100,yuanxiny=100;//圆心坐标

int banjin=100;//半径长度

int qix,qiy,zhongxh,zhongxm,zhongxs,zhongyh,zhongym,zhongys;//各指针的起点和终点坐标(起点一致)

Thread th;

BufferedImage buf;

int hour,minute,second;//记录当前时间

Calendar calendar;//用于获取当前时间

Graphics g1;//用于内存绘图

public void init(){

hour=minute=second=0;

buf=new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);//控制内存绘图区域大小

g1=buf.getGraphics();//创建缓冲绘图区

this.setSize(400,400);

}

public void start(){

if(th==null)

th=new Thread(this);

th.start();//启动线程

}

public void stop(){

th=null;//终止线程

}

public void run(){//线程的覆盖方法

while(true){

try{

th.sleep(1000);

}catch(Exception e){}

//用背景色填充绘图区,擦除上次绘图

g1.setColor(this.getBackground());

//g1.fillRect(yuanxinx-banjin,yuanxiny-banjin,2*banjin,2*banjin);

g1.fillRect(0,0,400,400);

repaint();

}

}

public void paint(Graphics g){

//将图像先画到缓冲区

g1.setColor(this.getForeground());

//绘制时钟刻盘(刻度长度为8)

g1.drawOval(yuanxinx-banjin,yuanxiny-banjin,yuanxinx+banjin,yuanxiny+banjin);

//特殊位置简便画法

//g1.drawLine(yuanxinx-banjin,yuanxiny,yuanxinx-banjin+8,yuanxiny);//9

//g1.drawLine(yuanxinx,yuanxiny-banjin,yuanxinx,yuanxiny-banjin+8);//12

//g1.drawLine(yuanxinx+banjin,yuanxiny,yuanxinx+banjin-8,yuanxiny);//3

//g1.drawLine(yuanxinx,yuanxiny+banjin,yuanxinx,yuanxiny+banjin-8);//6

//1

int qixx=(int)(yuanxinx-banjin*Math.cos((0.5+1.0/6)*PI));

int qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+1.0/6)*PI));

int zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+1.0/6)*PI));

int zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+1.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//2

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+2.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+2.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+2.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+2.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//3

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+3.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+3.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+3.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+3.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//4

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+4.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+4.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+4.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+4.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//5

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+5.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+5.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+5.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+5.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//6

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+6.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+6.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+6.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+6.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//7

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+7.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+7.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+7.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+7.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//8

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+8.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+8.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+8.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+8.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//9

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+9.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+9.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+9.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+9.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//10

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+10.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+10.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+10.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+10.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//11

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+11.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+11.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+11.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+11.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//12

qixx=(int)(yuanxinx-banjin*Math.cos((0.5+12.0/6)*PI));

qiyy=(int)(yuanxiny-banjin*Math.sin((0.5+12.0/6)*PI));

zhongxx=(int)(yuanxinx-(banjin-8)*Math.cos((0.5+12.0/6)*PI));

zhongyy=(int)(yuanxiny-(banjin-8)*Math.sin((0.5+12.0/6)*PI));

g1.drawLine(qixx,qiyy,zhongxx,zhongyy);

//g1.drawLine(150,15,140,25);

//g1.drawString(“”+qixx+” “+qiyy+” “+zhongxx+” “+zhongyy,200,300);

//获取当前时间

calendar=Calendar.getInstance();

hour=calendar.get(Calendar.HOUR);

minute=calendar.get(Calendar.MINUTE);

second=calendar.get(Calendar.SECOND);

//计算指针坐标

qix=qiy=yuanxinx;

zhongxh=(int)(yuanxinx-hourlen*Math.cos((0.5+2.0*hour/12)*Math.PI));

zhongyh=(int)(yuanxiny-hourlen*Math.sin((0.5+2.0*hour/12)*Math.PI));

zhongxm=(int)(yuanxinx-minutelen*Math.cos((0.5+2.0*minute/60)*Math.PI));

zhongym=(int)(yuanxiny-minutelen*Math.sin((0.5+2.0*minute/60)*Math.PI));

zhongxs=(int)(yuanxinx-secondlen*Math.cos((0.5+2.0*second/60)*Math.PI));

zhongys=(int)(yuanxiny-secondlen*Math.sin((0.5+2.0*second/60)*Math.PI));

//绘制指针

Font oldfont=this.getFont();

g1.setColor(Color.red);

g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+20));

g1.drawLine(qix,qiy,zhongxh,zhongyh);

g1.setColor(Color.green);

g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+15));

g1.drawLine(qix,qiy,zhongxm,zhongym);

g1.setColor(Color.blue);

g1.setFont(new Font(oldfont.getName(),Font.BOLD,oldfont.getStyle()+10));

g1.drawLine(qix,qiy,zhongxs,zhongys);

//输出时间、日期

g1.setFont(new Font(“Time New Roman”,Font.BOLD,25));

g1.setColor(Color.PINK);

g1.drawString(“”+hour+”:”+minute+”:”+second,150,240);

g1.setFont(new Font(“Time New Roman”,Font.BOLD,18));

g1.setColor(Color.green);

g1.drawString(“中国标准时间 Chinese Standard Time”,25,280);

g1.setFont(new Font(“Time New Roman”,Font.BOLD,20));

g1.setColor(Color.blue);

g1.drawString(new Date().toString(), 25,320);

//g1.drawString(“”+hour+” “+minute+” “+second, 200,200);

g.drawImage(buf, 0,0, this);//将缓冲区图像画到JApplet上

}

public void update(Graphics g){//覆盖方法

paint(g);

}

}

javagetforeground的简单介绍

java中如何从Color属性字符串中获取颜色Color

试试这个

int r=182;

int g=169;

int b=48;

textArea.setForeground(new Color(r,g,b));

java swing 滚动的虚线(蚂蚁线)

画虚线:

g2d.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[]{6.0f}, 0.0f));

g.setColor(lrcp.getForegroundColor());

g.drawLine(0, centerLine, getWidth(), centerLine);

java web 怎么在jtable中添加按钮?

java web在jtable中添加按钮的示例如下:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Dimension;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.io.File;

import javax.swing.BorderFactory;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.UIManager;

import javax.swing.border.Border;

import javax.swing.border.EmptyBorder;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.TableCellRenderer;

public class JTableButton extends JPanel {

 private JTable table;

 private JScrollPane scrollPane;

 private JButton[] buttons;

 private String path = System.getProperty(“user.dir”) + File.separator

 + “images” + File.separator;

 

 public JTableButton() {

  setBorder(BorderFactory.createLineBorder(Color.red, 1));

  init();

 }

 private void init() {

  String headName[] = { “Name”, “age”, “sex”, “adress”, “image” };

  

  buttons = new JButton[5];

  for(int i=0;ibuttons.length;i++){

   buttons[i] = new JButton(“”+i);

  }

  Object obj[][] = {

    { “LiMing”, 23, Boolean.TRUE, buttons[0],

      new ImageIcon(path + “icon.png”) },

    { “ZhangSan”, 25, Boolean.TRUE,buttons[1],

      new ImageIcon(path + “icon.png”) },

    { “WangWu”, 21, Boolean.FALSE, buttons[2],

      new ImageIcon(path + “icon.png”) },

    { “LiSi”, 28, Boolean.TRUE, buttons[3],

      new ImageIcon(path + “icon.png”) },

    { “LuBo”, 20, Boolean.FALSE, buttons[4],

      new ImageIcon(path + “icon.png”) }, };

  

  table = new JTable(new MyTableModel(headName,obj));

  table.setDefaultRenderer(JButton.class, new ComboBoxCellRenderer());

  scrollPane = new JScrollPane(table);

  setLayout(new BorderLayout());

  add(scrollPane, BorderLayout.CENTER);

  addHandler();

 }

 private void addHandler(){

  //添加事件

  table.addMouseListener(new MouseAdapter(){

   public void mouseClicked(MouseEvent e) {

    System.out.println(“table”);

    int row = table.getSelectedRow();

    int column = table.getSelectedColumn();

    System.out.println(“row=”+row+”:”+”column=”+column);

    if(column==3){

     //处理button事件写在这里…

     System.out.println(((JButton)table.getValueAt(row, column)).getText());

    }

   }

  });

 }

 public static void main(String[] args) {

  JFrame frame = new JFrame();

  frame.add(new JTableButton());

  frame.setSize(new Dimension(800, 400));

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }

 class MyTableModel extends AbstractTableModel {

  private String headName[];

  private Object obj[][];

  

  public MyTableModel() {

   super();

  }

  

  public MyTableModel(String[] headName, Object[][] obj) {

   this();

   this.headName = headName;

   this.obj = obj;

  }

  public int getColumnCount() {

   return headName.length;

  }

  public int getRowCount() {

   return obj.length;

  }

  public Object getValueAt(int r, int c) {

   return obj[r][c];

  }

  public String getColumnName(int c) {

   return headName[c];

  }

  public Class? getColumnClass(int columnIndex) {

   return obj[0][columnIndex].getClass();

  }

  @Override

  public boolean isCellEditable(int rowIndex, int columnIndex) {

   if (columnIndex == 3 || columnIndex == 4) {

    return false;

   }

   return true;

  }

 }

}

class ComboBoxCellRenderer implements TableCellRenderer {

 public Component getTableCellRendererComponent(JTable table, Object value,

   boolean isSelected, boolean hasFocus, int row, int column) {

  JButton cmb = (JButton) value;

  if (isSelected) {

   cmb.setForeground(table.getSelectionForeground());

   cmb.setBackground(table.getSelectionBackground());

  } else {

   cmb

     .setForeground((unselectedForeground != null) ? unselectedForeground

       : table.getForeground());

   cmb

     .setBackground((unselectedBackground != null) ? unselectedBackground

       : table.getBackground());

  }

  cmb.setFont(table.getFont());

  if (hasFocus) {

   cmb

     .setBorder(UIManager

       .getBorder(“Table.focusCellHighlightBorder”));

   if (!isSelected  table.isCellEditable(row, column)) {

    Color col;

    col = UIManager.getColor(“Table.focusCellForeground”);

    if (col != null) {

     cmb.setForeground(col);

    }

    col = UIManager.getColor(“Table.focusCellBackground”);

    if (col != null) {

     cmb.setBackground(col);

    }

   }

  } else {

   cmb.setBorder(noFocusBorder);

  }

  return cmb;

 }

 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

 private Color unselectedForeground;

 private Color unselectedBackground;

}

我想问一个java图形界面的问题。高手进。

自己写一个ListCellRenderer使得jlist可以显示成你图里那个样子。然后就是数据一个listModel删除另一个添加了。界面布局用netbeans拖出来。

public class A {

public static void main(String args[]) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(3);

JList list = new JList(new Integer[]{1, 2, 3, 4, 5, 6});

list.setCellRenderer(new MyCellRenderer());

JScrollPane panel = new JScrollPane(list);

frame.getContentPane().add(panel);

frame.setVisible(true);

}

static ImageIcon createIcon(Color color) {

BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_BGR);

Graphics2D g = image.createGraphics();

g.setColor(color);

g.fill(new Rectangle(0, 0, 10, 10));

return new ImageIcon(image);

}

static class MyCellRenderer extends JLabel implements ListCellRenderer {

final static ImageIcon longIcon = createIcon(Color.RED);

final static ImageIcon shortIcon = createIcon(Color.blue);

// This is the only method defined by ListCellRenderer.

// We just reconfigure the JLabel each time we’re called.

public Component getListCellRendererComponent(

JList list, // the list

Object value, // value to display

int index, // cell index

boolean isSelected, // is the cell selected

boolean cellHasFocus) // does the cell have focus

{

String s = value.toString();

setText(s);

setIcon((index % 2 == 0) ? longIcon : shortIcon);

if (isSelected) {

setBackground(list.getSelectionBackground());

setForeground(list.getSelectionForeground());

} else {

setBackground(list.getBackground());

setForeground(list.getForeground());

}

setEnabled(list.isEnabled());

setFont(list.getFont());

setOpaque(true);

return this;

}

}

}

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月25日 15:35:04
下一篇 2024年3月25日 15:43:14

相关推荐

  • c语言mallloc使用的简单介绍

    C语言中使用malloc必须加#includemallo.h? 1、在C语言中使用malloc函数进行动态内存分配。malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void malloc(unsigned int num_bytes);功能:分配长度为num_bytes字节的内存块。 2、你可以看一下C语言那本…

    2024年5月23日
    4400
  • javascriptcanvas的简单介绍

    如何使用js在画布上绘制图形 1、可以使用 drawImage()方法把一幅图像绘制到画布上。 以使用三种不同的参数组合。最简单的调用方式是传入一个 HTML 元素,以及绘制该图像的起点的 x 和 y 坐标。 2、效果图:使用JavaScript在画布中绘制文本图形首先我们来看看要在画布上绘制文本图形,需要用到的最重要的属性和方法:font属性:定义文本的字…

    2024年5月23日
    4200
  • cortexm4linux的简单介绍

    Cortex-M4的主要功能 Cortex-M4提供了无可比拟的功能,以将[1] 32位控制与领先的数字信号处理技术集成来满足需要很高能效级别的市场。 Cortex-M4核心具有浮点单元(FPU)单精度,支持所有Arm单精度数据处理指令和数据类型。它还实现了一套完整的DSP指令和一个提高应用程序安全性的内存保护单元(MPU)。 主要是m4比m3多了dsp的支…

    2024年5月23日
    4300
  • 3desjavaphp的简单介绍

    php的3des加密结果与java不一致 他们的加密算法都是通用的,是可以解开的,只要你des的模式,加密长度,初始向量什么的都一样就可以。 JAVA写RSA加密,私钥都是一样的,公钥每次加密的结果不一样跟对数据的padding(填充)有关。Padding(填充)属性定义元素边框与元素内容之间的空间。padding简写属性在一个声明中设置所有内边距属性。 要…

    2024年5月23日
    4700
  • 黑客代码软件学习推荐歌曲的简单介绍

    我想自学编程代码,,目地是“黑”网站,开发出破解代码。有没有这方面的… 这个迭代周期不应该以周为周期或以月为周期发生,而是应该以日为周期。知识等待使用的时间越久,知识这把斧头就越钝。等待学习新知识的时间越长,你就越难以将其融入到代码中。 我认为这个问题问得本身就显得有点矛盾,想学却担心自己看不懂代码学不来,试问哪个编程人员不是从零开始的。坚定信念…

    2024年5月23日
    4700
  • java8种基本类型范围的简单介绍

    java中常用的数据类型有哪些 1、java数据类型分为基本数据类型和引用数据类型,基本数据类型有boolean 、long 、int 、char、byte、short、double、float。引用数据类型有类类型、接口类型和数组类型。 2、java中包含的基本数据类型介绍:\x0d\x0aJava共支持8种内置数据类型。内置类型由Java语言预先定义好,…

    2024年5月23日
    4700
  • linux系统与gpt的关系的简单介绍

    linux下查看分区是不是gpt 看分区会报错,比如:WARNING: GPT (GUID Partition Table) detected on /dev/sda! The util fdisk doesnt support GPT. Use GNU Parted.所以这个sda就是gpt的。 选择“管理”;在“磁盘管理”中,右键“磁盘0”,在弹出的右键…

    2024年5月23日
    5400
  • 北京黑客学习培训的简单介绍

    现在学什么好啊 包括建筑设计、服装设计、珠宝首饰设计等,选择适合自己的设计专业,好好学习,就业容易且收入高。学前教育专业:很多女生喜欢小孩子,且女生一般细心且有耐心,教育行业假期宽裕,工作环境单纯,就业机会多。 现在比较热门好就业的专业有人工智能、机械专业、电子商务专业、人力资源专业、金融学专业、小语种类专业等等。人工智能:人工智能领域的研究包括机器人、语言…

    2024年5月23日
    4100
  • javaee要学那些东西的简单介绍

    java主要学习哪些内容 1、学java最重要的是下面四个内容:掌握Java语言的使用:语言语法、程序逻辑,OOP(面向对象)思想,封装、继承、多态,集合框架、泛型、File I\O技术,多线程技术、socket网络编程,XML技术。 2、Java基础:了解Java的基本语法、数据类型、控制流程、数组、字符串等基础概念。学习面向对象编程(OOP)的原则和概念…

    2024年5月23日
    4200
  • excel自杀的简单介绍

    excel表格中宏代码,具有自杀功能,密码输入错误3次就自动删除表格_百度… 我给楼主一个建议,利用excel自身的密码保护,保护此工作簿的结构,将sheet提前隐藏。这样如果有人将同一份excel打开时,如果不能输入正确的密码,就不能将sheet取消隐藏,就能达到楼主所说的保护了。 如果你还有源文件的话,可以这样试试:打开其他的excel文件,…

    2024年5月23日
    8100

发表回复

登录后才能评论



关注微信