/*文章中用到的代碼只是一部分,需要源碼的可通過郵箱聯系我 1978702969@qq.com*/
這段時間在學JAVA的swing界面開發,試着做了個畫圖板。實現了直線、曲線、噴槍、矩形、圓形、文字、橡皮等操作,感覺收獲很大。
既然要做畫圖板,那最好的參考當然是windows系統自帶的畫圖啦!雖然技術有限不能做的一模一樣,但感覺還是能看(手動滑稽)。下面就講講如何實現了。
首先不用想,肯定是先把界面做好了(這是我做的界面,emmmmmm。。。。功能和界面都還有待完善)
注:代碼已上傳至github:https://github.com/leo6033/Java_Project

仔細看一看大概就能想到怎么實現了,首先創建一個DrawMain類繼承(extends)JFrame類
public class DrawMain extends JFrame {
public static void main(String[] args) {
DrawMain dm = new DrawMain();
dm.setLookAndFeel();
dm.initUI();
}
/**
* 為主面板設置皮膚,這個是我隨便找的一個,具體可以自己去研究
*/
public void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
public void initUI() {
this.setTitle("畫圖程序");
this.setSize(1000, 700);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setLayout(new BorderLayout());
this.setVisible(true);
}
這當然只是主界面啦,那后面該怎么弄呢?上面可以有那么多個分區當然需要再放幾個容器類型的組件啦。就是組件里放組件了,那么此時布局的選擇很重要,首先利用主界面是BroderLayout,就在北方向上放上一個JPanel上去咯
JPanel NorthJPanel = new JPanel(); NorthJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 0)); NorthJPanel.setBackground(new Color(240, 240, 240));//設置背景色
//NorthJPanel.setBorder(BorderFactory.createEtchedBorder(new Color(0, 0, 0), new Color(0, 255, 0)));設置邊框,可以看看有什么區別 this.add(NorthJPanel, BorderLayout.NORTH);
運行一下,再拉拉邊框,有什么發現沒有?這個剛貼上去的組件大小會隨着你拉邊框而改變,所以我們應該再貼一個JPanel到這個JPanel里,然后再設置好大小防止改變
JPanel InNorthJPanel = new JPanel(); InNorthJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 0)); InNorthJPanel.setPreferredSize(new Dimension(900, 150)); InNorthJPanel.setBackground(new Color(240, 240, 240)); NorthJPanel.add(InNorthJPanel);
然后該怎么做呢?設置分區,自然,每個分區就是一個JPanel組件
/*
* 形狀區域
*
* @param ShapeJPanel 形狀區域的面板,界面布局
*
* @param InShapeJPanel 形狀區域中放置形狀選項的面板,放在ShapeJPanel中,流式布局
*
* @param InShapeLabel 形狀區域中標識區域的標簽,放在ShapeJPanel中
*/
JPanel ShapeJPanel = null;
ShapeJPanel = createJPanel(InNorthJPanel);
ShapeJPanel.setPreferredSize(new Dimension(300, 150));
JPanel InShapeJPanel = new JPanel();
InShapeJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
InShapeJPanel.setBackground(null);// 設置背景色透明
InShapeJPanel.setOpaque(false);
InShapeJPanel.setPreferredSize(new Dimension(300, 110));
ShapeJPanel.add(InShapeJPanel, BorderLayout.NORTH);
JLabel InShapeLabel = null;
InShapeLabel = createJLabel("形狀", ShapeJPanel);
/*
* 顏色區域
*
* @param ColorJPanel 顏色區域面板,界面布局
*
* @param InColorJPanel 顏色區域中放置顏色選項的面板,放在ColorJPanel中,流式布局
*
* @param InColorLabel 顏色區域中標識區域的標簽,放在ColorJPanel中
*/
JPanel ColorJPanel = null;
ColorJPanel = createJPanel(InNorthJPanel);
JPanel IncolorJPanel = new JPanel();
IncolorJPanel.setPreferredSize(new Dimension(200, 110));
IncolorJPanel.setBackground(null);// 設置背景色透明
IncolorJPanel.setOpaque(false);
IncolorJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
ColorJPanel.add(IncolorJPanel, BorderLayout.NORTH);
JLabel InColorLabel = null;
InColorLabel = createJLabel("顏色", ColorJPanel);
/*
* 粗細設置區域
*
* @param StrokeJPanel 粗細設置區域面板,界面布局
*
* @param InStrokeJPanel 粗細設置區域中放置粗細選項的面板,放在StrokeJPanel中,流式布局
*
* @param InStrokeLabel 粗細設置區域的標簽,放在StrokeJPanel中
*/
JPanel StrokeJPanel = null;
StrokeJPanel = createJPanel(InNorthJPanel);
StrokeJPanel.setPreferredSize(new Dimension(50, 150));
JPanel InStrokeJPanel = new JPanel();
InStrokeJPanel.setPreferredSize(new Dimension(50, 110));
InStrokeJPanel.setBackground(null);
InStrokeJPanel.setOpaque(false);
InStrokeJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
StrokeJPanel.add(InStrokeJPanel, BorderLayout.NORTH);
JLabel InStrokeLabel = null;
InStrokeLabel = createJLabel("粗細", StrokeJPanel);
可能你會發現,我在里面用了createJLabel()和createJPanel(),這是我寫的方法,因為在創建過程中很多代碼是重復的,自己寫兩個方法用在里面代碼看上去會舒服很多,而且也能少寫很多代碼。兩個方法的具體實現
private JPanel createJPanel(JPanel InNorthJPanel) {
JPanel jp = new JPanel();
jp.setBorder(BorderFactory.createEtchedBorder(new Color(0, 0, 0), new Color(0, 255, 0)));
jp.setPreferredSize(new Dimension(200, 150));
jp.setBackground(new Color(240, 240, 240));
InNorthJPanel.add(jp);
return jp;
}
private JLabel createJLabel(String s, JPanel jp) {
JLabel jl = new JLabel(s);
jl.setHorizontalAlignment(JLabel.CENTER);// 設置對其格式劇中
jl.setFont(new Font("楷體", Font.BOLD, 20));// 設置字體 樣式 大小
jp.add(jl, BorderLayout.SOUTH);
return jl;
}
這樣上面的邊框就做好了,接下來就是貼按鈕和文本框之類的了
/*
* 放置按鈕
*/
String[] typeArray = { "Line", "Oval", "Rect", "RoundRect", "fill3DRect", "fillArc", "Image", "Text", "Pencil",
"iso_Tri", "Polygon","噴槍", "Erase" };
Color[] colorArray = { Color.red, Color.black, Color.green, Color.BLUE, new Color(255, 255, 255) };
String[] widthArray = { "1", "3", "5" };
JTextField text = new JTextField();
text.setPreferredSize(new Dimension(100, 30));
DrawListener dl = new DrawListener(this, text, list);
for (int i = 0; i < typeArray.length; i++) {
JButton button = new JButton(typeArray[i]);
InShapeJPanel.add(button);
button.addActionListener(dl);
if(i>=12)
{
JButton button1 = new JButton(typeArray[i]);
InNorthJPanel.add(button);
button1.addActionListener(dl);
}
}
for (int i = 0; i < colorArray.length; i++) {
JButton button = new JButton();
button.setBackground(colorArray[i]);
button.setPreferredSize(new Dimension(30, 30));
IncolorJPanel.add(button);
button.addActionListener(dl);
}
for (int i = 0; i < widthArray.length; i++) {
JButton button = new JButton(widthArray[i]);
InStrokeJPanel.add(button);
button.addActionListener(dl);
}
InNorthJPanel.add(text);
這樣,我們的界面就做好了。
