图形用户界面编辑

package texet;

import javax.swing.*;
import java.awt.*;
public class text extends JFrame {
     
public text(){
     
this.setTitle("第一个界面");
this.setBounds(300, 300, 600, 450);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
     
new text();
}
}

程序功能:添加控件,使用按钮控制布局。

package texet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class text extends JFrame implements ActionListener{
     
private Container cont;
private JButton btnLeft;
private JButton btnCenter;
private JButton btnRight;
private JButton btnBorder;
private JButton btnGrid;
private JLabel labHit;
public text (){
     
this.setTitle("第一个界面");
this.setBounds(300, 300, 600, 450);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
cont = this.getContentPane();
cont.setLayout(new GridLayout(3, 9));
initCompent();
addCompent();
this.setVisible(true);
}
public void initCompent(){
     
btnLeft = new JButton("LEFT");
btnLeft.addActionListener(this);
btnRight = new JButton("RIGHT");
btnRight.addActionListener(this);
btnCenter = new JButton("CENTER");
btnCenter.addActionListener(this);
btnBorder = new JButton("BORDER");
btnBorder.addActionListener(this);
btnGrid = new JButton("GRID");
btnGrid.addActionListener(this);
labHit = new JLabel("提示");
}
public void addCompent(){
     
cont.add(btnLeft);
cont.add(btnCenter);
cont.add(btnRight);
cont.add(btnBorder);
cont.add(btnGrid);
cont.add(labHit);
}
public void addBorderCompent(){
     
cont.add(btnLeft, "West");
cont.add(btnCenter, "Center");
cont.add(btnRight, "East");
cont.add(btnBorder, "North");
cont.add(btnGrid, "South");
}
public void actionPerformed(ActionEvent aEvt) {
     
if (btnLeft == aEvt.getSource()) {
     
cont.setLayout(new FlowLayout(FlowLayout.LEFT));
labHit.setText("流式布局管理器左靠齐");
} else {
     
if (btnRight == aEvt.getSource()) {
     
cont.setLayout(new FlowLayout(FlowLayout.RIGHT));
labHit.setText("流式布局管理器右靠齐");
} else {
     
if (btnCenter == aEvt.getSource()) {
     
cont.setLayout(new FlowLayout(FlowLayout.CENTER));
labHit.setText("流式布局管理器中靠齐");
} else {
     
if (btnBorder == aEvt.getSource()) {
     
cont.setLayout(new BorderLayout());
addBorderCompent();
labHit.setText("边界布局管理器");
} else {
     
if (btnGrid == aEvt.getSource()) {
     
cont.setLayout(new GridLayout(3, 2));
labHit.setText("网格布局管理器");
}
}
}
}
}
}
public static void main(String[] args){
     
new text();
}
}

程序中添加菜单,并使用菜单控制布局。

package texet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class text extends JFrame implements ActionListener {
     
private Container cont;
private JButton btnLeft;
private JButton btnCenter;
private JButton btnRight;
private JButton btnBorder;
private JButton btnGrid;
private JLabel labHit;
private JMenuBar menuBar;
private JMenu mOperation;
private JMenu mHelp;
private JMenuItem mItemFlowLeft;
private JMenuItem mItemFlowCenter;
private JMenuItem mItemFlowRight;
private JMenuItem mItemBorder;
private JMenuItem mItemGrid;
private JPanel panelMenu;
private JPanel panelCompent;
public text () {
     
this.setTitle("第一个界面");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(400, 300);
this.setLocation(300, 300);
cont = this.getContentPane();
this.setLayout(null);
panelMenu = new JPanel();
panelMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
panelMenu.setBounds(0, 0, 400, 30);
panelCompent = new JPanel();
panelCompent.setLayout(new GridLayout(3, 2));
panelCompent.setBounds(0, 30, 400, 240);
cont.add(panelMenu);
cont.add(panelCompent);
addMenu();
panelMenu.add(menuBar);
btnLeft = new JButton("Left");
btnCenter = new JButton("Center");
btnRight = new JButton("Right");
btnBorder = new JButton("边界");
btnGrid = new JButton("网格");
labHit = new JLabel("网格布局管理器");
panelCompent.add(btnLeft);
panelCompent.add(btnCenter);
panelCompent.add(btnRight);
panelCompent.add(btnBorder);
panelCompent.add(btnGrid);
panelCompent.add(labHit);
btnLeft.addActionListener(this);
btnCenter.addActionListener(this);
btnRight.addActionListener(this);
btnBorder.addActionListener(this);
btnGrid.addActionListener(this);
this.setVisible(true);
}
public void addMenu() {
     
menuBar = new JMenuBar();
mOperation = new JMenu("布局操作");
mHelp = new JMenu("帮助");
mItemFlowLeft = new JMenuItem("流式左靠齐");
mItemFlowCenter = new JMenuItem("流式居中");
mItemFlowRight = new JMenuItem("流式右靠齐");
mItemBorder = new JMenuItem("边界");
mItemGrid = new JMenuItem("网格");
mItemFlowLeft.addActionListener(this);
mItemFlowCenter.addActionListener(this);
mItemFlowRight.addActionListener(this);
mItemBorder.addActionListener(this);
mItemGrid.addActionListener(this);
mOperation.add(mItemFlowLeft);
mOperation.add(mItemFlowCenter);
mOperation.add(mItemFlowRight);
mOperation.add(mItemBorder);
mOperation.add(mItemGrid);
menuBar.add(mOperation);
menuBar.add(mHelp);
}
public void actionPerformed(ActionEvent aEvt) {
     
if ((aEvt.getSource() == btnLeft)
|| (aEvt.getSource() == mItemFlowLeft)) {
     
panelCompent.setLayout(new FlowLayout(FlowLayout.LEFT));
labHit.setText("流式布局管理器左靠齐");
} else {
     
if ((aEvt.getSource() == btnRight)
|| (aEvt.getSource() == mItemFlowRight)) {
     
panelCompent.setLayout(new
FlowLayout(FlowLayout.RIGHT));
labHit.setText("流式布局管理器右靠齐");
} else {
     
if ((aEvt.getSource() == btnCenter)
|| (aEvt.getSource() == mItemFlowCenter)) {
     
panelCompent.setLayout(new
FlowLayout(FlowLayout.CENTER));
labHit.setText("流式布局管理器居中");
} else {
     
if ((aEvt.getSource() == btnGrid)
|| (aEvt.getSource() == mItemGrid)) {
     
panelCompent.setLayout(new GridLayout(3, 2));
labHit.setText("网格布局管理器");
} else {
     
if ((aEvt.getSource() == btnBorder)
|| (aEvt.getSource() == mItemBorder)) {
     
panelCompent.setLayout(new BorderLayout());
labHit.setText("边界布局管理器左靠齐");
panelCompent.add(btnLeft, "West");
panelCompent.add(btnCenter, "Center");
panelCompent.add(btnRight, "East");
panelCompent.add(btnBorder, "North");
panelCompent.add(btnGrid, "South");
}
}
}
}
}
}
public static void main(String[] args) {
     
new text ();
}
}

程序中添加工具栏,并使用工具栏控制布局。需在当前目录下存放5
个图片文件

package texet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class text extends JFrame implements ActionListener {
     
private Container cont;
private JPanel panelMenu;
private JPanel panelToolBar;
private JPanel panelCompent;
private JButton btnLeft;
private JButton btnCenter;
private JButton btnRight;
private JButton btnBorder;
private JButton btnGrid;
private JLabel labHit;
private JMenuBar menuBar;
private JMenu menuOperation;
private JMenu menuHelp;
private JMenuItem mItemFlowLeft;
private JMenuItem mItemFlowCenter;
private JMenuItem mItemFlowRight;
private JMenuItem mItemBorder;
private JMenuItem mItemGrid;
private JToolBar toolBar;
private JButton tBtnLeft;
private JButton tBtnCenter;
private JButton tBtnRight;
private JButton tBtnBorder;
private JButton tBtnGrid;
public text() {
     
this.setTitle("第一个界面");
this.setBounds(300, 300, 600, 450);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
cont = this.getContentPane();
cont.setLayout(null);
initPanel();
initMenu();
initToolBar();
initCompent();
addMenu();
addCompent();
this.setVisible(true);
}
public void initPanel() {
     
panelMenu = new JPanel();
panelMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
panelMenu.setBounds(0, 0, 600, 30);
panelToolBar = new JPanel();
panelToolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
panelToolBar.setBounds(0, 30, 600, 50);
panelCompent = new JPanel();
panelCompent.setLayout(new GridLayout(3, 2));
panelCompent.setBounds(0, 80, 600, 350);
cont.add(panelMenu);
cont.add(panelToolBar);
cont.add(panelCompent);
}
public void initMenu() {
     
menuBar = new JMenuBar();
menuOperation = new JMenu("布局操作");
menuHelp = new JMenu("帮助");
mItemFlowLeft = new JMenuItem("流式左布局");
mItemFlowLeft.addActionListener(this);
mItemFlowCenter = new JMenuItem("流式居中");
mItemFlowCenter.addActionListener(this);
mItemFlowRight = new JMenuItem("流式右布局");
mItemFlowRight.addActionListener(this);
mItemBorder = new JMenuItem("边界布局");
mItemBorder.addActionListener(this);
mItemGrid = new JMenuItem("网格布局");
mItemGrid.addActionListener(this);
}
public void initToolBar() {
     
toolBar = new JToolBar();
tBtnLeft = new JButton(new ImageIcon("COPY.jpg"));
tBtnLeft.addActionListener(this);
tBtnLeft.setToolTipText("流式左布局");
tBtnCenter = new JButton(new ImageIcon("HELP.jpg"));
tBtnCenter.addActionListener(this);
tBtnCenter.setToolTipText("流式居中");
tBtnRight = new JButton(new ImageIcon("FIND.jpg"));
tBtnRight.addActionListener(this);
tBtnRight.setToolTipText("流式右布局");
tBtnBorder = new JButton(new ImageIcon("CUT.jpg"));
tBtnBorder.addActionListener(this);
tBtnBorder.setToolTipText("边界布局");
tBtnGrid = new JButton(new ImageIcon("OPEN.jpg"));
tBtnGrid.addActionListener(this);
tBtnGrid.setToolTipText("网格布局");
toolBar.add(tBtnLeft);
toolBar.add(tBtnCenter);
toolBar.add(tBtnRight);
toolBar.add(tBtnBorder);
toolBar.add(tBtnGrid);
panelToolBar.add(toolBar);
}
public void initCompent() {
     
btnLeft = new JButton("LEFT");
btnLeft.addActionListener(this);
btnRight = new JButton("RIGHT");
btnRight.addActionListener(this);
btnCenter = new JButton("CENTER");
btnCenter.addActionListener(this);
btnBorder = new JButton("BORDER");
btnBorder.addActionListener(this);
btnGrid = new JButton("GRID");
btnGrid.addActionListener(this);
labHit = new JLabel("提示");
}
public void addMenu() {
     
menuOperation.add(mItemFlowLeft);
menuOperation.add(mItemFlowCenter);
menuOperation.add(mItemFlowRight);
menuOperation.add(mItemBorder);
menuOperation.add(mItemGrid);
menuBar.add(menuOperation);
menuBar.add(menuHelp);
panelMenu.add(menuBar);
}
public void addCompent() {
     
panelCompent.add(btnLeft);
panelCompent.add(btnCenter);
panelCompent.add(btnRight);
panelCompent.add(btnBorder);
panelCompent.add(btnGrid);
panelCompent.add(labHit);
}
public void addBorderCompent() {
     
panelCompent.add(btnLeft, "West");
panelCompent.add(btnCenter, "Center");
panelCompent.add(btnRight, "East");
panelCompent.add(btnBorder, "North");
panelCompent.add(btnGrid, "South");
}
public void actionPerformed(ActionEvent aEvt) {
     
if ((btnLeft == aEvt.getSource())
|| (mItemFlowLeft == aEvt.getSource())
|| (tBtnLeft == aEvt.getSource())) {
     
panelCompent.setLayout(new FlowLayout(FlowLayout.LEFT));
labHit.setText("流式布局管理器左靠齐");
} else {
     
if ((btnRight == aEvt.getSource())
|| (mItemFlowRight == aEvt.getSource())
|| (tBtnRight == aEvt.getSource())) {
     
panelCompent.setLayout(new
FlowLayout(FlowLayout.RIGHT));
labHit.setText("流式布局管理器右靠齐");
} else {
     
if ((btnCenter == aEvt.getSource())
|| (mItemFlowCenter == aEvt.getSource())
|| (tBtnCenter == aEvt.getSource())) {
     
panelCompent.setLayout(new
FlowLayout(FlowLayout.CENTER));
labHit.setText("流式布局管理器中靠齐");
} else {
     
if ((btnBorder == aEvt.getSource())
|| (mItemBorder == aEvt.getSource())
|| (tBtnBorder == aEvt.getSource())) {
     
panelCompent.setLayout(new BorderLayout());
addBorderCompent();
labHit.setText("边界布局管理器");
} else {
     
if ((btnGrid == aEvt.getSource())
|| (mItemGrid == aEvt.getSource())
|| (tBtnGrid == aEvt.getSource())) {
     
panelCompent.setLayout(new GridLayout(3, 2));
labHit.setText("网格布局管理器");
}
}
}
}
}
}
public static void main(String[] args) {
     
new text();
}
}

你可能感兴趣的:(java,用户界面)