高图形用户界面

上传人:lc****zq 文档编号:54398218 上传时间:2018-09-12 格式:PPT 页数:94 大小:833.50KB
返回 下载 相关 举报
高图形用户界面_第1页
第1页 / 共94页
高图形用户界面_第2页
第2页 / 共94页
高图形用户界面_第3页
第3页 / 共94页
高图形用户界面_第4页
第4页 / 共94页
高图形用户界面_第5页
第5页 / 共94页
点击查看更多>>
资源描述

《高图形用户界面》由会员分享,可在线阅读,更多相关《高图形用户界面(94页珍藏版)》请在金锄头文库上搜索。

1、Chapter 14 Graphical User Components Part 2,Outline 14.1 Introduction 14.2 JTextArea 14.3 Creating a Customized Subclass of JPanel 14.4 JPanel Subclass that Handles Its Own Events 14.5 JSlider 14.6 Windows: Additional Notes 14.7 Using Menus with Frames 14.8 JPopupMenu 14.9 Pluggable Look-and-Feel 14

2、.10 JDesktopPane and JInternalFrame 14.11 JTabbedPane 14.12 Layout Managers: BoxLayout and GridBagLayout,14.1 Introduction,Advanced GUI components Text areas Sliders MenusMultiple Document Interface (MDI)Advanced layout managers BoxLayout GridBagLayout,TextArea类,TextArea(), 文本区的列数和行数取默认值. 文本区有水平和垂直滚

3、动条. TextArea(String s), 文本区的初始字符串为s. 文本区有水平和垂直滚动条. TextArea(int x,int y), 文本框行数为y,列数为x. 文本区有水平和垂直滚动条. TextArea(String s, int x,int y), 文区的初始字符串为s,文本框行数为y, 列数为 x. 文本区有水平和垂直滚动条.,TextArea类,TextArea(String s, int x, int y, int scrollbar) 文本区的初始字符串为s, 文本框行数为y, 列数为 x. scrollbar 取值 TextArea.SCROLLBARS_BOTH

4、 TextArea.SCROLLBARS_VERTICAL_ONLY TextArea.SCROLLBARS_HORIZONTAL_ONLY TextArea.SCROLLBARS_NONE 控制文本区滚动条的显示状态. public void setText(String s), 将文本区中的文本设置为参数s指定的文本,文本区中先前的文本将被清除.,TextArea类,public String getText(), 获取文本区中的文本. public void setEditable(boolean b), 指定文本区的可编辑性.文本区默认是为可编辑的. public boolean is

5、Editable(boolean b), 获取文本区是否是可编辑的, 当文本区是可编辑时, 该方法返回true, 否则返回false. public void insert(String s int x), 在指定位置x处, 插入指定文本s. x是指距文本区开始处字符的个数, x不能大于文本区中字符的个数. public void replaceRange(String s,int start,int end), 用给定新文本s替换从指定位置start开始到指定位置end 结束的文本,start 和end 不能大于文本区中字符的个数.,TextArea类,public void append(

6、String s), 在文本区中尾加文本 int getCaretPosition(), 获取文本区中输入光标的位置. public void setCaretPosition(int position), 设置文本区中输入光标的位置,其中position不能大于文本区中字符的个数. String getSelectedText(), 获取文本区中选中的文本, public int getSelectionStart(), 获取被选中的文本的开始位置. public int getSelectionEnd(), 获取被选中的文本的结束位置.,TextArea类,public void setS

7、electionStart(int n), 设置文本区中被选中的文本的开始位置, 其中n不能大于文本区中字符的个数. public void setSelectionStart(int n), 设置文本区中被选中的文本的结束位置, 其中n不能大于文本区中字符的个数. public void selectAll(), 选中文本中的全部文本. addTextListener(TextListener), 向文本框增加文本监视器. removeTextListener(TextListener), 移去文本框上的文本监视器.,TextArea类,import java.applet.*; impor

8、t java.awt.*; public class Example9_4 extends Applet TextArea text1, text2;public void init() text1 = new TextArea(“我是学生“,6,16);text2 = new TextArea(6,16);add(text1);add(text2);text2.append(“我在学习java语言“);text1.insert(“们“,1);text1.selectAll();int length=text2.getText().length();text2.setSelectionStar

9、t(2); text2.setSelectionEnd(length); ,14.2 JTextArea,JTextAreaArea for manipulating multiple lines of textextends JTextComponent与JTextField不同:没有动作事件。通常用一个外部事件来处理JextArea中的文本,import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextAreaDemo extends JFrame private JTextArea t

10、extArea1, textArea2;private JButton copyButton;/ set up GUIpublic TextAreaDemo() super( “TextArea Demo“ );Box box = Box.createHorizontalBox();String string = “This is a demo string ton“ + “illustrate copying textnfrom one textarea to n“ +“another textarea using annexternal eventn“;/ set up textArea1

11、textArea1 = new JTextArea( string, 10, 15 );box.add( new JScrollPane( textArea1 ) );/ set up copyButtoncopyButton = new JButton( “Copy “ );box.add( copyButton );copyButton.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) textArea2.setText( textArea1.getSelecte

12、dText() ); / end anonymous inner class); / end call to addActionListener,/ set up textArea2textArea2 = new JTextArea( 10, 15 );textArea2.setEditable( false );box.add( new JScrollPane( textArea2 ) );/ add box to content paneContainer container = getContentPane();container.add( box ); / place in Borde

13、rLayout.CENTERsetSize( 425, 200 );setVisible( true ); / end constructor TextAreaDemopublic static void main( String args ) TextAreaDemo application = new TextAreaDemo();application.addWindowListener( new WindowAdapter() public void windowClosing(WindowEvent event) System.exit(0); ); / end class Text

14、AreaDemo,运行结果,14.3 Creating a Customized Subclass of JPanel,Jpanel用来作为接收鼠标事件的专用绘图区域 合并绘图区和Swing的GUI构件会导致图形、GUI构件不能正确显示; 扩展Jpanel用来创建新构件,将图形和GUI构件分开; paintComponent方法:从Jcomponent类继承: 确保在GUI上下文中按正确的次序绘制; 确保完整地保留Swing的绘图机制;,paintComponent方法的覆盖:,14.3 Creating a Customized Subclass of JPanel,JFrame和JAppl

15、et类不是Jcomponent的子类; 不包含paintComponent方法; 直接在 JFrame 或 JApplet 的子类中绘图,需要覆盖 paint 方法。 例:将绘图区域与按钮分开,14.3 Creating a Customized Subclass of JPanel,import java.awt.*; import javax.swing.*; public class CustomPanel extends JPanel public final static int CIRCLE = 1, SQUARE = 2;private int shape;/ use shape

16、 to draw an oval or rectanglepublic void paintComponent( Graphics g ) super.paintComponent( g );if ( shape = CIRCLE ) g.fillOval( 50, 10, 60, 60 );else if ( shape = SQUARE ) g.fillRect( 190, 10, 60, 60 );/ set shape value and repaint CustomPanelpublic void draw( int shapeToDraw ) shape = shapeToDraw

17、;repaint(); / end class CustomPanel,import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CustomPanelTest extends JFrame private JPanel buttonPanel;private CustomPanel myPanel;private JButton circleButton, squareButton;/ set up GUIpublic CustomPanelTest() super( “CustomPanel

18、 Test“ );/ create custom drawing areamyPanel = new CustomPanel();myPanel.setBackground( Color.green );/ set up squareButtonsquareButton = new JButton( “Square“ );squareButton.addActionListener( new ActionListener() / draw a squarepublic void actionPerformed( ActionEvent event ) myPanel.draw( CustomPanel.SQUARE ); ); circleButton = new JButton( “Circle“ );circleButton.addActionListener( new ActionListener() / draw a circlepublic void actionPerformed( ActionEvent event ) myPanel.draw( CustomPanel.CIRCLE ); );,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号