Java语言程序设计教程(魏永红版)第8章课本源码-创新互联
【例8.1】通过继承JFrame类定义窗体。
标题名称:Java语言程序设计教程(魏永红版)第8章课本源码-创新互联
本文URL:http://ybzwz.com/article/hoiej.html
import javax.swing.*;
public class JFrameDemo extends JFrame {
public JFrameDemo(boolean b) {
this.setTitle("自定义窗体");
this.setBounds(80,80,300,200);
this.setResizable(b);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame frame = new JFrameDemo(false);
}
}
【例8.4】创建JTextArea文本域组件,并将其放入滚动面板JScrollPane中,最后将滚动面板加入窗体容器中。import javax.swing.*;
public class JScrollPaneDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("JScrollPane应用实例");
JTextArea ta = new JTextArea("带滚动条的面板演示实例!!!");
JScrollPane sp = new JScrollPane(ta);
jf.add(sp);
jf.setSize(300,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
【例8.5】创建水平方向分隔面板outerPane和垂直方向分隔面板interPane,并将inter-Pane设置为outerPane面板的右边部分,同时设置分隔条的初始位置和大小。import javax.swing.*;
public class JSplitPaneDemo extends JFrame{
private JSplitPane outerPane,interPane;
public JSplitPaneDemo(){
outerPane =new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true);
outerPane.setOneTouchExpandable(true);
interPane =new JSplitPane(JSplitPane.VERTICAL_SPLIT);
interPane.setOneTouchExpandable(true);
interPane.setLeftComponent(new JLabel("右上窗格"));
interPane.setRightComponent(new JLabel("右下窗格"));
outerPane.setLeftComponent(new JLabel("左边窗格"));
outerPane.setRightComponent(interPane);
outerPane.setDividerLocation(60);
interPane.setDividerLocation(70);
outerPane.setDividerSize(10);
interPane.setDividerSize(10);
this.add(outerPane);
this.setTitle("分隔面板应用");
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[]args)
{
new JSplitPaneDemo();
}
}
【例8.6】使用FlowLayout布局管理器布局JFrame组件中的5个按钮。import java.awt.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("流布局管理器");
FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
flowLayout.setHgap(10);
flowLayout.setVgap(20);
frame.setLayout(flowLayout);
for(int i = 1;i<= 5;i++) {
frame.add(new JButton("Button" + i));
frame.setSize(400,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
【例8.7】按照BorderLayout布局管理器在JFrame窗体中放置5个按钮。import java.awt.*;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame {
public BorderLayoutDemo() {
super("BorderLayout布局管理器");
BorderLayout layout = new BorderLayout(5,5);
this.setLayout(layout);
JButton buttonEast = new JButton("东");
JButton buttonWest = new JButton("西");
JButton buttonSouth = new JButton("南");
JButton buttonNorth = new JButton("北");
JButton buttonCenter = new JButton("中");
this.add(buttonNorth , BorderLayout.NORTH);
this.add(buttonSouth , BorderLayout.SOUTH);
this.add(buttonEast , BorderLayout.EAST);
this.add(buttonWest , BorderLayout.WEST);
this.add(buttonCenter , BorderLayout.CENTER);
this.setSize(300 , 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
BorderLayoutDemo application = new BorderLayoutDemo();
}
}
【例8.8】按GridLayout布局管理器部署JFrame窗体中6个按钮。import java.awt.*;
import javax.swing.*;
public class GridLayoutDemo extends JFrame {
private final String names[] = {"one" , "two" , "three" ,
"four" , "five" , "six"};
public GridLayoutDemo() {
super("GridLayout布局管理器");
GridLayout grid1 = new GridLayout(2,2,5,5);
this.setLayout(grid1);
JButton buttons[] = new JButton[names.length];
for(int count = 0;count< names.length;count++) {
buttons[count] = new JButton(names[count]);
this.add(buttons[count]);
}
this.setSize(300,150);
this.setVisible(true);
}
public static void main(String args[]) {
GridLayoutDemo application = new GridLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
【例8.9】在JFrame中创建cardPanel和controlPanel两个JPanel容器,并使用默认布局管理器将它们分别部署到“Center”和“South”区域。cardPanel容器的布局管理器设置为CardLayout,部署4个按钮组件;controlPanel容器的布局管理器采用默认,部署两个按钮组件。import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutDemo extends JFrame {
private CardLayout cardLayout;
private JPanel cardPanel;
public CardLayoutDemo() {
super("CardLayout布局管理器");
cardPanel = new JPanel();
cardLayout = new CardLayout(10,10);
cardPanel.setLayout(cardLayout);
for(int i = 1;i<= 4;i++) {
cardPanel.add("button" + i , new JButton("button" + i));
}
this.add(cardPanel , BorderLayout.CENTER);
JButton nextButton = new JButton("Next");
JButton prevButton = new JButton("Previous");
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.previous(cardPanel);
}
});
prevButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.previous(cardPanel);
}
});
JPanel controlPanel = new JPanel();
controlPanel.add(prevButton);
controlPanel.add(nextButton);
this.add(controlPanel , BorderLayout.SOUTH);
}
public static void main(String[] args) {
CardLayoutDemo cardDemo = new CardLayoutDemo();
cardDemo.setSize(300,200);
cardDemo.setVisible(true);
cardDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
【例8.12】本例以窗口JFrame作为事件源演示WindowEvent事件处理。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowEventDemo extends JFrame implements WindowConstants{
public WindowEventDemo(){
super("WindowEvent处理应用");
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
}
public void windowActivated(WindowEvent e){
System.out.println("This window is activated.");
}
public void windowClosed(WindowEvent e){
System.out.println("This window has closed.");
System.exit(0);
}
public void windowClosing(WindowEvent e) {
System.out.println("This window is closing now.");
this.dispose();
}
public void windowDeactivated(WindowEvent e){
System.out.println("This window is deactivated");
}
public void windowDeiconified(WindowEvent e){
System.out.println("This window is deiconified");
}
public void windowIconified(WindowEvent e){
System.out.println("This window is iconified");
}
public void windowOpened(WindowEvent e){
System.out.println("This window is opened");
}
public static void main(String[] args){
new WindowEventDemo();
}
}
【例8.13】以JTextArea组件作为产生KeyEvent事件的事件源的键盘事件处理程序。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyEventDemo extends JFrame implements KeyListener {
private JTextField tf;
public KeyEventDemo(){
super("KeyEvent处理应用");
tf=new JTextField(15);
tf.addKeyListener(this);
JPanel panel=new JPanel();
panel.add(new JLabel("输入框"));
panel.add(tf);
this.add(panel,BorderLayout.CENTER);
this.setSize(300,80);
this.setVisible(true);
}
public void keyPressed(KeyEvent e){}
public void keyRelesased(KeyEvent e){}
public void keyTyped(KeyEvent e){
System.out.println(e.getKeyChar());
}
public static void main(String[] args){
new KeyEventDemo();
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
创新互联建站主营城阳网站建设的网络公司,主营网站建设方案,重庆APP软件开发,城阳h5微信小程序搭建,城阳网站营销推广欢迎城阳等地区企业咨询标题名称:Java语言程序设计教程(魏永红版)第8章课本源码-创新互联
本文URL:http://ybzwz.com/article/hoiej.html