標籤:stack 如何 rar rgs etl draw 分區 java set
/*文章中用到的代碼只是一部分,需要源碼的可通過郵箱聯絡我 [email protected]*/
這段時間在學JAVA的swing介面開發,試著做了個畫圖板。實現了直線、曲線、噴槍、矩形、圓形、文字、橡皮等操作,感覺收穫很大。
既然要做畫圖板,那最好的參考當然是windows系統內建的畫圖啦!雖然技術有限不能做的一模一樣,但感覺還是能看(手動滑稽)。下面就講講如何?了。
首先不用想,肯定是先把介面做好了(這是我做的介面,emmmmmm。。。。功能和介面都還有待完善)
仔細看一看大概就能想到怎麼實現了,首先建立一個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);
這樣,我們的介面就做好了。
JAVA 畫圖板實現(基本畫圖功能+介面UI)一、介面實現