java gui图形界面编程

转载学习

转自:https://blog.csdn.net/u012891055/article/details/50095365


看了下网上的gui教程都没有什么比较好的,不管是java、安卓还是ios,设计UI都应该先从布局上来考虑,而不是看一点写一点。如果你一来就想着用绝对布局,我只能说这种思想很危险,砖慢慢搬吧。

这个是中期考试的时候边学边做的一个东西,做一个eclipse的搜索gui,类似下图,其实也就是个苦力活。

原图:

我的代码跑出来的图:



先说布局,我直接给张图:



设计到的控件(从上到下、从左到右的顺序):

North区:

JLabel、JComboBox

JRadioButton

JCheckBox

South区:

JButton


值得一提的是panel的titleBorder,就是这个玩意儿:


这是我花时间最长的地方,因为我一直以为那是个控件,其实是panel的Border,像这样设置一下panel的Border属性就可以了:

[html]  view plain  copy
  1. JPanel panelDirector = new JPanel();  
  2. panelDirector .setBorder(BorderFactory.createTitledBorder("Director"));  


其他都没什么问题了,我直接贴代码了:

[html]  view plain  copy
  1. package org.echo.project2;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.FlowLayout;  
  5. import java.awt.GridLayout;  
  6.   
  7. import javax.swing.BorderFactory;  
  8. import javax.swing.BoxLayout;  
  9. import javax.swing.ButtonGroup;  
  10. import javax.swing.JButton;  
  11. import javax.swing.JCheckBox;  
  12. import javax.swing.JComboBox;  
  13. import javax.swing.JFrame;  
  14. import javax.swing.JLabel;  
  15. import javax.swing.JPanel;  
  16. import javax.swing.JRadioButton;  
  17.   
  18. /**  
  19.  * echo 邮箱[email protected]  
  20.  *   
  21.  */  
  22. public class MainView extends JFrame{  
  23.   
  24.     public MainView() {  
  25.           
  26.         this.setTitle("Find/Replace");  
  27.         this.setSize(600, 600);  
  28.         this.setLocation(500, 500);  
  29.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  30.           
  31.         // find  
  32.         JPanel panelFind = new JPanel();  
  33.         JLabel findLabel = new JLabel("Find:");  
  34.         JComboBox findBox = new JComboBox();  
  35.         findBox.setEditable(true);  
  36.         panelFind.setLayout(new GridLayout(1, 2));  
  37.         panelFind.add(findLabel);  
  38.         panelFind.add(findBox);  
  39.           
  40.         // replace  
  41.         JPanel panelReplace = new JPanel();  
  42.         panelReplace.setLayout(new GridLayout(1, 2));  
  43.         JLabel replaceLabel = new JLabel("Replace with:");;  
  44.         JComboBox replaceBox = new JComboBox();  
  45.         replaceBox.setEditable(true);  
  46.         panelReplace.add(replaceLabel);  
  47.         panelReplace.add(replaceBox);  
  48.           
  49.         // find and replace  
  50.         JPanel panelArea1 = new JPanel();  
  51.         panelArea1.setLayout(new BoxLayout(panelArea1, BoxLayout.Y_AXIS));  
  52.         panelArea1.add(panelFind);  
  53.         panelArea1.add(panelReplace);  
  54.           
  55.         // direction  
  56.         JPanel panelDirection = new JPanel();  
  57.         panelDirection.setLayout(new BoxLayout(panelDirection, BoxLayout.Y_AXIS));  
  58.         JRadioButton forButton = new JRadioButton("Forward");  
  59.         JRadioButton backButton = new JRadioButton("Backward");  
  60.         ButtonGroup directionGroup = new ButtonGroup();  
  61.         directionGroup.add(forButton);  
  62.         directionGroup.add(backButton);  
  63.         panelDirection.add(forButton);  
  64.         panelDirection.add(backButton);  
  65.         panelDirection.setBorder(BorderFactory.createTitledBorder("Director"));  
  66.           
  67.         // scope  
  68.         JPanel panelScope = new JPanel();  
  69.         panelScope.setLayout(new BoxLayout(panelScope, BoxLayout.Y_AXIS));  
  70.         JRadioButton allButton = new JRadioButton("All");  
  71.         JRadioButton selectedButton = new JRadioButton("Seleted lines");  
  72.         ButtonGroup scopeGroup = new ButtonGroup();  
  73.         scopeGroup.add(allButton);  
  74.         scopeGroup.add(selectedButton);  
  75.         panelScope.add(allButton);  
  76.         panelScope.add(selectedButton);  
  77.         panelScope.setBorder(BorderFactory.createTitledBorder("Scope"));  
  78.           
  79.         // direction and scope  
  80.         JPanel panelDireAndScope = new JPanel();  
  81.         panelDireAndScope.setLayout(new GridLayout(1,2));  
  82.         panelDireAndScope.add(panelDirection);  
  83.         panelDireAndScope.add(panelScope);  
  84.           
  85.         // options  
  86.         JPanel panelOptions = new JPanel();  
  87.         panelOptions.setLayout(new GridLayout(3,2));  
  88.         JCheckBox checkBox1 = new JCheckBox("Case sensitive");  
  89.         JCheckBox checkBox2 = new JCheckBox("Wrap search");  
  90.         JCheckBox checkBox3 = new JCheckBox("Whole word");  
  91.         JCheckBox checkBox4 = new JCheckBox("Incremental");  
  92.         JCheckBox checkBox5 = new JCheckBox("Regular expressions");  
  93.         ButtonGroup optionsGroup = new ButtonGroup();  
  94.         optionsGroup.add(checkBox1);  
  95.         optionsGroup.add(checkBox2);  
  96.         optionsGroup.add(checkBox3);  
  97.         optionsGroup.add(checkBox4);  
  98.         optionsGroup.add(checkBox5);  
  99.         panelOptions.add(checkBox1);  
  100.         panelOptions.add(checkBox2);  
  101.         panelOptions.add(checkBox3);  
  102.         panelOptions.add(checkBox4);  
  103.         panelOptions.add(checkBox5);  
  104.         panelOptions.setBorder(BorderFactory.createTitledBorder("Options"));  
  105.           
  106.         // choose buttons  
  107.         JPanel panelButtons = new JPanel();  
  108.         panelButtons.setLayout(new GridLayout(3, 2));  
  109.         JButton btnFind = new JButton("Find");  
  110.         JButton btnReOrFind = new JButton("Replace/Find");  
  111.         JButton btnReplace = new JButton("Replace");  
  112.         JButton btnReplaceAll = new JButton("Replace All");  
  113.         JLabel lblNull = new JLabel("");  
  114.         JButton btnClose = new JButton("Close");  
  115.         panelButtons.add(btnFind);  
  116.         panelButtons.add(btnReOrFind);  
  117.         panelButtons.add(btnReplace);  
  118.         panelButtons.add(btnReplaceAll);  
  119.         panelButtons.add(lblNull);  
  120.         panelButtons.add(btnClose);  
  121.           
  122.         // panel south  
  123.         JPanel southPanel = new JPanel();  
  124.         southPanel.setLayout(new BorderLayout());  
  125.         southPanel.add(panelButtons, BorderLayout.EAST);  
  126.           
  127.         // panel north  
  128.         JPanel northPanel = new JPanel();  
  129.         northPanel.setLayout(new GridLayout(3, 1));  
  130.         northPanel.add(panelArea1);  
  131.         northPanel.add(panelDireAndScope);  
  132.         northPanel.add(panelOptions);  
  133.           
  134.         this.setLayout(new BorderLayout());         
  135.         this.add(northPanel, BorderLayout.NORTH);  
  136.         this.add(southPanel, BorderLayout.SOUTH);  
  137.         this.setVisible(true);  
  138.     }  
  139.       
  140.     public static void main(String[] args) {  
  141.         // Single mode  
  142.         MainView mainView = new MainView();  
  143.     }  
  144.   
  145. }  

你可能感兴趣的:(学习日记)