码迷,mamicode.com
首页 > 编程语言 > 详细

Java-第15章图形用户界面设计例题

时间:2020-05-24 16:58:27      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:布局   容器   lips   信息   efault   汽车   readline   close   npe   

Example15_1.java JFrame常用方法

import javax.swing.*;
import static javax.swing.JFrame.*;
public class Example15_1 {
    public static void main(String args[]) {
        JFrame window1=new JFrame("撤销窗口");
        JFrame window2=new JFrame("退出程序");
        window1.setBounds(60,100,188,108);
        window2.setBounds(260,100,188,108);
        window1.setVisible(true);
        window1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        window2.setVisible(true);
        window2.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        
    }
}

技术图片

Example 15_2.java菜单条、菜单、菜单项

public class Example15_2 {
    public static void main(String args[]) {
        WindowMenu win=new WindowMenu("带菜单的窗口",20,30,200,190);
        
    }

}
import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import static javax.swing.JFrame.*;
public class WindowMenu extends JFrame{
    JMenuBar menubar ;
    JMenu menu,subMenu;
    JMenuItem item1,item2;
    public WindowMenu() {}
    public WindowMenu(String s,int x,int y,int w,int h) {
        init(s);
        setLocation(x,y);
        setSize(w,h);
        setVisible (true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }
void init(String s) {
    setTitle(s);
    menubar =new JMenuBar();
    menu=new JMenu("菜单");
    subMenu=new JMenu("软件项目");
    item1=new JMenuItem("JAVA话题",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\a.gif"));//图片所在位置
    item2=new JMenuItem("动画话题",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\b.gif"));//图片所在位置
item1.setAccelerator(KeyStroke.getKeyStroke(‘A‘)); 
item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menu.add(item1);
menu.addSeparator();
menu.add(item2);
menu.add(subMenu);
subMenu.add(
new JMenuItem("汽车销售系统",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\c.gif")));
subMenu.add(
new JMenuItem("农场信息系统",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\d.gif")));
menubar.add(menu);
setJMenuBar(menubar);
}
}

技术图片

技术图片技术图片技术图片技术图片

Example15_3 常用组件

public class Example15_3 {
public static void main (String args[]) {
    ComponentInWindow win =new ComponentInWindow();
    win.setBounds(100,100,310,260);
    win.setTitle("常用组件");
    
}
}
import java.awt.*;
import javax.swing.*;
import static javax.swing.JFrame.*;
public class ComponentInWindow extends JFrame{
    JTextField text;
    JButton button;
    JCheckBox checkBox1,checkBox2,checkBox3 ;
    JRadioButton radio1,radio2;
    ButtonGroup group;
    JComboBox comBox;
    JTextArea area ;
    public ComponentInWindow() {
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
void init() {
    setLayout(new FlowLayout());
    add(new JLabel("文本框"));
    text=new JTextField(10);
    add(text);
    add(new JLabel("按钮:"));
    button =new JButton("确定");
    add(button);
    add(new JLabel("选择框:")) ;
    checkBox1=new JCheckBox("喜欢音乐");
    checkBox2=new JCheckBox("喜欢旅游");
    checkBox3=new JCheckBox("喜欢篮球");
    add(checkBox1);
    add(checkBox2);
    add(checkBox3);
    add(new JLabel("单选按钮:"));
    group=new ButtonGroup();
    radio1=new JRadioButton("男");
    radio2=new JRadioButton("女");
    group.add(radio1);
    group.add(radio2);
    add(radio1);
    add(radio2);
    add(new JLabel("下拉列表:"));
    comBox=new JComboBox();
    comBox.addItem("音乐天地");
    comBox.addItem("武术天地");
    comBox.addItem("象棋乐园");
    add(comBox);
    add(new JLabel("文本区:"));
    area=new JTextArea(6,12);
    add(new JScrollPane(area));
    
    
}
}

技术图片

Example15_4常用布局

public class Example15_4{
    public static void main(String args[])
    {
        WindowBoxLayout win=new WindowBoxLayout () ;
        win. setBounds (100, 100, 310,260);
        win. setTitle("嵌套盒式布局容器");
    }
}
import javax.swing.*;
public class WindowBoxLayout extends JFrame{
Box baseBox,boxV1,boxV2;
public WindowBoxLayout() {
    setLayout (new java.awt. FlowLayout()) ;
init() ;
setVisible (true) ;
setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE) ;
}
void init () {
boxV1=Box.createVerticalBox() ;
boxV1. add (new JLabel ("姓名")) ;
boxV1.add (Box.createVerticalStrut(8)) ;
boxV1.add (new JLabel ("email")) ;
boxV1. add (Box.createVerticalStrut(8)) ;
boxV1.add (new JLabel ("职业")) ;
boxV2=Box. createVerticalBox() ;
boxV2. add (new JTextField(10) ) ;
boxV2.add (Box.createVerticalStrut(8)) ;
boxV2.add(new JTextField(10)) ;
boxV2. add (Box. createVerticalStrut(8));
boxV2. add (new JTextField(10));
baseBox=Box.createHorizontalBox() ;
baseBox.add (boxV1) ;
baseBox.add (Box.createVerticalStrut(10));
baseBox.add(boxV2) ;
add (baseBox) ;

}
}

技术图片

Example15_6 ActionEvent事件

public class Example15_6 {
public static void main(String args[]) {

WindowActionEvent win=new WindowActionEvent();
win.setBounds(100, 100, 460,360); 
win. setTitle ("处理ActionEvent事件");
}
}
import java.awt. *;

import javax.swing.*;

public class WindowActionEvent extends JFrame{

JTextField inputText;

JTextArea textShow;

JButton button;

PoliceListen listener;

public WindowActionEvent() {

init() ;

setVisible(true) ;

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

void init() {

setLayout (new FlowLayout()) ;

inputText = new JTextField(10);

button = new JButton("读取");

textShow = new JTextArea(9, 30) ;

listener= new PoliceListen();

listener.setJTextField(inputText);

listener.setJTextArea (textShow) ;

inputText.addActionListener (listener); 

add (inputText) ;

add (button) ;

add (new JScrollPane (textShow)) ;
}
}
import java.awt.event.*;
import java.io.*;
import javax. swing.*;

public class PoliceListen implements ActionListener{
JTextField textInput;
JTextArea textShow;
public void setJTextField (JTextField text) {
    textInput = text;
}
public void setJTextArea (JTextArea area) {
    textShow = area;
}
public void actionPerformed (ActionEvent e) {
textShow.setText (null);
try { File file = new File (textInput.getText());
FileReader inOne = new FileReader (file) ;
BufferedReader inTwo = new BufferedReader (inOne) ;String s=null;
while((s=inTwo. readLine()) !=null)
textShow.append(s+"n") ;
inOne.close();
inTwo.close();
}
catch (Exception ee) {
textShow. append (ee.toString());
}
}
}

技术图片

注意:如果出现无法解析的情况,可将错误代码重新键盘输入。

Java-第15章图形用户界面设计例题

标签:布局   容器   lips   信息   efault   汽车   readline   close   npe   

原文地址:https://www.cnblogs.com/zmqzmq828/p/12951221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!