图形用户界面小结

来源:互联网 发布:安东尼生涯数据 编辑:程序博客网 时间:2024/06/14 01:30

1. 界面布局

在用setBounds(int x, int y, int width, int height)对组件进行绝对定位时,在设计复杂的界面时,记得最后要向主容器中添加一个空的组件(例如:空的Label标签)。不然最后一个组件会错位,不能实现正确的绝对定位。

2. ScrollPane类应用小结:

要想建一个拥有滚动条的组件(例如:为JTextArea添加滚动条)有以下几种方法:

第一种:

textArea = new JTextArea() ;

textArea.setEditable(true);

textArea.setLineWrap(true);

scrollPane = new JScrollPane(textArea,JScrollPane.

VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

第二种:

textArea = new JTextArea() ;

textArea.setEditable(true);

textArea.setLineWrap(true);

scrollPane = new JScrollPane(JScrollPane.

VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scrollPane.getViewport().add(textArea) ;

3. 移除容器中的所有元素:调用容器类中的 removeAll()方法

scrollPane.getViewport().removeAll();

northPanelChildPanel01.removeAll();

4. 给组件设置边框

例1:

testBorder = new TitledBorder("答题区");

testScrollPane.setBorder(testBorder);

例2:

/*一个TitleBorder标题占15个像素*/

contentBorder = new TitledBorder("题目信息区");contentScrollPane.setBorder(contentBorder);

contentScrollPane.setBounds(5, 95, 755, 350);

5. 给组件设置规定的大小:

examButton = new JButton("进入考试");

examButton.setPreferredSize(new Dimension(90,38));

6. 在标签上添加图片

imageLabel = new JLabel(new ImageIcon("img/image.jpg"));

7. 显示提示框

JOptionPane.showMessageDialog(null, new String("学号不存在,请核实后再登录"), "提示框" , JOptionPane.ERROR_MESSAGE);

8. 关闭窗口的两种方法

第一种:

frame.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

System.exit(1);

}

});

第二种:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

原创粉丝点击