Java图形界面接触

来源:互联网 发布:汽车配件平台源码下载 编辑:程序博客网 时间:2024/05/22 00:17
框架 :JFrame(只可以有一个)
JButton
布局管理器: FlowLayout BorderLayout , GridLayout , CardLayout , GridBagLayout
JDialog


GridLayout(int rows ,int cols , int haap. int vgap) 
面板组件 : JPanel(多个)


单选框组件 : JRadioButton
多选框组件 : JCheckBox


使用:
创建ButtonGroup -> 将 JRadioButton放在ButtonGroup中
下拉框组件: JCombox
列表组件 :   JList
滑动窗格组件 : JScrollPane
拆分窗格: JSplitPane


单行文本框组件 : JTextField
多行文本框组件 JTextArea
选项卡窗格: JTabbedPane




绘图技术:
Component类下:
Paint(Graphics g);//绘制组件的外观
Repaint(); //刷新组件的外观
paint()被调用的情况:
1.窗口最大化
2.窗口的大小发生变化
3.repaint()函数被调用
Graphics类(Graphics g):
/* draw绘制类*/
g.drawLine(int x1,int y1, int x2,int y2)//画直线
g.drawRect(int x,int y ,int widht.int height)//画矩形边框
g.drawOval(int x,int y,int width,int height)//画椭圆边框
g.drawRoundRect(int x,int y,int width,int height)//绘制圆角矩形
g.drawPolygon(Polygon P)//绘制多边形
Polygon p = new Polygon 
p.addPoint(int x1,int y1)
.
.
.
g.drawPolygon(Polygon p);
/* fill填充类*/
g.fillRect(int x,int y,int width,int height)
g.fillOval(int x,int y,int width,int height)
g.fillRountRect(int x,int y,int width,int height)
/*颜色相关*/
g.getColor() //返回当前颜色设置
g.setColor(Color c)//设置当前颜色
Color(int redness,int greenness,int blueness)(0~255)
// Component提供的颜色处理方法
g.setBackground();//设置背景色
g.setForeground();//设置在组建上进行绘制的默认颜色
/*图片处理方法*/
g.drawImage(Image image,int x,int y,null);
/*本地*/
String name = "Test.gif或jpg"
Image image = Toolkit.getDefaultToolkit().getImage(name);
/*网络*/
URL url = new URL("http:\\www.sun.com/im/sun_logo.gif")
Image image = Toolkit.getDefaultToolkit().getImage(url);
Java事件处理机制:-> java.awt.event事件类库


监听器 (Listener)
注册监听器:
eventSourceObject.addEventListener(eventListenerObject);
使用监听器:
实现接口
public class impMyActionListener implement ActionListener
{
public void actionPerformed(ActionEvent event)
{
//相应操作
}
}
impMyActionListener listener = new ActionListener();
JButton button = new JButton("Submit");
button.addActionListener(listener);
捕获窗口事件:
MyWindowListener listener = new MyWindowListener();
frame.addWindowListener(listener);
/*window的七种事件*/
public interface WindowListener
{
void windowOpened(WindowEvent e)
void windowClosing(WindowEvent e)
void windowClosed(WindowEvent e)
void windowIconified(WindowEvent e)
void windowDeiconified(WindowEvent e)
void windowActivated(WindowEvent e)
void windowDeactivated(WindowEvent e)
} //以上方法均需要继承重定义, 不然为空


键盘事件:
public void keyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)
public void ketTyped(KeyEvent e)
鼠标事件:
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
public void mouseClicked(MouseEvent e)
//区别单双击操作 
getClickCount();
MouseMotionListener接口 -> mouseMoved
/*鼠标指针形状设置:*/
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
/*获取鼠标指针的位置:*/
getX(); getY();



布局管理器:
setLayout(new FlowLayout(FlowLayout.LEFT))//左对齐
setLayout(new FlowLayout(FlowLayout.RIGHT))//右对齐
setLayout(new FlowLayout(FlowLayout.CENTER))//居中


文本组件:
JTextField//单行
JTextArea//多行文本
获得文本内容:
add = Integer.parseInt(originText.getText().trim());
跟踪文本内容的修改
DocumentListener resultListener = new ResultListener();
originText.getDocument().addDocumentListener(resultListener);
当文本发生改变时.以下方法之一会被调用
void insertUpdate(DocumentEvent e)
void removeUpdate(DocumentEvent e)
void changedUpdate(DocumentEvent e)
重设文本域的内容
resultText.setText(s);
密码域
JPassworkField//输入时候 显示*以防他人偷窥
JTextArea//多行文本域
多行文本之间的换行:
1.输入ENTER键
2.设置换行属性  
textArea.setLineWrap(true);//这样不会隐式的插入\n
3.给文本区域添加滚动条
JScrollPane scrollPane = new JScrollPane(textArea);
标签:
JLable lable = new JLable("NoWarpArea",JLable.LEFT)//设置一个显示NoWarpArea的标签对象,左对齐
按钮:
JButton
loginButton = new JButton("登陆",new ImageIcon("login.gif"));
//注册事件监听器
loginButton.addActionListener(new LoginAction());
//实现事件监听
private class LoginAction implements ActionListener


Swing类常用的选择类组件
单选框组件:
ButtonGroup group = new ButtonGroup();
JRadioButton button1 = new JRadioButton("EAST",true);
.
.
.
JRadioButton button2 = new JRadioButton("NORTH",false);
//第一个按钮是true 其他是false
复选框组件:
//复选框是否被选择可以用isSelected方法判断
boldCheck = new JCheckBox("Bold");//新建复选框
italicCheck = new JCheckBox("Italic")//新建复选框
boldCheck.isSelected();//判断boldCheck复选框是否被选中
滑块组件:
JSlider ageSlider = new JSlider();
ageSlider = new JSlider(SwingConstants,VERTICAL,0,120,20);
/*添加监听器*/
AgeListener = myAgeListener = new AgeListener();
ageLider.addChangeListener(myAgeListener);
private class AgeListener implement ChangeListener
{
public void stateChanged(ChangedEvent e)
{}
}
/*获取当前值*/
getValue()
边界:
Border border = BorderFactory.createEmptyBorder();
Border titled = BorderFactory.createTitleBorder(border,"Border types");
/*给Panel面板设置一个边界*/
buttonPane.setBorder(titled);


创建菜单:
JMenuBar 菜单栏
JMenu 菜单
JMenuBar.add(JMenu)
JMenu.add(JMenu)
JMenu.add(JMenuItem)
菜单的使用状态:
setEnabled(true or false);
设置快捷键:
fileMenu.setMnemonic('F');
pasteItem = new JMenuItem("Paste",'P');
JMenuItem exitItem = new JMenuItem("exit",'T');
设置exitItem的加速器?
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK));
弹出式菜单:
popup = new JPopupMenu();
cutItemPop = new JMenuItem("cut",'C');
popup.add(cutItemPop);


对话框: JOptionPane
Swing中:提供了四种对话框
showMessageDialog//显示消息等待用户点ok
showConfirmDialog//显示消息并等待确认
showOptionDialog//显示消息等待用户在一组自定义的选项中选择
showInputDialog//显示一条消息并等待输入
0 0
原创粉丝点击