import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DrawingBoard extends JFrame{
int[] xPoint={200,300,500};
int[] yPoint={100,180,240};
static int m=15;
static Color color=new Color(255,0,0);
private JComboBox jcb2=new JComboBox(new Object[]{
" 20"," 28"," 36"," 44"," 52"," 72 "
});
private JComboBox jcb3=new JComboBox(new Object[]{
"紅色","黑色","藍色","綠色"
});
public DrawingBoard(){
JMenuBar Bar=new JMenuBar();
JMenu jmFile=new JMenu("文件");
JMenuItem jmi1=new JMenuItem("新建");
JMenuItem jmi2=new JMenuItem("打開");
JMenuItem jmi3=new JMenuItem("保存");
JMenuItem jmi4=new JMenuItem("退出");
jmFile.add(jmi1);
jmFile.add(jmi2);
jmFile.add(jmi3);
jmFile.add(jmi4);
JLabel jlb1=new JLabel("線條");
JLabel jlb2=new JLabel("線寬");
JLabel jlb3=new JLabel("顏色");
final JComboBox jcb1=new JComboBox(new Object[]{
"實線","虛線"
});
JButton jbt0=new JButton("直線");
JButton jbt1=new JButton("三角形");
JButton jbt2=new JButton("圓");
JButton jbt3=new JButton("矩形");
JPanel jp1=new JPanel();
jp1.add(jlb1);
jp1.add(jcb1);
jp1.add(jlb2);
jp1.add(jcb2);
jp1.add(jlb3);
jp1.add(jcb3);
jp1.add(jbt0);
jp1.add(jbt1);
jp1.add(jbt2);
jp1.add(jbt3);
final DrawingPanel jp2=new DrawingPanel();
jp2.setBackground(Color.white);
jp2.setPreferredSize(new Dimension(736,300));
this.add(Bar);
this.add(jp1,BorderLayout.CENTER);
this.add(jp2,BorderLayout.SOUTH);
jcb1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb1.getSelectedItem()=="實線"){
Graphics g=jp2.getGraphics();
g.drawLine(10,10,736,10);
}
if(jcb1.getSelectedItem()=="虛線"){
Graphics g=jp2.getGraphics();
for(int i=1;i<100;i++){
g.drawLine(6*i-6,50,6*i-3,50);
}
}
}
});
jcb2.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb2.getSelectedItem()==" 20"){
m=20;
repaint();
}
if(jcb2.getSelectedItem()==" 28"){
m=28;repaint();
}
if(jcb2.getSelectedItem()==" 36"){
m=36;repaint();
}
if(jcb2.getSelectedItem()==" 44"){
m=44;repaint();
}
if(jcb2.getSelectedItem()==" 52"){
m=52;repaint();
}
if(jcb2.getSelectedItem()==" 72 "){
m=72;repaint();
}
}
});
jcb3.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb3.getSelectedItem()=="紅色"){
color=new Color(255,0,0);
repaint();
}
if(jcb3.getSelectedItem()=="藍色"){
color=new Color(0,0,255);
repaint();
}
if(jcb3.getSelectedItem()=="黑色"){
color=new Color(0,0,0);
repaint();
}
if(jcb3.getSelectedItem()=="綠色"){
color=new Color(0,128,0);
repaint();
}
}
});
jbt0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics g=jp2.getGraphics();
g.drawLine(10,10,736,10);
}
});
jbt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics grap=jp2.getGraphics();//創建所畫組件對象
grap.drawPolygon(xPoint,yPoint,3);
}
});
jbt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics gr=jp2.getGraphics();
gr.drawOval(100,10,200,200);
}
});
jbt3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics G= jp2.getGraphics();
G.drawRect(500,50,200,200);
}
});
}
public static void main(String[] args){
JFrame frame=new DrawingBoard();
frame.setTitle("測試畫圖板");
frame.setSize(736,379);
frame.setLocation(100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class DrawingPanel extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setFont(new Font("宋體",Font.BOLD,m));
g.setColor(color);
g.drawString("Heleoeo ",100, 100);
}
}
}
有壹些問題還沒有解決,因為重畫,所以字符串可以重畫,別的我沒有重畫,妳自己可以修改壹下,
還有壹個問題,妳的菜單條上的功能我暫時不會寫,所以沒有加上去,
希望對妳有幫助,壹起努力吧!!!