JAVA 自己马马虎虎学了已经有快三个月了
以前写出的东西一般都是通过命令行来跟用户进行交互,这种应用程序通常称为控制台应用程雪,显示中多数程序都具有图形用户界面。
图形用户界面借助菜单、按钮等图形方式,是的用户可以方便有好地与程雪交互。它避免了使用字符界面时用户必须记忆的各种命令的烦恼,深受广大用户的欢迎,业已成为当下软件开发的重要方面
布局管理、事件处理、常用swing组件、绘图、字体和颜色等。
1.创建框架
import javax.swing.JFrame;
public class FrameDome {
public static void main (String []args ) {
JFrame frame =new JFrame("第一个JFame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920,1080);
frame.setVisible(true);
}
}
2.添加组件
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
public class WithButton {
public static void main(String [] args ) {
JFrame frame =new JFrame("添加一个按钮");//创建矿建
JButton button =new JButton("按钮");//创建一个按钮
Container container =frame.getContentPane();//获取框架
container.add(button); //添加上去
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
另一种方法
import javax.swing.JButton;
import javax.swing.JFrame;
public class FrameWithButton1 extends JFrame {
public FrameWithButton1() {
JButton button=new JButton("按钮");
add(button);
}
public static void main (String []args ) {
FrameWithButton1 frame =new FrameWithButton1();
frame.setTitle("标题");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920,1080);
frame.setVisible(true);
}
}
3.动作事件处理
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JButtonEvent extends JFrame implements ActionListener {
public JButtonEvent() {
JButton button =new JButton("按钮");
button.addActionListener(this);
add(button);
}
public static void main(String[] args) {
JButtonEvent frame =new JButtonEvent();
frame.setTitle("演示动作事件处理");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,100);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("接受点击反馈");
System.out.println(((JButton)e.getSource()).getText());//获取点击按钮上的文本
}
}
使用匿名内部类实现监听接口