JAVA GUI 图形界面

①事件源和监听
举例,窗口里有一个按钮Button,按下这个Button,就能在窗口某个地方显示出一个图片。
此例中:
事件源是Button
监听是一段程序,一旦Button的状态被改变比如click ,这段程序就能接收到一个事件event并对窗口进行相应的操作

所以几乎事件源都要添加一个相应的监听程序:
下面是一个很简单的JAVA GUI的代码:
JAVA GUI 图形界面 - 小镜子~ - 菜园子
 这是程序运行的结果:(一个硕大的按钮占据了整个屏幕)
JAVA GUI 图形界面 - 小镜子~ - 菜园子               JAVA GUI 图形界面 - 小镜子~ - 菜园子
//click之前                                                                  //click 之后
 
在上面的go()方法里加入下面两行的代码,能让Button里面的字体变大:
Font bigFont=new Font(Font.SANS_SERIF,Font.BOLD,34);//里面的参数依次是字体,样式,字号(前面两个随便选的)
button.setFont(bigFont);

字体变大之后的运行结果:
JAVA GUI 图形界面 - 小镜子~ - 菜园子



②多个事件源 多个监听
创建多个监听类ButtonListener ListListener...
button.add(new ButtonListener());
list.add(new ListListener());



③其他swing组件
JFrame
JFrame frame=new JFrame("frameName");//整个框架
frame.getContentPane().add(button);//在框架的主面板上添加swing组件

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
//这三个设置frame的属性,基本都要写
要设置JFrame不可改变大小:frame.setResizable(false);


JButton
JButton button = new JButton("button");
button.setText("new button");//更换 button上的文字
button.setSize(30, 30);//这个效果没试出来,待定
JAVA GUI 图形界面 - 小镜子~ - 菜园子
事件处理函数就是actionPeformed(ActionEvent ae){}(类要implements ActionListener)
 

JPanel 
JPanel panel=new JPanel();
panel.add(button);
panel.add(new JPanel);//panel可叠加
如果想在panel上绘制或添加图片http://blog.163.com/it_novice/blog/static/20918306920133128328829/


JTextField
JTextField field=new JTextField(20);//构造函数要求输入长度
field.setText("field");
field.selectAll();选取文本字段的内容
field.requestFocus();//置光标于field
JAVA GUI 图形界面 - 小镜子~ - 菜园子
 

JTextArea
JTextArea textArea=newJTextArea(10,20);//构造函数要求设定 行高 和 每行的宽度
textArea.setText("textarea");
textArea.append("lalala");//在末尾添加
textArea.setLineWrap(true) ;//启动自动换行
JAVA GUI 图形界面 - 小镜子~ - 菜园子
 

JScrollPane(带有滚动条的面板,一般和JTextArea或者JList一起用)
JScrollPane scroller=new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//设置总是有垂直滚动条
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);//设置没有水平滚动条
panel.add(scroller);
JAVA GUI 图形界面 - 小镜子~ - 菜园子
 

JCheckBox
JCheckBox check=new JCheckBox("CheckBox");
check.setSelected(true);
JAVA GUI 图形界面 - 小镜子~ - 菜园子
JCheckBox的事件处理函数:(类要implements ItemEventListener)
public void itemStateChanged(ItemEvent ev){}

 
JList
String[ ] element={"xiaojing","xinyi","songsong","aying"};
JList list=new JList(element);
JScroller scroller=new JScroller(list);
list.setVisibleRowCount(3);//设定显示的行数
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.getSelectedValue();//获取已经被选中的值
JAVA GUI 图形界面 - 小镜子~ - 菜园子
JList事件处理函数:(要写一个内部类implements ListSelectionListener)
JAVA GUI 图形界面 - 小镜子~ - 菜园子
 以下是运行结果,点哪个选项就在控制台输出哪个:
JAVA GUI 图形界面 - 小镜子~ - 菜园子


 




 
 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值