java图形界面设计代码_Java图形界面设计

本文详细介绍了Java图形用户界面的设计,包括Swing组件、容器如JFrame和JPanel的使用,以及FlowLayout、BorderLayout、GridLayout、CardLayout和BoxLayout等布局管理器的工作原理和示例代码,帮助开发者理解如何创建和组织GUI元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、总述

Java的图形用户界面由各种组件(component)构成,它们主要位于java.awt包与javax.swing包中。Swing与AWT最大的不同是,Swing在实现时,不包含任何本地代码(native),是一种“轻量级(lightweight)”的组件

Swing具有状态的组件。

二、容器

1.顶层容器:

JFrame、JApplet、JDialog 和 JWindow

2.JFrame创建的一个程序

2.1代码

importjava.awt.*;

importjavax.swing.*;

publicclassJFrameDemo{

publicstaticvoidmain(String args[]){

JFrame frame = newJFrame("JFrameDemo");

JButton button = newJButton("Press Me");

//first way to do that

// frame.getContentPane().add(button,BorderLayout.CENTER);

//another way to set the Pane

JPanel contentPane = newJPanel();

contentPane.setLayout(newBorderLayout());

contentPane.add(button,BorderLayout.CENTER);

frame.setContentPane(contentPane);

//frame.pack();

frame.setVisible(true);

}

}

import java.awt.*;

import javax.swing.*;

public class JFrameDemo{

public static void main(String args[]){

JFrame frame = new JFrame("JFrameDemo");

JButton button = new JButton("Press Me");

//first way to do that

// frame.getContentPane().add(button,BorderLayout.CENTER);

//another way to set the Pane

JPanel contentPane = new JPanel();

contentPane.setLayout(new BorderLayout());

contentPane.add(button,BorderLayout.CENTER);

frame.setContentPane(contentPane);

//frame.pack();

frame.setVisible(true);

}

}

2.2执行结果

admin

3.面板(JPanel)

可以相互嵌套,不能独立存在,只能添加到其他窗口内部。

3.1代码

importjava.awt.*;

importjavax.swing.*;

publicclassFrameWithPanel{

publicstaticvoidmain(String args[]){

JFrame frame = newJFrame("Frame with Panel");

Container contentPane = frame.getContentPane();

contentPane.setBackground(Color.CYAN);

JPanel panel = newJPanel();

panel.setBackground(Color.yellow);

JButton button = newJButton("Press me");

panel.add(button);

//add JButton instance to JPanel

//add JPanel instance to JFrame's south

contentPane.add(panel,BorderLayout.SOUTH);

frame.setSize(300,200);

frame.setVisible(true);

}

}

import java.awt.*;

import javax.swing.*;

public class FrameWithPanel{

public static void main(String args[]){

JFrame frame = new JFrame("Frame with Panel");

Container contentPane = frame.getContentPane();

contentPane.setBackground(Color.CYAN);

JPanel panel = new JPanel();

panel.setBackground(Color.yellow);

JButton button = new JButton("Press me");

panel.add(button);

//add JButton instance to JPanel

//add JPanel instance to JFrame's south

contentPane.add(panel,BorderLayout.SOUTH);

frame.setSize(300,200);

frame.setVisible(true);

}

}

3.2执行结果

admin

三、布局

1.总述

组件的布局(包括位置与大小)通常由Layout Manager负责安排。Java平台提供了多种布局管理器,以下对其部分,进行说明。

2.FlowLayout Layout Manager

2.1FlowLayout 的三种构造方法

publicFlowLayout()

publicFlowLayout(intalign)

publicFlowLayout(intalign,inthgap,intvgap)

public FlowLayout()

public FlowLayout(int align)

public FlowLayout(int align,int hgap,int vgap)

构造方法中,提供了一个对齐方式的可选项align,取值有三种形式:FlowLayout.LEFT、 FlowLayout.CENTER 、FlowLayout.RIGHT。hgap和vgap可以设定组件的水平间距和垂直距离。

2.2参考代码

importjava.awt.*;

importjavax.swing.*;

publicclassFlowLayoutDemo{

privateJFrame frame;

privateJButton btn1,btn2,btn3;

publicstaticvoidmain(String args[]){

FlowLayoutDemo that = newFlowLayoutDemo();

that.go();

}

publicvoidgo(){

frame = newJFrame("Flow Layout");

Container contentPane = frame.getContentPane();

contentPane.setLayout(newFlowLayout());

btn1 = newJButton("OK");

btn2 = newJButton("Open");

btn3 = newJButton("Close");

contentPane.add(btn1);

contentPane.add(btn2);

contentPane.add(btn3);

frame.setSize(300,200);

frame.setVisible(true);

}

}

import java.awt.*;

import javax.swing.*;

public class FlowLayoutDemo{

private JFrame frame;

private JButton btn1,btn2,btn3;

public static void main(String args[]){

FlowLayoutDemo that = new FlowLayoutDemo();

that.go();

}

public void go(){

frame = new JFrame("Flow Layout");

Container contentPane = frame.getContentPane();

contentPane.setLayout(new FlowLayout());

btn1 = new JButton("OK");

btn2 = new JButton("Open");

btn3 = new JButton("Close");

contentPane.add(btn1);

contentPane.add(btn2);

contentPane.add(btn3);

frame.setSize(300,200);

frame.setVisible(true);

}

}

2.3执行结果

改变Frame的大小,Frame中组件的布局也会随之改变。

admin

admin

admin

3.BorderLayout 布局管理器

3.1概述

BorderLayout是顶层窗口中内容窗格的默认布局管理器,被划分为BorderLayout.NORTH、BorderLayout.SOUTH、BorderLayout.WEST、BorderLayout.EAST、BorderLayout.CENTER 五个区域。

3.2构造函数

BorderLayout()

BorderLayout(int,int)

BorderLayout()

BorderLayout(int,int)

3.3示例代码

importjava.awt.*;

importjavax.swing.*;

publicclassBorderLayoutDemo{

privateJFrame frame;

privateJButton be,bw,bn,bs,bc;

publicstaticvoidmain(String args[]){

BorderLayoutDemo that = newBorderLayoutDemo();

that.go();

}

publicvoidgo(){

frame = newJFrame("Border Layout");

be = newJButton("East");

bw = newJButton("West");

bn = newJButton("North");

bs = newJButton("South");

bc = newJButton("Center");

frame.getContentPane().add(be,"East");

frame.getContentPane().add(bw,BorderLayout.WEST);

frame.getContentPane().add(bn,BorderLayout.NORTH);

frame.getContentPane().add(bs,BorderLayout.SOUTH);

frame.getContentPane().add(bc,BorderLayout.CENTER);

frame.setSize(350,200);

frame.setVisible(true);

}

}

import java.awt.*;

import javax.swing.*;

public class BorderLayoutDemo{

private JFrame frame;

private JButton be,bw,bn,bs,bc;

public static void main(String args[]){

BorderLayoutDemo that = new BorderLayoutDemo();

that.go();

}

public void go(){

frame = new JFrame("Border Layout");

be = new JButton("East");

bw = new JButton("West");

bn = new JButton("North");

bs = new JButton("South");

bc = new JButton("Center");

frame.getContentPane().add(be,"East");

frame.getContentPane().add(bw,BorderLayout.WEST);

frame.getContentPane().add(bn,BorderLayout.NORTH);

frame.getContentPane().add(bs,BorderLayout.SOUTH);

frame.getContentPane().add(bc,BorderLayout.CENTER);

frame.setSize(350,200);

frame.setVisible(true);

}

}

3.4执行结果

admin

4.GridLayout布局管理器

4.1总述

是一种网格式的布局管理器,将窗口空间分割成若干行,乘若干列的网格。组件依次放入,占一格。

4.2构造函数

public GridLayout()

public GridLayout(int rows, int cols)

public GridLayout(int rows, int cols, int hgap, int vgap)

4.3参考代码

importjava.awt.*;

importjavax.swing.*;

publicclassGridLayoutDemo{

privateJFrame frame;

privateJButton b1,b2,b3,b4,b5,b6;

publicstaticvoidmain(String args[]){

GridLayoutDemo that = newGridLayoutDemo();

that.go();

}

publicvoidgo(){

frame = newJFrame("Grid Frame");

Container contentPane = frame.getContentPane();

contentPane.setLayout(newGridLayout(3,2));

b1 = newJButton("grid_1");

b2 = newJButton("grid_2");

b3 = newJButton("grid_3");

b4 = newJButton("grid_4");

b5 = newJButton("grid_5");

b6 = newJButton("grid_6");

contentPane.add(b1);

contentPane.add(b2);

contentPane.add(b3);

contentPane.add(b4);

contentPane.add(b5);

contentPane.add(b6);

frame.pack();

frame.setVisible(true);

}

}

import java.awt.*;

import javax.swing.*;

public class GridLayoutDemo{

private JFrame frame;

private JButton b1,b2,b3,b4,b5,b6;

public static void main(String args[]){

GridLayoutDemo that = new GridLayoutDemo();

that.go();

}

public void go(){

frame = new JFrame("Grid Frame");

Container contentPane = frame.getContentPane();

contentPane.setLayout(new GridLayout(3,2));

b1 = new JButton("grid_1");

b2 = new JButton("grid_2");

b3 = new JButton("grid_3");

b4 = new JButton("grid_4");

b5 = new JButton("grid_5");

b6 = new JButton("grid_6");

contentPane.add(b1);

contentPane.add(b2);

contentPane.add(b3);

contentPane.add(b4);

contentPane.add(b5);

contentPane.add(b6);

frame.pack();

frame.setVisible(true);

}

}

4.4执行结果

admin

5.CardLayout 布局管理器

5.1总述

是一种卡片式的布局管理器,它将容器中的组件处理成一系列卡片,每一时刻只显示出其中一张。

5.2参考代码:

importjava.awt.*;

importjavax.swing.*;

importjava.awt.event.*;

publicclassCardLayoutDemoextendsMouseAdapter{

JPanel p1,p2,p3,p4,p5;

JLabel l1, l2, l3, l4, l5;

//declare a Cardlayout object

CardLayout myCard;

JFrame frame;

Container contentPane;

publicstaticvoidmain(String args[]){

CardLayoutDemo that = newCardLayoutDemo();

that.go();

System.out.println("test");

}

publicvoidgo(){

frame = newJFrame("Card Test");

contentPane = frame.getContentPane();

myCard = newCardLayout();

contentPane.setLayout(myCard);

p1=newJPanel();

p2 = newJPanel();

p3 = newJPanel();

p4 = newJPanel();

p5 = newJPanel();

//set the different bk color for each JPanel label

l1 =  newJLabel("This is the first JPanel");

p1.add(l1);

p1.setBackground(Color.yellow);

l2 =  newJLabel("This is the second JPanel");

p2.add(l2);

p2.setBackground(Color.green);

l3 =  newJLabel("This is the third JPanel");

p3.add(l3);

p3.setBackground(Color.magenta);

l4 =  newJLabel("This is the forth JPanel");

p4.add(l4);

p4.setBackground(Color.white);

l5 =  newJLabel("This is the fifth JPanel");

p5.add(l5);

p5.setBackground(Color.cyan);

//set mouseListener

p1.addMouseListener(this);

p2.addMouseListener(this);

p3.addMouseListener(this);

p4.addMouseListener(this);

p5.addMouseListener(this);

//set each JPanle as a Card to insert frame

contentPane.add(p1,"First");

contentPane.add(p2,"Sencond");

contentPane.add(p3,"Third");

contentPane.add(p4,"Forth");

contentPane.add(p5,"Fifth");

//display the first pic

myCard.show(contentPane,"First");

frame.setSize(300,200);

frame.show();

}

publicvoidmouseClicked(MouseEvent e){

myCard.next(contentPane);

}

}

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class CardLayoutDemo extends MouseAdapter{

JPanel p1,p2,p3,p4,p5;

JLabel l1, l2, l3, l4, l5;

//declare a Cardlayout object

CardLayout myCard;

JFrame frame;

Container contentPane;

public static void main(String args[]){

CardLayoutDemo that = new CardLayoutDemo();

that.go();

System.out.println("test");

}

public void go(){

frame = new JFrame("Card Test");

contentPane = frame.getContentPane();

myCard = new CardLayout();

contentPane.setLayout(myCard);

p1=new JPanel();

p2 = new JPanel();

p3 = new JPanel();

p4 = new JPanel();

p5 = new JPanel();

//set the different bk color for each JPanel label

l1 = new JLabel("This is the first JPanel");

p1.add(l1);

p1.setBackground(Color.yellow);

l2 = new JLabel("This is the second JPanel");

p2.add(l2);

p2.setBackground(Color.green);

l3 = new JLabel("This is the third JPanel");

p3.add(l3);

p3.setBackground(Color.magenta);

l4 = new JLabel("This is the forth JPanel");

p4.add(l4);

p4.setBackground(Color.white);

l5 = new JLabel("This is the fifth JPanel");

p5.add(l5);

p5.setBackground(Color.cyan);

//set mouseListener

p1.addMouseListener(this);

p2.addMouseListener(this);

p3.addMouseListener(this);

p4.addMouseListener(this);

p5.addMouseListener(this);

//set each JPanle as a Card to insert frame

contentPane.add(p1,"First");

contentPane.add(p2,"Sencond");

contentPane.add(p3,"Third");

contentPane.add(p4,"Forth");

contentPane.add(p5,"Fifth");

//display the first pic

myCard.show(contentPane,"First");

frame.setSize(300,200);

frame.show();

}

public void mouseClicked(MouseEvent e){

myCard.next(contentPane);

}

}

5.2执行结果

admin

6.BoxLayout布局管理器

6.1总述

将容器中的组件,按水平排成一行,或按垂直方向排成一列。

6.2构造函数

BoxLayout(Container target, int axis)

axis = BoxLayout.X_AXIS | BoxLayout.Y_AXIS

6.3参考代码

import java.awt.*;

import javax.swing.*;

public class BoxLayoutDemo{

private JFrame frame;

private JPanel pv,ph;

public static void main(String args[]){

BoxLayoutDemo that = new BoxLayoutDemo();

that.go();

}

public void go(){

frame = new JFrame("Box Layout example");

Container contentPane = frame.getContentPane();

pv = new JPanel();

pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS));

pv.add(new JLabel("Monday"));

pv.add(new JLabel("Tuesday"));

pv.add(new JLabel("Wednesday"));

pv.add(new JLabel("Thursday"));

pv.add(new JLabel("Friday"));

pv.add(new JLabel("Saturday"));

pv.add(new JLabel("Sunday"));

contentPane.add(pv,BorderLayout.CENTER);

ph  = new JPanel();

ph.setLayout(new BoxLayout(ph,BoxLayout.X_AXIS));

ph.add(new JButton("Yes"));

ph.add(new JButton("No"));

ph.add(new JButton("Cancle"));

contentPane.add(ph,BorderLayout.SOUTH);

frame.pack();

frame.setVisible(true);

}

}

6.4执行结果

admin

6.5专门使用BoxLayout的特殊容器—Box类

6.5.1创建实例的静态方法

publicstaticBox createHorizontalBox()

publicstaticBox createVerticalBox()

public static Box createHorizontalBox()

public static Box createVerticalBox()

6.5.2|6.3的代码,改写如下

//the specific Box class for BoxLayout Container

importjava.awt.*;

importjavax.swing.*;

publicclassBoxDemo{

privateJFrame frame;

privateBox bv,bh;

publicstaticvoidmain(String arg[]){

BoxDemo that = newBoxDemo();

that.go();

}

voidgo(){

frame = newJFrame("Box Layout example");

Container contentPane = frame.getContentPane();

bv = Box.createVerticalBox();

bv.add(newJLabel("Monday"));

bv.add(newJLabel("Tuesday"));

bv.add(newJLabel("Wednesday"));

bv.add(newJLabel("Thursday"));

bv.add(newJLabel("Friday"));

bv.add(newJLabel("Saturday"));

bv.add(newJLabel("Sunday"));

contentPane.add(bv,BorderLayout.CENTER);

bh = Box.createHorizontalBox();

bh.add(newJButton("Yes"));

bh.add(newJButton("No"));

bh.add(newJButton("Cancel"));

contentPane.add(bh,BorderLayout.SOUTH);

frame.setSize(300,200);

//frame.pack();

frame.setVisible(true);

}

}

//the specific Box class for BoxLayout Container

import java.awt.*;

import javax.swing.*;

public class BoxDemo{

private JFrame frame;

private Box bv,bh;

public static void main(String arg[]){

BoxDemo that = new BoxDemo();

that.go();

}

void go(){

frame = new JFrame("Box Layout example");

Container contentPane = frame.getContentPane();

bv = Box.createVerticalBox();

bv.add(new JLabel("Monday"));

bv.add(new JLabel("Tuesday"));

bv.add(new JLabel("Wednesday"));

bv.add(new JLabel("Thursday"));

bv.add(new JLabel("Friday"));

bv.add(new JLabel("Saturday"));

bv.add(new JLabel("Sunday"));

contentPane.add(bv,BorderLayout.CENTER);

bh = Box.createHorizontalBox();

bh.add(new JButton("Yes"));

bh.add(new JButton("No"));

bh.add(new JButton("Cancel"));

contentPane.add(bh,BorderLayout.SOUTH);

frame.setSize(300,200);

//frame.pack();

frame.setVisible(true);

}

}

6.6Box类提供的创建不可组件方法

publicstaticComponent createHorizontalGlue()

publicstaticComponent createHorizontalStrut(intwidth)

publicstaticComponent createVerticalGlue()

publicstaticComponent createVerticalStrut(intheight)

publicstaticComponent createRigidArea(Dimension d)

public static Component createHorizontalGlue()

public static Component createHorizontalStrut(int width)

public static Component createVerticalGlue()

public static Component createVerticalStrut(int height)

public static Component createRigidArea(Dimension d)

6.6.1参考代码

//to create invisible component

importjava.awt.*;

importjavax.swing.*;

publicclassGlueAndStrut{

privateJFrame frame;

privateBox b1,b2,b3,b4;

publicstaticvoidmain(String args[]){

GlueAndStrut that = newGlueAndStrut();

that.go();

}

voidgo(){

frame = newJFrame("Glue And Strut");

Container contentPane = frame.getContentPane();

contentPane.setLayout(newGridLayout(4,1));

b1 = Box.createHorizontalBox();

b1.add(newJLabel("Box 1:"));

b1.add(newJButton("Yes"));

b1.add(newJButton("No"));

b1.add(newJButton("Cancel"));

b2 = Box.createHorizontalBox();

b2.add(newJLabel("Box 2:"));

b2.add(newJButton("Yes"));

b2.add(newJButton("No"));

b2.add(Box.createHorizontalGlue());

b2.add(newJButton("Cancel"));

b3 = Box.createHorizontalBox();

b3.add(newJLabel("Box 3:"));

b3.add(newJButton("Yes"));

b3.add(newJButton("No"));

b3.add(Box.createHorizontalStrut(20));

b3.add(newJButton("Cancel"));

b4 = Box.createHorizontalBox();

b4.add(newJLabel("Box 4:"));

b4.add(newJButton("Yes"));

b4.add(newJButton("No"));

b4.add(Box.createRigidArea(newDimension(10,10)));

b4.add(newJButton("Cancel"));

contentPane.add(b1);

contentPane.add(b2);

contentPane.add(b3);

contentPane.add(b4);

frame.setSize(300,200);

frame.setVisible(true);

}

}

//to create invisible component

import java.awt.*;

import javax.swing.*;

public class GlueAndStrut{

private JFrame frame;

private Box b1,b2,b3,b4;

public static void main(String args[]){

GlueAndStrut that = new GlueAndStrut();

that.go();

}

void go(){

frame = new JFrame("Glue And Strut");

Container contentPane = frame.getContentPane();

contentPane.setLayout(new GridLayout(4,1));

b1 = Box.createHorizontalBox();

b1.add(new JLabel("Box 1:"));

b1.add(new JButton("Yes"));

b1.add(new JButton("No"));

b1.add(new JButton("Cancel"));

b2 = Box.createHorizontalBox();

b2.add(new JLabel("Box 2:"));

b2.add(new JButton("Yes"));

b2.add(new JButton("No"));

b2.add(Box.createHorizontalGlue());

b2.add(new JButton("Cancel"));

b3 = Box.createHorizontalBox();

b3.add(new JLabel("Box 3:"));

b3.add(new JButton("Yes"));

b3.add(new JButton("No"));

b3.add(Box.createHorizontalStrut(20));

b3.add(new JButton("Cancel"));

b4 = Box.createHorizontalBox();

b4.add(new JLabel("Box 4:"));

b4.add(new JButton("Yes"));

b4.add(new JButton("No"));

b4.add(Box.createRigidArea(new Dimension(10,10)));

b4.add(new JButton("Cancel"));

contentPane.add(b1);

contentPane.add(b2);

contentPane.add(b3);

contentPane.add(b4);

frame.setSize(300,200);

frame.setVisible(true);

}

}

6.6.2执行结果

admin

admin

7.不使用布局管理器

7.1一般做法

a.setLayout(null)将布局管理器设置为空

b.setBounds(int x,int y,int width,int height)设置组件的位置与大小

7.2参考代码

importjava.awt.*;

importjavax.swing.*;

publicclassNullLayoutDemo{

privateJFrame frame;

privateJButton b1,b2,b3;

publicstaticvoidmain(String args[]){

NullLayoutDemo that = newNullLayoutDemo();

that.go();

}

privatevoidgo(){

frame = newJFrame("Null Layout Demo");

Container contentPane = frame.getContentPane();

//set the layout as null;

contentPane.setLayout(null);

//add the buttons

b1 = newJButton("Yes");

contentPane.add(b1);

b2 = newJButton("No");

contentPane.add(b2);

b3 = newJButton("Cancel");

contentPane.add(b3);

//set the buttons' pos and size;

b1.setBounds(30,15,75,20);

b2.setBounds(150,15,75,50);

b3.setBounds(150,100,75,20);

frame.setSize(300,200);

frame.setVisible(true);

}

}

import java.awt.*;

import javax.swing.*;

public class NullLayoutDemo{

private JFrame frame;

private JButton b1,b2,b3;

public static void main(String args[]){

NullLayoutDemo that = new NullLayoutDemo();

that.go();

}

private void go(){

frame = new JFrame("Null Layout Demo");

Container contentPane = frame.getContentPane();

//set the layout as null;

contentPane.setLayout(null);

//add the buttons

b1 = new JButton("Yes");

contentPane.add(b1);

b2 = new JButton("No");

contentPane.add(b2);

b3 = new JButton("Cancel");

contentPane.add(b3);

//set the buttons' pos and size;

b1.setBounds(30,15,75,20);

b2.setBounds(150,15,75,50);

b3.setBounds(150,100,75,20);

frame.setSize(300,200);

frame.setVisible(true);

}

}

7.3执行结果

admin

admin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值