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 邮箱756451227@qq.com  
  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. }  

javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值