关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

【Java_GUI编程】--基本操作你要知道(一)

发布时间:2023-06-30 18:00:47

一、组件和容器

1、JFrame

package www.qyl.lesson01; import javax.swing.*; import java.awt.*; public class TestFrame {  public static void main(String[]args){  JFrame frame = new JFrame("第一个GUI窗口");  frame.setSize(400,400); //设置窗口大小  frame.setLocation(200,200); //弹出的初始位置  frame.setBackground(new Color(49, 77, 125)); //设置背景颜色  frame.setResizable(false); //设置大小固定(不可调整大小)  frame.setVisible(true); //设置窗口可见性  } }

   

2、面板JPanel

注意: //JFrame设置背景色的区域一般是看不到的。一般看到的window背景区域是JFrame里的ContentPane,所以在ContentPane设置。

java frame.setBackground(new Color(52, 111, 121)); 改为 frame.getContentPane().setBackground(new Color(52, 111, 121));

package www.qyl.lesson01; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestPanel {  public static void main(String[] args) {  JFrame frame = new JFrame(); //创建窗体  JPanel panel = new JPanel(); //创建面板  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置窗体可关闭  frame.setLayout(null); //定义布局为 绝对布局  frame.setBounds(200,200,300,300); //设置窗体x,y的坐标(弹出位置)和宽度、高度   // frame.setBackground(new Color(52, 111, 121)); 这样设置没有效果  frame.getContentPane().setBackground(new Color(52, 111, 121)); //设置窗体背景颜色   frame.setResizable(true);  frame.setVisible(true);  panel.setBackground(new Color(56, 127, 96)); //设置面板背景颜色  panel.setBounds(50,50,180,150);  frame.add(panel); //将面板放入到窗体中  } }

   

3、布局管理器

流式布局(从左到右)

package www.qyl.lesson01; import javax.swing.*; import java.awt.*; public class TestFlowLayout {  public static void main(String[] args) {  JFrame frame = new JFrame();   //组件-按钮  Button button1 = new Button("button1");  Button button2 = new Button("button2");  Button button3 = new Button("button3");  //设置为流式布局  frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); //默认为从左到右,这里设置靠右边right  frame.setSize(300,300);  frame.add(button1);  frame.add(button2);  frame.add(button3);  frame.setVisible(true);  } }

   

东南西北中

package www.qyl.lesson01; import javax.swing.*; import java.awt.*; public class TestBorderLayout {  public static void main(String[] args) {  JFrame frame = new JFrame("TestBorderLayout");  Button button1 = new Button("button1");  Button button2 = new Button("button2");  Button button3 = new Button("button3");  Button button4 = new Button("button4");  Button button5 = new Button("button5");   //设置东南西北中布局  frame.add(button1,BorderLayout.EAST);  frame.add(button2,BorderLayout.WEST);  frame.add(button3,BorderLayout.SOUTH);  frame.add(button4,BorderLayout.NORTH);  frame.add(button5,BorderLayout.CENTER);  frame.setSize(300,300);  frame.setVisible(true);  } }

   

网格布局

package www.qyl.lesson01; import javax.swing.*; import java.awt.*; public class TestGridLayout {  public static void main(String[] args) {  JFrame frame = new JFrame("TestBorderLayout");  Button button1 = new Button("button1");  Button button2 = new Button("button2");  Button button3 = new Button("button3");  Button button4 = new Button("button4");  Button button5 = new Button("button5");  Button button6 = new Button("button6");  //设置网格布局,3行2列  frame.setLayout(new GridLayout(3,2));  frame.add(button1);  frame.add(button2);  frame.add(button3);  frame.add(button4);  frame.add(button5);  frame.add(button6);  frame.setSize(300,300);  frame.setVisible(true);  } }

   

4、事件监听

package www.qyl.lesson01; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestActionEvent {  public static void main(String[] args) {  JFrame jFrame = new JFrame();  JButton button = new JButton("button1");  jFrame.setBounds(10,10,300,300);  jFrame.setVisible(true);  jFrame.add(button);  button.addActionListener(new MyActionEvent());  } } class MyActionEvent implements ActionListener{  @Override  public void actionPerformed(ActionEvent e) {  System.out.println("你点击了按钮!!");  } }

   

点击按钮后,就会触发System.out.println("你点击了按钮!!");这条命令


/template/Home/leiyu/PC/Static