📄 draw.java
字号:
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.*;
import javax.swing.*;
public class Draw extends JFrame implements ActionListener,ClipboardOwner{
static JFrame frame=new JFrame("表格画画");
Clipboard clipboard;
JPanel panel;
JTextArea out;
JTextField width,height,forcolor,baccolor,cellsp;
JButton change;
JButton copy;
JButton fresh;
JLabel label1,label2,label3,label4,label5;
JButton button[][]=new JButton[12][12];
int state[][]=new int[12][12];
Draw(){
clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
panel=new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(null);
out=new JTextArea(10,2);
width=new JTextField("120");
height=new JTextField("120");
forcolor=new JTextField("blue");
baccolor=new JTextField("yellow");
cellsp=new JTextField("1");
label1=new JLabel("宽度:");
label2=new JLabel("高度:");
label3=new JLabel("前景色:");
label4=new JLabel("背景色:");
label5=new JLabel("间距:");
label1.setBounds(0,330,30,30); width.setBounds(30,330,60,30);
label2.setBounds(90,330,30,30);
height.setBounds(120,330,60,30);
label3.setBounds(180,330,60,30);
forcolor.setBounds(220,330,60,30);
label4.setBounds(0,360,60,30);
baccolor.setBounds(50,360,60,30);
label5.setBounds(120,360,30,30);
cellsp.setBounds(150,360,60,30);
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5);
panel.add(width);
panel.add(height);
panel.add(forcolor);
panel.add(baccolor);
panel.add(cellsp);
for(int i=0;i<12;i++)
for(int j=0;j<12;j++)
{
button[i][j]=new JButton();
button[i][j].setBounds(30+j*20,30+i*20,20,20);
button[i][j].setBackground(Color.white);
state[i][j]=0;
panel.add(button[i][j]);
button[i][j].addActionListener(this);
}
out.setBounds(30,300,100,30);
panel.add(out);
change=new JButton("转换");
change.setBounds(150,300,60,30);
panel.add(change);
change.addActionListener(this);
copy=new JButton("复制");
copy.setBounds(210,300,60,30);
panel.add(copy);
copy.addActionListener(this);
fresh=new JButton("刷新");
fresh.setBounds(220,360,60,30);
panel.add(fresh);
fresh.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String output="<style>#id1{background-color:"+forcolor.getText()+";}#id2{background-color:"+baccolor.getText()+";}</style><table width="+width.getText()+" height="+height.getText()+" border=0 cellspacing="+cellsp.getText()+" cellpadding=0>";
for(int i=0;i<12;i++)
for(int j=0;j<12;j++)
{
if(e.getSource()==button[i][j]){
if(state[i][j]==0)
{
button[i][j].setBackground(Color.blue);
state[i][j]=1;
}
else
{
button[i][j].setBackground(Color.white);
state[i][j]=0;
}
}
}
if(e.getSource()==change)
{
for(int i=0;i<12;i++)
{
output+="<tr>";
for(int j=0;j<12;j++)
{
if(state[i][j]==1) output+="<td id=id1></td>";
else output+="<td id=id2></td>";
}
output+="</tr>";
}
output+="</table>";
out.setText(output);
}
if(e.getSource()==change)
{
StringSelection content=new StringSelection(output);
clipboard.setContents(content,this);
}
if(e.getSource()==fresh){
for(int i=0;i<12;i++)
for(int j=0;j<12;j++){
button[i][j].setBackground(Color.white);
state[i][j]=0;
}
}
}
public void lostOwnership(Clipboard clip,Transferable trans){}
public static void main(String args[]){
Draw a=new Draw();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,450);
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -