Java GUI 图形界面

图形界面元素结构

图形界面
    Component            组件,一个能以图形化方式显示出来,并可以与用户交互的对象
        Container        容器,一种特殊的Component,可以盛装普通的Component
        LayoutManager    布局,容器管理其他组件布局的方式
    MenuComponent        菜单,图形界面的菜单组件

容器

Container
    Window       可以独立存在的顶级窗口
        Frame    常见窗口
        Dialog
    Panel        不能独立存在的容器
    ScrollPane

Frame 对象

  • 有标题
  • 允许通过拖拉来改变窗口的位置、大小
  • 初始化时不可见,可用 setVisible(true) 使其显示出来
  • 默认使用 BorderLayout 作为其布局管理器

Panel 对象

  • 不能独立存在、必须放在其他容器中的容器。
  • 外在表现为一个矩形区域。
  • 该区域可以盛装其他组件,为放置其他组件提供空间。
  • 默认使用 FlowLayout 作为其布局管理器。

ScrollPane 对象

  • 当组件占用空间过大时,自动产生滚动条。也可以设置参数默认具有滚动条。
  • 不能独立存在、必须放在其他容器中的容器。
  • 默认使用 BorderLayout 作为其布局管理器。通常不允许改变其布局管理器。

布局管理器

LayerManager 可以根据运行平台来调整组件的大小。

所有 AWT 容器都有默认的布局管理器,为容器指定布局管理器通过调用容器对象的 setLayout() 方法来完成。

AWT 提供了 FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout 5个常用的布局管理器。

FlowLayout

放置规律:从左到右、从上到下进行放置。

public FlowLayout() {
    this(CENTER, 5, 5);
}

public FlowLayout(int align) {
    this(align, 5, 5);
}

public FlowLayout(int align, int hgap, int vgap) {
    this.hgap = hgap;
    this.vgap = vgap;
    setAlignment(align);
}

BorderLayout

BorderLayout 把界面分成 5 个区域:North、South、East、West 和 Center,每个区域只能放置一个组件。

public BorderLayout() {
    this(0, 0);
}

public BorderLayout(int hgap, int vgap) {
    this.hgap = hgap;
    this.vgap = vgap;
}

常用方法

Component 类常用方法

// 设置组件的位置
public void setLocation(int x, int y) {
    move(x, y);
}

public void setLocation(Point p) {
    setLocation(p.x, p.y);
}
// 设置组件的大小
public void setSize(int width, int height) {
    resize(width, height);
}

public void setSize(Dimension d) {
    resize(d);
}

// 设置组件的位置、大小
public void setBounds(int x, int y, int width, int height) {
    reshape(x, y, width, height);
}

public void setBounds(Rectangle r) {
    setBounds(r.x, r.y, r.width, r.height);
}
// 设置组件的可见性
public void setVisible(boolean b)

Container 容器类常用方法

// 向容器中添加其他组件,并返回被添加的组件
// 该组件既可以是普通组件,也可以是容器
public Component add(Component comp){
    addImpl(comp, null, -1);
    return comp;
}

public void add(Component comp, Object constraints) {
    addImpl(comp, constraints, -1);
}

// 设置此容器的布局管理器
public void setLayout(LayoutManager mgr) {
    layoutMgr = mgr;
    invalidateIfValid();
}

Window 容器的方法

// Window 容器提供的方法
// 将窗口调整到最佳大小
public void pack() {}

// 设置窗口相对于指定组件的位置,如果组件未显示或者为 null,则此窗口将置于屏幕的中央
public void setLocationRelativeTo(Component c) {}

Frame 窗口的方法

// 禁用或启用此 Frame 的装饰
public void setUndecorated(boolean undecorated) {}

AWTUtilities

// 创建自定义窗口形状
public static void setWindowShape(Window var0, Shape var1) {}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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 ); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ironprosper

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值