java 底部菜单栏
时间: 2023-11-20 10:56:59 浏览: 84
底部菜单栏
以下是Java实现底部菜单栏的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class BottomMenuDemo extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JPanel bottomPanel;
private JButton btnHome;
private JButton btnCategory;
private JButton btnCart;
private JButton btnProfile;
public BottomMenuDemo() {
setTitle("底部菜单栏示例");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(400, 300));
setResizable(false);
contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel lblTitle = new JLabel("底部菜单栏示例", SwingConstants.CENTER);
lblTitle.setFont(new Font("宋体", Font.BOLD, 20));
contentPane.add(lblTitle, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(new JLabel("这里是内容区域"));
contentPane.add(centerPanel, BorderLayout.CENTER);
bottomPanel = new JPanel(new GridLayout(1, 4));
bottomPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY));
btnHome = new JButton("首页");
btnHome.addActionListener(this);
bottomPanel.add(btnHome);
btnCategory = new JButton("分类");
btnCategory.addActionListener(this);
bottomPanel.add(btnCategory);
btnCart = new JButton("购物车");
btnCart.addActionListener(this);
bottomPanel.add(btnCart);
btnProfile = new JButton("个人中心");
btnProfile.addActionListener(this);
bottomPanel.add(btnProfile);
contentPane.add(bottomPanel, BorderLayout.SOUTH);
setContentPane(contentPane);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnHome) {
System.out.println("点击了首页按钮");
} else if (e.getSource() == btnCategory) {
System.out.println("点击了分类按钮");
} else if (e.getSource() == btnCart) {
System.out.println("点击了购物车按钮");
} else if (e.getSource() == btnProfile) {
System.out.println("点击了个人中心按钮");
}
}
public static void main(String[] args) {
new BottomMenuDemo();
}
}
```
阅读全文