JFrame 布局 FlowLayout BorderLayout GridLayout 菜单练习 事件处理练习
Java JFrame 布局 FlowLayout BorderLayout GridLayout 菜单练习 事件处理练习
1.FlowLayout布局练习
import javax.swing.*;
import java.awt.*;
class MyJFrame01 extends JFrame {
MyJFrame01(String s, int x, int y, int w, int h) {
setLayout(new FlowLayout());
setTitle(s);
setBounds(x, y, w, h);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Flow01{
public static void main(String args[]) {
MyJFrame01 myJFrame = new MyJFrame01("FlowLayout Demo", 100, 100, 300, 80);
String[] names = {"北","东","西","南","中"};
for (String i:names) {
myJFrame.add(new JButton(i));
}
myJFrame.setVisible(true);
}
}
2.BorderLayout布局练习
import javax.swing.*;
import java.awt.*;
class MyJFrame02 extends JFrame {
MyJFrame02(String s, int x, int y, int w, int h) {
setLayout(new BorderLayout());
setTitle(s);
setBounds(x, y, w, h);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Border02{
public static void main(String args[]) {
MyJFrame02 myJFrame = new MyJFrame02("BorderLayout Demo", 100, 100, 160, 120);
myJFrame.add(new JButton("北"), BorderLayout.NORTH);
myJFrame.add(new JButton("南"), BorderLayout.SOUTH);
myJFrame.add(new JButton("东"), BorderLayout.EAST);
myJFrame.add(new JButton("西"), BorderLayout.WEST);
myJFrame.add(new JButton("中"), BorderLayout.CENTER);
myJFrame.setVisible(true);
}
}
3.GridLayout布局练习
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MyJFrame03 extends JFrame {
MyJFrame03(String s, int x, int y, int w, int h) {
setLayout(new GridLayout(5,1));
setTitle(s);
setBounds(x, y, w, h);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Grid03 {
public static void main(String[] args) {
MyJFrame03 myJFrame = new MyJFrame03("JPanel Demo", 100, 100, 400, 400);
JPanel panel1 = new JPanel(new FlowLayout());
panel1.setBackground(Color.red);
JPanel panel2 = new JPanel(new FlowLayout());
panel2.setBackground(Color.orange);
JPanel panel3 = new JPanel(new FlowLayout());
panel3.setBackground(Color.yellow);
JPanel panel4 = new JPanel(new FlowLayout());
panel4.setBackground(Color.green);
JPanel panel5 = new JPanel(new FlowLayout());
panel5.setBackground(Color.blue);
myJFrame.add(panel1);
myJFrame.add(panel2);
myJFrame.add(panel3);
myJFrame.add(panel4);
myJFrame.add(panel5);
myJFrame.setVisible(true);
}
}
4.菜单练习
package Exp04;
import javax.swing.*;
import java.awt.*;
class MyJFrame04 extends JFrame {
MyJFrame04(String s, int x, int y, int w, int h) {
setLayout(new FlowLayout());
setTitle(s);
setBounds(x, y, w, h);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Menu04 {
public static void main(String args[]) {
MyJFrame04 myJFrame = new MyJFrame04("带菜单的...", 100, 100, 230, 200);
JMenuBar menubar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Edit");
JMenuItem menuItem1 = new JMenuItem("open");
JMenuItem menuItem2 = new JMenuItem("close");
JMenuItem menuItem3 = new JMenuItem("exit");
JMenuItem menuItem21 = new JMenuItem("copy");
JMenu menuItem22 = new JMenu("find and replace");
JMenuItem menuItem221 = new JMenuItem("find");
JMenuItem menuItem222 = new JMenuItem("replace");
// 将菜单条加到窗口里
myJFrame.setJMenuBar(menubar);
// 将菜单加到菜单条里
menubar.add(menu1);
menubar.add(menu2);
// 将菜单项加到菜单里
menu1.add(menuItem1);
menu1.add(menuItem2);
menu1.addSeparator();// 添加分隔线
menu1.add(menuItem3);
menu2.add(menuItem21);
menu2.addSeparator();// 添加分隔线
menu2.add(menuItem22);
menuItem22.add(menuItem221);
menuItem22.add(menuItem222);
myJFrame.setVisible(true);
}
}
5.事件处理练习
点击某个按钮,在文本框中显示对应的文字。
package Exp04;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MyJFrame extends JFrame {
MyJFrame(String s, int x, int y, int w, int h) {
setLayout(new BorderLayout());
setTitle(s);
setBounds(x, y, w, h);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Something05 {
public static void main(String args[]) {
MyJFrame myJFrame = new MyJFrame("我...", 100, 100, 160, 150);
// 创建上半部分panel1玻璃窗,存放文本域
Panel panel1 = new Panel(new FlowLayout());
JTextArea textArea = new JTextArea(1, 10);
panel1.add(new JScrollPane(textArea));
// 创建上半部分panel1玻璃窗,存放五个按钮
Panel panel2 = new Panel(new BorderLayout());
//分别独立创建 东西南北中 五个按钮,以便后续监听使用
String[] name = {"北","南","东","西","中"};
JButton[] button = new JButton[5];
JButton button_north = new JButton("北");
panel2.add(button_north, BorderLayout.NORTH);
JButton button_south = new JButton("南");
panel2.add(button_south, BorderLayout.SOUTH);
JButton button_east = new JButton("东");
panel2.add(button_east, BorderLayout.EAST);
JButton button_west = new JButton("西");
panel2.add(button_west, BorderLayout.WEST);
JButton button_center = new JButton("中");
panel2.add(button_center, BorderLayout.CENTER);
JButtonHandler listener = new JButtonHandler();
//创建监听事件,listener实例,以及连接文本域
listener.setJTextArea(textArea);
//监听各按钮
button_north.addActionListener(listener);
button_south.addActionListener(listener);
button_east.addActionListener(listener);
button_west.addActionListener(listener);
button_center.addActionListener(listener);
//将两个玻璃窗放入容器窗格,构造完成
myJFrame.add(panel1, BorderLayout.NORTH);
myJFrame.add(panel2, BorderLayout.CENTER);
myJFrame.setVisible(true);
}
}
class JButtonHandler implements ActionListener {
JTextArea textshow;
public void setJTextArea(JTextArea area) {
textshow = area;
}
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()得到button按钮上的字符串
textshow.setText("");
switch (e.getActionCommand()) {
case "北":
textshow.append("北");
break;
case "南":
textshow.append("南");
break;
case "东":
textshow.append("东");
break;
case "西":
textshow.append("西");
break;
case "中":
textshow.append("中");
break;
default:
textshow.append("请重新点击");
}
}
}