------- android培训、java培训、期待与您交流! ----------
常见的组件和菜单(jmenubar)
package cn.itcast.a_gui;
import java.awt.BorderLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import cn.itcast.a_util.FrameUtil;
public class Demo6 {
/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
//面板
JPanel panel = new JPanel();
// panel.setBackground(Color.red);
frame.add(panel);
//标签
JLabel label = new JLabel("用户名:");
panel.add(label);
//文本域
JTextField textField = new JTextField(12);
panel.add(textField);
//标签
JLabel label2 = new JLabel("密码:");
panel.add(label2);
//密码域
JPasswordField passwordField = new JPasswordField(8);
panel.add(passwordField);
//单选按钮
JLabel label3 = new JLabel("性别:");
panel.add(label3);
JRadioButton man = new JRadioButton("男");
JRadioButton woman = new JRadioButton("女");
//单选按钮-互斥(多选一)
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(man);
buttonGroup.add(woman);
//单选按钮不能添加进容器
panel.add(man);
panel.add(woman);
//复选框
JLabel label4 = new JLabel("爱好:");
panel.add(label4);
JCheckBox play = new JCheckBox("运动");
JCheckBox sleep = new JCheckBox("睡觉");
JCheckBox movie = new JCheckBox("看电影");
panel.add(play);
panel.add(sleep);
panel.add(movie);
//文本域
JLabel label5 = new JLabel("个人简历");
panel.add(label5);
JTextArea textArea = new JTextArea(10,10);
panel.add(textArea);
//列表
String[] data = {"one","two","three"};
JList list = new JList(data);
panel.add(list);
//按钮-注册
JButton button = new JButton("注册");
panel.add(button);
//菜单条
JMenuBar menuBar = new JMenuBar();
//菜单
JMenu menu1 = new JMenu("文件");
JMenu menu2= new JMenu("格式");
JMenu menu3 = new JMenu("帮助");
JMenu menu4 = new JMenu("页面设置");
//菜单项
JMenuItem item1 = new JMenuItem("新建");
JMenuItem item2 = new JMenuItem("保存");
JMenuItem item3 = new JMenuItem("另存为");
JMenuItem item4 = new JMenuItem("段落设置");
JMenuItem item5 = new JMenuItem("文字设置");
JMenuItem item6 = new JMenuItem("颜色设置");
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu1.add(menu4);
menu4.add(item4);
menu4.add(item5);
menu4.add(item6);
//选项里面有多个选项
frame.add(menuBar,BorderLayout.NORTH);
FrameUtil.initFrame(frame, 300, 320);
}
}
FlowLayout流式布局
在JPanel中默认是流式布局:
public class ShowFlowLayout extends JFrame {
public ShowFlowLayout() {
super.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
add(new JLabel("姓名:"));
add(new JTextField(8));
add(new JLabel("邮箱:"));
add(new JTextField(8));
add(new JLabel("电话:"));
add(new JTextField(8));
}
public static void main(String[] args) {
ShowFlowLayout frame = new ShowFlowLayout();
frame.setTitle("FlowLayout");
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
GridLayout网格布局
public class ShowGridLayout extends JFrame {
public ShowGridLayout() {
setLayout(new GridLayout(3, 2, 5, 5));
add(new JLabel("姓名:"));
add(new JTextField(8));
add(new JLabel("邮箱:"));
add(new JTextField(8));
add(new JLabel("电话:"));
add(new JTextField(8));
}
public static void main(String[] args) {
ShowGridLayout frame = new ShowGridLayout();
frame.setTitle("GridLayout");
frame.setSize(200, 125);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
BorderLayout边框布局
public class ShowBorderLayout extends JFrame {
public ShowBorderLayout() {
setLayout(new BorderLayout(5, 10));
add(new JButton("东"), BorderLayout.WEST);
add(new JButton("西"), BorderLayout.EAST);
add(new JButton("南"), BorderLayout.SOUTH);
add(new JButton("北"), BorderLayout.NORTH);
add(new JButton("中"), BorderLayout.CENTER);
}
public static void main(String[] args) {
ShowBorderLayout frame = new ShowBorderLayout();
frame.setTitle("BorderLayout");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
事件
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ShowButton extends JFrame {
public ShowButton() {
JButton button = new JButton("点我");
add(button);
// 添加鼠标监听事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击");
Object source = e.getSource();
JButton button = (JButton) source;
String text = button.getText();
if ("按钮被点击".equals(text)) {
button.setText("点我");
} else {
button.setText("按钮被点击");
}
}
});
}
public static void main(String[] args) {
ShowButton frame = new ShowButton();
frame.setTitle("我的框架");
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
适配器
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class AdapterDemo extends JFrame {
AdapterDemo() {
addWindowListener(new MyAdapter());
}
public static void main(String[] args) {
AdapterDemo frame = new AdapterDemo();
frame.setTitle("我的框架");
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class MyAdapter extends WindowAdapter {
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated....");
}
}
}