Java-实例-小窗体界面(菜单栏 工具栏 状态栏)本人用于多逻辑程序测试用

发布时间:2026/7/15 1:45:06

Java-实例-小窗体界面(菜单栏 工具栏 状态栏)本人用于多逻辑程序测试用 界面再主程序中调用MainUI mainUI new MainUI(); mainUI.mainWindow();import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MainUI { private static Toolkit kk Toolkit.getDefaultToolkit(); public void mainWindow() { //---------窗体显示在屏幕的中间------------ Dimension screensizekk.getScreenSize(); int height (int)screensize.getHeight(); int width (int)screensize.getWidth(); JFrame f new JFrame(小窗体); f.setSize(500, 400); f.setLocation((width-300)/2,(height-300)/2); //------------------------------------- f.setJMenuBar(myMenuBarTop()); //顶端-窗体添加菜单栏 f.setLayout(new BorderLayout()); f.add(myToolBarTop(),BorderLayout.NORTH); //上-工具栏 f.add(myDesktopPane(),BorderLayout.CENTER); //中-主窗体 f.add(myStutasBar(),BorderLayout.SOUTH); //下-端提示框 //------------------------------------- //菜单不受JFrame布局影响 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //直接关闭应用程序 f.setResizable(false); //最大化按钮无效 f.setIconImage(new ImageIcon(./img/11.png).getImage()); f.setVisible(true); } private JMenuBar myMenuBarTop(){ JMenuBar tBarnew JMenuBar(); tBar.setBorder(new EmptyBorder(5, 5, 5, 5)); tBar.setBackground(Color.PINK); JMenu tJMenu1new JMenu( 文 件 ); JMenu tJMenu2new JMenu( 工 程 ); JMenu tJMenu3new JMenu( 关 于 ); tBar.add(tJMenu1); //菜单栏中添加菜单 tBar.add(tJMenu2); tBar.add(tJMenu3); //---------------菜单下添加功能项目----------------- JMenuItem tItemnew JMenuItem(退出); tJMenu1.add(tItem); //菜单文 件下添加退出键及事件 tItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {System.exit(0);} }); return tBar; } private JToolBar myToolBarTop(){ JToolBar toolBar new JToolBar(工具栏); toolBar.setFloatable(false); // 禁止浮动 toolBar.setBorder(new EmptyBorder(5, 5, 5, 5)); toolBar.setBackground(Color.LIGHT_GRAY); toolBar.add(new JLabel(工具栏 )); toolBar.addSeparator(new Dimension(20, 20)); toolBar.add(new JLabel(可以再是这里添加)); return toolBar; } // private JPanel myDesktopPane(){ JPanel desktopPanenew JPanel(); desktopPane.setBackground(Color.WHITE); desktopPane.setLayout(new FlowLayout()); JButton bt_test1new JButton(测试键1); JButton bt_test2new JButton(测试键2); bt_test1.setActionCommand(bt_test1.getText()); bt_test2.setActionCommand(bt_test2.getText()); bt_test1.addActionListener(myActionListener); bt_test2.addActionListener(myActionListener); desktopPane.add(bt_test1); desktopPane.add(bt_test2); return desktopPane; } ActionListener myActionListener new ActionListener() { public void actionPerformed(ActionEvent e) { String command e.getActionCommand(); switch (command){ case 测试键1:{System.out.println(command);break;} case 测试键2:{System.out.println(command);break;} } } }; private JPanel myStutasBar(){ JPanel stutasBarnew JPanel(); stutasBar.setBackground(Color.LIGHT_GRAY); stutasBar.setBorder(new EmptyBorder(7, 7, 7, 7)); // 四边各20像素内边距 stutasBar.setLayout(new BoxLayout(stutasBar,BoxLayout.Y_AXIS)); stutasBar.add(new JLabel(状态栏)); return stutasBar; } }

相关新闻