当前位置:文档之家› Java Swing组件基本简介及其应用

Java Swing组件基本简介及其应用

JFrame是所有窗口的源,可以在JFrame的容器中加入各种元素,同时可以调整JFrame的较多属性,借此来进行窗口的设计。
界面的形成是一个构造的过程,要有构造的动作(构造函数)
当界面初始化以后,改变组件参数,相应改变会实时反映出来
利用监听器或者设置响应函数可以做到界面的实时反映
this.getContentPane().setLayout(null);//JFrame中的一个调用镶嵌界面(除菜单栏外所有)的函数->设置布局
this.add(getJLabel(),null);//添加组件
this.setTitle("输入测试");//设置标题
frame.pack();//自动调整窗口大小
基本组件(自携带初始化方法):
标签:JLabel
按钮:JButton
文本条:JTextField
密码条:JPasswordField /setEchoChar()/
文本框:JTextArea /setLineWrap(true),setWrapStyleWord(true)/
滚动条:
JScrollPane scroll=new JScrollPane(jTextField);
scroll.setViewportView(jTextField);
scroll.setBounds(0,0,0,0);
this.add(scroll);
下拉框:JComboBox /addItem()/
单选框:JRadioButton /ButtonGroup/
复选框:JCheckbox
标值栏:JSlider /setMinimum(),setMaximum(),setOrientation(),setValue(),JSlider jSlider=new JSlider(JSlider.vertical,0,100,50),setPaintLabels(true),setMajotTickSpacing(50),setPaintTicks(true),setMinorTickSpacing(5);/
浮动栏:JSpinner
菜单栏:JMenuBar->JMenu->JMenuItem /add(),setIcon()/
工具栏:JToolBar /add()/
提示框:JOptionPane
/*
提示消息:
showMessageDialog(null,"提示","标题",JOptionPane.WARNING_MESSAGE);
JOptionpane.WARNING_MESSAGE
JOptionPane.ERROR_MESSAGE
JOptionPane.PLAIN_MESSAGE
JOptionPane.QUESTION_MESSAGE
https://www.doczj.com/doc/0918072771.html,RMATION_MESSAGE
提供双选:
int i=showOptionDialog(null,"你高兴吗?","标题",JOptionPane.YES_NO_OPTION);
自定义例:
Object options[]={"好啊!","去一边!"};
int m=JOptionPane.showOptionDialog(null,"我可以约你吗","标题",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0])
提供多选:
Object[] obg2={"足球","篮球","排球"};
String s=(String)JOptionPane.showInputDialog(null,"请选择你的爱好:\n","爱好",JOptionPane.PLAIN_MESSAGE,new ImageIcon("icon.png"),obj2,"足球");
提供输入:
JOptionPane.showInputDialog(null,"请输入你的爱好:","title",JOptonPane.PLAIN_MESSAGE,icon,null,"在这里输入")
*/
子窗格:JPane
/*
add()
setlayout()
FlowLayout、<水流>
BorderLayout、<方位>
GridLayout、<网格>
CardLayout、<重叠式>
GridBagLayout<网格组合>
*/
基本方法:
setToolTipText("提示");
setBackground("Color.red");
setText("姓名");
setBounds(34,49,53,18);//左上角坐标x+坐标y+长+宽//y越向下越大
setBorder();
/*BorderFactory.createMatteBorder(1, 5, 1, 1, Color.yellow)*/
/*BorderFactory.createRaisedBevelBorder()*/斜面边框(凸)
/*BorderFactory.createEtchedBorder()*/蚀刻边框
/*BorderFactory.createTitledBorder("标题")*/标题边框

/*BorderFactory.createLineBorder(Color.red, 3)*/线边框
/*BorderFactory.createEmptyBorder(5, 5, 5, 5)*/空白边框
/*BorderFactory.createLoweredBevelBorder()*/斜面边框(凹)
setIcon(new ImageIcon("c:\\Users\\ASUS\\Desktop\\1.png"));
setPressedIcon(Icon pressedIcon)//按下时的图标。
setRolloverIcon(Icon rolloverIcon)//鼠标经过时的图标。
setSelectedIcon(Icon selectedIcon)//选中时的图标。
setRolloverSelectedIcon(Icon rolloverSelectedIcon)//鼠标经过时且被选中状态的图标
setDisabledIcon(Icon disabledIcon)//禁用时显示的图标。例如可以换一张灰度图片
setDisabledSelectedIcon(Icon disabledSelectedIcon) //禁用且被选中状态的图标
/只允许png图标文件/
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
/鼠标经过按钮时候的指针改变/
System.out.println("你点击的按钮上的文字是"+((JButton) (e.getSource())).getText() );























package HaveATry;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class HelloWorld extends JFrame{
private JLabel jLabel;
private JTextField jTextField;
private JButton jButton;
private JComboBox jComboBox;
public HelloWorld()
{
super();
this.setSize(300, 200);
this.getContentPane().setLayout(null);
this.add(getJLabel(), null);
this.add(getJTextField(), null);
this.add(getJButton(), null);
this.add(getJComboBox(),null);
this.setTitle("HelloWorld");
}
private javax.swing.JComboBox getJComboBox() {
String s[]={"苹果","橘子"};
jComboBox=new JComboBox(s);
jComboBox.addItem("榴莲");
jComboBox.setBounds(10, 10, 120, 53);
return jComboBox;
}
private javax.swing.JLabel getJLabel() {
if(jLabel == null) {
jLabel = new javax.swing.JLabel();
jLabel.setBounds(34, 90, 53, 30);
jLabel.setText("Name:");
}
return jLabel;
}
private javax.swing.JTextField getJTextField() {
if(jTextField == null) {
jTextField = new javax.swing.JTextField();
jTextField.setBounds(96, 90, 160, 30);
jTextField.setText("360");
}
return jTextField;
}
private javax.swing.JButton getJButton() {
if(jButton == null) {
jButton = new javax.swing.JButton();
jButton.setBounds(103, 110, 71, 27);
jButton.setText("OK");
jButton.setIcon(new ImageIcon("c:\\Users\\ASUS\\Desktop\\1.png"));
}
return jButton;
}
public void changeSite(HelloWorld w)
{
int s=10;
while(1==1)
{
s=(int)(Math.random()*300);
String ss=s+"";
jButton.setText(ss);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
jButton.setBounds(s,110,71,27);
}
}
public static void main(String[] args)
{
HelloWorld w = new HelloWorld();
w.setVisible(true);
w.changeSite(w);
}
}



















关于Java中GridBagLayout布局管理器的用法。
最近要写一个界面,我却发现一般的布局管理器都不那么好用。上网百度了一下,有人推荐GridBagLayout,却有很多人说GridBagLayout不好用,看不懂。

于是我仔细查了一下java API,感觉掌握GridBagLayout最简单的用法还是蛮简单的,也是很有必要的。因为个人不喜欢绝对定位,而使用相对定位的话就必须用到GridBagLayout,主要是由于其它的几个布局管理器太简单,可操作性差。
总结了GridBagLayout的用法中的关键点如下:

1.要明确一点概念:每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,称为显示区域。
网格的总体方向取决于容器的 ComponentOrientation 属性。对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增。

2.要使用GidBagLayout要先定义一个GridBagConstraints对象。
java API说明如下:“每个由 GridBagLayout 管理的组件都与 GridBagConstraints 的实例相关联。Constraints 对象指定组件在网格中的显示区域以及组件在其显示区域中的放置方式。”
例如,如下几行代码就可以添加其它组件:
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
JFrame f=new JFrame();
f.setLayout(gridbag);
Button button = new Button(name);
gridbag.setConstraints(button, c);
f.add(button);

3.为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints 对象。
即须设置GridBagConstraints 对象的属性。我认为只要能掌握以下四种参数就能很好的使用GidBagLayout:
(1)GridBagConstraints.gridwidth GridBagConstraints.gridheight
指定组件的显示区域行(针对 gridwidth)或列(针对 gridheight)中的单元数。默认值为 1。如下向窗口中添加一个占两个单元格(两行一列)的按钮的例子:
JFrame f=new JFrame();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
f.setLayout(gridbag);
c.gridheight=2;
c.gridwidth=1;
JButton jButton = new JButton("按钮1");
gridbag.setConstraints(button, c);
f.add(jButton);
(2)GridBagConstraints.fill
当组件的显示区域大于组件的所需大小时,用于确定是否(以及如何)调整组件。
可能的值为 GridBagConstraints.NONE(默认值)、
GridBagConstraints.HORIZONTAL(加宽组件直到它足以在水平方向上填满其显示区域,但不更改其高度)、

GridBagConstraints.VERTICAL(加高组件直到它足

以在垂直方向上填满其显示区域,但不更改其宽度)和

GridBagConstraints.BOTH(使组件完全填满其显示区域)。
使用情景举例:在一个很大的窗口(如300*300)中添加一个按钮(原始大小40*30)。

(3)GridBagConstraints.anchor
当组件小于其显示区域时,用于确定将组件置于何处(在显示区域中)。可能的值有两种:相对和绝对。相对值的解释是相对于容器的ComponentOrientation 属性,而绝对值则不然。个人觉得只使用绝对值就可以。有效值有:
绝对值
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER (the default)

(4)GridBagConstraints.weightx、GridBagConstraints.weighty (************最重要的属性)
用于确定分布空间的方式,这对于指定调整行为至关重要。例如:在一个很大的窗口(如300*300)中添加两个按钮(也可以是面板)(原始大小40*30),默认的,你会发现两个按钮分别处于上下两个等大小的区域中,且只占用了一小部分,没有被按钮占用的区域就被称为额外区域。该额外区域会随着参数weightx、weighty而被分配。





















































相关主题
文本预览
相关文档 最新文档