java 按钮居中_JAVA按钮 怎么居中。

展开全部

窗体的大小62616964757a686964616fe58685e5aeb931333337373632减去组件的大小除以2即可活的居中的位置

代码示例,我写的:import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Test extends JFrame

{

private JButton button;

public Test()

{

super("按钮居中显示示例");

button=new JButton("满意答案");

button.setSize(100,50);

Container rongqi=this.getContentPane();

rongqi.setLayout(null);

rongqi.add(button);

setSize(400,200);

setResizable(false);

setLocationRelativeTo(null);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//关键代码,设置按钮位置

button.setBounds((this.getWidth()-button.getWidth()-5)/2,(this.getHeight()-28-button.getHeight())/2,

button.getWidth(),button.getHeight());

setVisible(true);

}

public static void main(String[]args)

{

new Test();

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整理了Java GUI编程基础知识! public class AllFrame { /** * 这里的代码涉及到 GUI 编程基本面板、基本组件、事件(ActionListener、ChangeListener、MouseListener) */ public AllFrame() { // new 一个主窗体 也可以这样写 new MainFrame(); MainFrame mf = new MainFrame(); } public static void main(String[] args) { new AllFrame(); } // 主窗体 继承 窗体基本格式 类 Jframe 以下弹出窗口均继承 Jframe class MainFrame extends Jframe implements ActionListener { JMenuBar menubar = new JMenuBar();;// 菜单条 JMenu jmenu[] = new JMenu[5];// panelMenu,usedMenu1,usedMenu2,layoutMenu,helpMenu; // 菜单项 JButton closebtn;// 关闭窗体 // 具体的选项 JMenuItem panelitem[] = new JMenuItem[4]; JMenuItem useditem1[] = new JMenuItem[6]; JMenuItem useditem2[] = new JMenuItem[6]; JMenuItem layoutitem[] = new JMenuItem[3]; JMenuItem helpitem = new JMenuItem("关于"); // 菜单中名称 String menu[] = { "面板使用", "常用组件一", "常用组件二", "布局设置", "帮助" }; String pane[] = { "普通面板", "滚动面板", "分割面板", "选项面板" }; String uesd1[] = { "标签", "按钮", "文本框", "文本域", "单选按钮" ,"微调"}; String uesd2[] = { "滑块","复选框", "下拉菜单", "列表", "表格", "树状" }; String layout[] = { "流式布局", "边界布局", "网格布局" }; // Jpanelitem,Jscrollpaneitem,Splitpaneitem,JTabbedpanelitem, // Jlabelitem,Jbuttonitem,JTextfielditem,JTextareaitem,JRadiobuttonitem, // JCheckboxitem,JCombomoxitem,Jlistitem,Jtableitem,Jtreeitem, // Flowlayoutitem,Borderlayoutitem,Girdlayoutitem, // Helpitem}; public MainFrame() { this.setTitle("JAVA图型界面学习(GUI)"); // 加载菜单条到窗体中 this.setJMenuBar(menubar); // 重新设置大小 this.setSize(380, 290); // 简单的居中方法 在窗体基本格式类 Jframe 中有详细的居中方法 this.setLocationRelativeTo(null); // 设置关闭窗口 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 调用方法 menu(); // 设置容器 Container con = this.getContentPane(); // 定义一个 标签 “JAVA图型界面学习” JLabel jl = new JLabel("JAVA图型界面学习"); // 组件 设置字体大小, 其他组件用法相同 jl.setFont(new Font("隶书", Font.BOLD, 24)); // 具体定位 组件名.setBounds(x, y, width, height) jl.setBounds(30, 30, 280, 50); // 把标签加到 con 容器中 con.add(jl); /
package cn.com.edu.view.frame; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Dimension; import java.awt.GridBagLayout; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JToolBar; import org.jvnet.substance.SubstanceLookAndFeel; import org.jvnet.substance.skin.FindingNemoSkin; import cn.com.edu.action.JMenuItemAction; import cn.com.edu.action.MainFrameAction; import cn.com.edu.util.GBC; import cn.com.edu.view.panel.AddStudentInfoPanel; import cn.com.edu.view.panel.FindStudentInfoPanel; /** * 教务管理系统主界面 * * @author Administrator * */ public class MainFrame extends JFrame { private static MainFrame instance; private JMenuBar bar;// 菜单条 private JMenu studentJMenu;// 菜单 private JMenu teacherJMenu;// 菜单 private JPanel center = new JPanel();// 中心面板用来放置卡片 private CardLayout card = new CardLayout();// 卡片布局 private JPanel west;// 西边面板 private JSplitPane split;// 分割面板 private JToolBar tool;// 工具条 private MainFrameAction action = new MainFrameAction(this);// 按钮事件对象 private JMenuItemAction menuItemAction = new JMenuItemAction(this);// 菜单事件对象 private SystemTray tray;// 系统托盘 private TrayIcon trayIcon;// 设置系统托盘的图片 /** * 使用单子设计模式主界面对象 * */ private MainFrame() { init(); } public static MainFrame getInstance() { if (instance == null) { instance = new MainFrame(); } return instance; } /** * 初始化主界面 * */ public void init() { // 设置标题 this.setTitle("教务管理系统"); // 设置标题图片 ImageIcon icon = new ImageIcon("img/switchuser.png"); this.setIconImage(icon.getImage()); // 得到屏幕对象 Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); // 设置主界面大小 this.setSize(size.width, size.height - 20); // 设置居中 this.setLocationRelativeTo(null); // 添加工具条 this.add(createTool(), BorderLayout.NORTH); // 将菜单添加到主界面 this.setJMenuBar(createJMenuBar()); // 将卡片添加到主界面 center.setLayout(card); addCardPanel(center); this.add(createSplit()); // 设置关闭主界面 this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE); //创建系统托盘 createSystemTray(); //关闭窗口事件 closeWindow(this); // 设置显示主界面 this.setVisible(true); } public JMenuBar createJMenuBar() { if (bar == null) { bar = new JMenuBar(); studentJMenu = createJMenu("学生管理"); teacherJMenu = createJMenu("老师管理"); addJMenuItem(studentJMenu, "添加学生信息"); addJMenuItem(studentJMenu, "查询学生信息"); addJMenuItem(studentJMenu, "修改学生信息"); addJMenuItem(studentJMenu, "删除学生信息"); studentJMenu.addSeparator(); addJMenuItem(studentJMenu, "退出"); bar.add(studentJMenu); bar.add(teacherJMenu); } return bar; } /** * 创建菜单 * * @param name * @return */ private JMenu createJMenu(String name) { JMenu menu = new JMenu(name); return menu; } /** * 将创建的菜单项添加到菜单 * * @param menu * @param name */ private void addJMenuItem(JMenu menu, String name) { JMenuItem item = new JMenuItem(name); item.addActionListener(menuItemAction); menu.add(item); } /** * 用于添加卡片 * * @param center */ public void addCardPanel(JPanel center) { JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); jp2.add(new JButton("卡片2")); jp3.add(new JButton("卡片3")); jp4.add(new JButton("卡片4")); center.add(new AddStudentInfoPanel(), "添加学生信息"); center.add(new FindStudentInfoPanel(), "查询学生信息"); center.add(jp3, "修改学生信息"); center.add(jp4, "删除学生信息"); } /** * 创建西边面板,用添加选项按钮 * * @return */ public JPanel createWestPanel() { if (west == null) { west = new JPanel(); west.setLayout(new GridBagLayout()); west.add(createButton("添加学生信息", "img/switchuser.png"), new GBC(0, 0).setInset(10)); west.add(createButton("查询学生信息", "img/switchuser.png"), new GBC(0, 1).setInset(10)); west.add(createButton("修改学生信息", "img/switchuser.png"), new GBC(0, 2).setInset(10)); west.add(createButton("删除学生信息", "img/switchuser.png"), new GBC(0, 3).setInset(10)); } return west; } /** * 创建按钮方法 * * @param name * @return */ public JButton createButton(String name, String icon) { JButton button = new JButton(name); button.setIcon(new ImageIcon(icon)); button.addActionListener(action); return button; } public CardLayout getCard() { return card; } public JPanel getCenter() { return center; } /** * 分割面板 * * @return */ public JSplitPane createSplit() { if (split == null) { split = new JSplitPane(); split.setOneTouchExpandable(true); split.setLeftComponent(createWestPanel()); split.setRightComponent(center); } return split; } /** * 创建工具条 * * @return */ public JToolBar createTool() { if (tool == null) { tool = new JToolBar(); tool.add("添加学生信息", createButton("添加学生信息", "img/switchuser.png")); tool.add("查询学生信息", createButton("查询学生信息", "img/switchuser.png")); tool.add("修改学生信息", createButton("修改学生信息", "img/switchuser.png")); tool.add("删除学生信息", createButton("删除学生信息", "img/switchuser.png")); tool.add("帮助", createButton("帮助", "img/syssetup.png")); } return tool; } ///////////////////////////系统托盘设置///////////////////////////////////// /** * 窗口事件 * * @param jframe */ public void closeWindow(MainFrame jframe) { jframe.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int show = JOptionPane.showConfirmDialog(null, "是否确定关闭?", "确认关闭系统", JOptionPane.YES_NO_OPTION); if (show == JOptionPane.YES_OPTION) { System.exit(0); } } public void windowIconified(WindowEvent e) { if (getState() == 1) {// 最小化 try { tray.add(trayIcon); } catch (AWTException e1) { e1.printStackTrace(); } setVisible(false); } } }); } /** * 创建系统托盘 * */ public void createSystemTray() { // 得到当前系统的托盘对象 tray = SystemTray.getSystemTray(); ImageIcon icon = new ImageIcon("img/2.png"); // 添加鼠标右键 弹出菜单 PopupMenu menu = new PopupMenu(); MenuItem show = new MenuItem("显示窗体"); MenuItem exit = new MenuItem("退出窗体"); trayIcon = new TrayIcon(icon.getImage(), "学生管理系统", menu); trayIcon.addMouseListener(new MouseAdapter() { /** * 鼠标点击事件 */ public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) {// 鼠标双击 tray.remove(trayIcon); setVisible(true); // 设置窗口全屏 setExtendedState(JFrame.MAXIMIZED_BOTH); } } }); /** *鼠标右键显示窗体 */ show.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); setVisible(true); // 设置窗口全屏 setExtendedState(JFrame.MAXIMIZED_BOTH); } }); /** * 鼠标右键关闭窗体 */ exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int show = JOptionPane.showConfirmDialog(null, "是否确定关闭?", "确认关闭系统", JOptionPane.YES_NO_OPTION); if (show == JOptionPane.YES_OPTION) { System.exit(0); } } }); menu.add(show); menu.add(exit); } /** * @param args */ public static void main(String[] args) { SubstanceLookAndFeel.setSkin(new FindingNemoSkin()); // 蓝色幽灵 // SubstanceLookAndFeel.setSkin(new OfficeBlue2007Skin()); // 麦田风光 // SubstanceLookAndFeel.setSkin(new FieldOfWheatSkin()); // 默认皮肤 // SubstanceLookAndFeel.setSkin(new BusinessSkin()); // 朦胧风格 // SubstanceLookAndFeel.setSkin(new MistAquaSkin()); MainFrame.getInstance(); } }
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值