java gui图形界面编程(几乎包含所有常用的控件及布局)

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

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

原图:

我的代码跑出来的图:



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



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

North区:

JLabel、JComboBox

JRadioButton

JCheckBox

South区:

JButton


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


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

JPanel panelDirector = new JPanel();
panelDirector .setBorder(BorderFactory.createTitledBorder("Director"));


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

package org.echo.project2;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * echo 邮箱756451227@qq.com
 * 
 */
public class MainView extends JFrame{

	public MainView() {
		
		this.setTitle("Find/Replace");
		this.setSize(600, 600);
		this.setLocation(500, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// find
		JPanel panelFind = new JPanel();
		JLabel findLabel = new JLabel("Find:");
		JComboBox findBox = new JComboBox();
		findBox.setEditable(true);
		panelFind.setLayout(new GridLayout(1, 2));
		panelFind.add(findLabel);
		panelFind.add(findBox);
		
		// replace
		JPanel panelReplace = new JPanel();
		panelReplace.setLayout(new GridLayout(1, 2));
		JLabel replaceLabel = new JLabel("Replace with:");;
		JComboBox replaceBox = new JComboBox();
		replaceBox.setEditable(true);
		panelReplace.add(replaceLabel);
		panelReplace.add(replaceBox);
		
		// find and replace
		JPanel panelArea1 = new JPanel();
		panelArea1.setLayout(new BoxLayout(panelArea1, BoxLayout.Y_AXIS));
		panelArea1.add(panelFind);
		panelArea1.add(panelReplace);
		
		// direction
		JPanel panelDirection = new JPanel();
		panelDirection.setLayout(new BoxLayout(panelDirection, BoxLayout.Y_AXIS));
        JRadioButton forButton = new JRadioButton("Forward");
        JRadioButton backButton = new JRadioButton("Backward");
        ButtonGroup directionGroup = new ButtonGroup();
        directionGroup.add(forButton);
        directionGroup.add(backButton);
        panelDirection.add(forButton);
        panelDirection.add(backButton);
        panelDirection.setBorder(BorderFactory.createTitledBorder("Director"));
        
        // scope
        JPanel panelScope = new JPanel();
        panelScope.setLayout(new BoxLayout(panelScope, BoxLayout.Y_AXIS));
        JRadioButton allButton = new JRadioButton("All");
        JRadioButton selectedButton = new JRadioButton("Seleted lines");
        ButtonGroup scopeGroup = new ButtonGroup();
        scopeGroup.add(allButton);
        scopeGroup.add(selectedButton);
        panelScope.add(allButton);
        panelScope.add(selectedButton);
        panelScope.setBorder(BorderFactory.createTitledBorder("Scope"));
        
        // direction and scope
        JPanel panelDireAndScope = new JPanel();
        panelDireAndScope.setLayout(new GridLayout(1,2));
        panelDireAndScope.add(panelDirection);
        panelDireAndScope.add(panelScope);
        
        // options
        JPanel panelOptions = new JPanel();
        panelOptions.setLayout(new GridLayout(3,2));
        JCheckBox checkBox1 = new JCheckBox("Case sensitive");
        JCheckBox checkBox2 = new JCheckBox("Wrap search");
        JCheckBox checkBox3 = new JCheckBox("Whole word");
        JCheckBox checkBox4 = new JCheckBox("Incremental");
        JCheckBox checkBox5 = new JCheckBox("Regular expressions");
        ButtonGroup optionsGroup = new ButtonGroup();
        optionsGroup.add(checkBox1);
        optionsGroup.add(checkBox2);
        optionsGroup.add(checkBox3);
        optionsGroup.add(checkBox4);
        optionsGroup.add(checkBox5);
        panelOptions.add(checkBox1);
        panelOptions.add(checkBox2);
        panelOptions.add(checkBox3);
        panelOptions.add(checkBox4);
        panelOptions.add(checkBox5);
        panelOptions.setBorder(BorderFactory.createTitledBorder("Options"));
        
        // choose buttons
        JPanel panelButtons = new JPanel();
        panelButtons.setLayout(new GridLayout(3, 2));
        JButton btnFind = new JButton("Find");
        JButton btnReOrFind = new JButton("Replace/Find");
        JButton btnReplace = new JButton("Replace");
        JButton btnReplaceAll = new JButton("Replace All");
        JLabel lblNull = new JLabel("");
        JButton btnClose = new JButton("Close");
        panelButtons.add(btnFind);
        panelButtons.add(btnReOrFind);
        panelButtons.add(btnReplace);
        panelButtons.add(btnReplaceAll);
        panelButtons.add(lblNull);
        panelButtons.add(btnClose);
        
        // panel south
        JPanel southPanel = new JPanel();
        southPanel.setLayout(new BorderLayout());
        southPanel.add(panelButtons, BorderLayout.EAST);
        
        // panel north
        JPanel northPanel = new JPanel();
        northPanel.setLayout(new GridLayout(3, 1));
        northPanel.add(panelArea1);
        northPanel.add(panelDireAndScope);
        northPanel.add(panelOptions);
        
        this.setLayout(new BorderLayout());       
        this.add(northPanel, BorderLayout.NORTH);
        this.add(southPanel, BorderLayout.SOUTH);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		// Single mode
		MainView mainView = new MainView();
	}

}




  • 16
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 ); } }
Java GUI图形界面编程是指使用Java编程语言来创建图形用户界面(Graphical User Interface,GUI)应用程序的过程。Java提供了一组丰富的类库和工具,使得开发人员可以快速、方便地创建交互性强、用户友好的应用程序。 Java GUI图形界面编程的主要组成部分是Swing和JavaFX两个库。Swing是一个基于Java的图形用户界面工具包,提供了丰富的用户界面组件,如按钮、文本框、表格等,可以在应用程序中创建和管理这些组件,实现用户界面的交互。JavaFX是Java平台上的富客户端应用程序框架,提供了更强大、更灵活的界面组件和布局控制,可以创建更加先进、更具吸引力的GUI应用程序。 使用Java GUI图形界面编程进行应用程序开发有许多优点。首先,Java GUI库提供了丰富的组件和控件,开发人员可以根据自己的需求选择合适的组件,快速构建出用户界面。其次,Java GUI库具有良好的跨平台性,可以在不同操作系统上运行,并且具有相同的用户界面。再者,Java提供了强大的事件驱动模型,使得应用程序能够响应用户的操作,实现交互性。 总之,Java GUI图形界面编程是一种灵活、方便、强大的开发方式,使得开发人员能够更加简单地创建出功能强大、用户友好的应用程序。无论是简单的桌面应用还是复杂的企业级应用,都可以通过Java GUI图形界面编程来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值