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