java swing 有哪些_一个经典的java swing gui设计(几乎包含所有常用的控件及布局)

本文介绍了如何使用Java Swing构建一个类似Eclipse搜索GUI的界面,包括各种布局(GridLayout, BoxLayout, FlowLayout等)的运用,以及JLabel、JComboBox、JRadioButton、JCheckBox等常见控件的使用。强调布局在UI设计中的重要性,并提供了完整的代码示例。" 44101191,4949381,Android实现圆形ImageView,"['Android开发', 'UI设计', '图像处理', '自定义组件']
摘要由CSDN通过智能技术生成

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

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

原图:

c14fb60a1510d73a9a150570051438d9.png

我的代码跑出来的图:

19b14ded3ff56246ea88ccd262231f9c.png

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

b5cb903d4a9f909f040b83f2da7602a1.png

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

North区:

JLabel、JComboBox

JRadioButton

JCheckBox

South区:

JButton

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

192353e26dce137bff6023b432bd8d43.png

这是我花时间最长的地方,因为我一直以为那是个控件,其实是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();

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值