java GUL编程
GUL(圖形用戶界面)
- awt
- sping
?Frame
Frame:GUL編程的最底層的窗口接口,為標簽,文本等提供平臺。
? ? ? ?Frame窗口常見方法:? setSize()設置窗口大小
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?setBackground()設置窗口背景顏色
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResizable(boolean)設置窗口固定大小
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?setLayout()設置布局
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??setLocation()設置窗口初始位置
注:對于Frame窗口,沒有關閉功能的實現,即使點擊關閉按鈕也關不掉,如果想實現Fame關閉功能可以使用事件監聽來關閉窗口。
frame.addWindowListener(new WindowAdapter() { //窗口關閉事件@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);} });?如下是建立一個java窗口:
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;public class GULCode {public static void main(String[] args){new MyFrame();} } class MyFrame {public MyFrame (){Frame frame=new Frame("frame窗口"); //創建窗口及窗口名frame.setSize(400,300); //設置窗口大小frame.setVisible(true); //窗口是否可見frame.setBackground(Color.CYAN); //設置背景顏色frame.setLocation(400,400); //窗口固定位置frame.addWindowListener(new WindowAdapter() { //窗口關閉事件@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);} });} }面板(Panel)
面板(Panel):面板面板,顧名思義就是放在窗口上的,面板窗口是獨立的空間,不能脫離窗口獨立存在。
如下窗口中黑色部分就是面板。
布局:
FlowLayout:流式布局,窗口里的內容位置會隨著窗口的大小發生改變
BorderLayout:東西南北中布局,劃分好窗口布局位置
GridLayout:表格布局
流式布局
?當改變窗口大小時,窗口內容的位置會發生改變,就像流水一樣,如下,窗口大小改變,按鈕位置發生改變。
?流式布局常用參數:
frame.setLayout(new FlowLayout(FlowLayout.CENTER)); //居中frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //左對齊frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//右對齊BorderLayout布局
?代碼及效果如上,不解釋
表格布局
?GridLayout(rows,cols)定義表格的行和列
輸入框
TextFileld()
畫筆
Graphics
?注意:畫筆的paint方法是重寫的方法
監聽事件
ActionListrner
public class LayoutCode {public static void main(String[] args) {Frame frame=new Frame("事件監聽");frame.setVisible(true);frame.setBounds(1000,200,400,300);Button button=new Button("點擊關閉窗口"); //Button會有亂碼,JButton沒有 MyActionListener myActionListener=new MyActionListener();button.addActionListener(myActionListener); //事件監聽需要一個myActionListener參數frame.add(button);}private static class MyActionListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0); //點擊關閉窗口}} }ActionListrner是一個抽象接口,如上button的監聽事件需要一個參數,所有要創建一個類來通過這個參數。
鼠標監聽
MouseListener
public class LayoutCode {public static void main(String[] args) {Frame frame = new Frame("事件監聽");frame.setVisible(true);frame.setBounds(1000, 200, 400, 300);mouseListener mouseListener=new mouseListener();frame.addMouseListener(mouseListener);} }class mouseListener extends MouseAdapter{ //適配器 MouseAdapter繼承了MouseListener,避免了要重寫所有方法@Overridepublic void mouseClicked(MouseEvent e) {System.out.println("鼠標被點擊");System.exit(0);} }mouseClicked是鼠標點擊方法,在MouseAdaper中還有其他方法,按ctrl+b可查看其源碼
窗口事件
WindowListener
如下是窗口關閉事件,窗口適配器windowAdapter提供了其他方法,在此不一一列舉
public class LayoutCode {public static void main(String[] args) {Frame frame = new Frame("事件監聽");frame.setVisible(true);frame.setBounds(1000, 200, 400, 300);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});} }鍵盤監聽事件
KeyListener
public class LayoutCode {public static void main(String[] args) {Frame frame = new Frame("事件監聽");frame.setVisible(true);frame.setBounds(1000, 200, 400, 300);frame.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {int keycode= e.getKeyCode(); //獲得鍵盤值System.out.println(keycode);if(keycode==10){System.exit(0); //enter鍵是10,按下關閉窗口}}});} }JFrame窗口
JFrame是spring編程的窗口函數,用法和awt中的Fram差不多,但JFrame窗口有關閉窗口的方法
創建窗口:
public class LayoutCode {public static void main(String[] args) {JFrame jframe = new JFrame("JFrame窗口");jframe.setVisible(true);jframe.setBounds(400,300,400,300);jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //關閉窗口} }彈窗?
JDialog
ublic class LayoutCode {public static void main(String[] args) {JFrame jframe = new JFrame("JFrame窗口");jframe.setVisible(true);jframe.setBounds(400,300,400,300);JButton jButton=new JButton("彈窗");jframe.add(jButton);jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //關閉窗口jButton.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {new mydialog();}});} } class mydialog extends JDialog{public mydialog(){setVisible(true);setBounds(100,200,400,300);setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);Container container=new Container();container.add(new JLabel("彈窗"));} }點擊button按鈕會跳出一個彈窗。
Icon標簽
public class LayoutCode {public static class IconDemo extends JFrame implements Icon{ //重寫Icon方法 private int width; private int height;public IconDemo(){}public IconDemo(int width,int height){this.height=height;this.width=width;}public void init(){IconDemo iconDemo= new IconDemo(15, 15);JLabel JLabel=new JLabel("圖片標簽",iconDemo,SwingConstants.CENTER); //居中Container container=getContentPane();container.add(JLabel);this.setBounds(400,300,400,300);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) { //Icon繪圖方法g.fillOval(x,y,width,height);}@Overridepublic int getIconWidth() { //獲得圖寬return width;}@Overridepublic int getIconHeight() { //獲得圖高return height;}}public static void main(String[] args) {new IconDemo().init(); } }效果圖:
ImageIcon標簽
文本域和JScrollPane面板
文本域:JTextArea
JScrollPane面板在窗口小于指定大小時會有滾動框出行。
?
? ?圖片按鈕
?單選框
JRadioButton
?
多選框
JChecBox
?
下拉框
JComboBox
?
列表框
?
文本框
???JTextField
?
密碼框
JPasswordField
?
文本域
JTextArea
?
總結
以上是生活随笔為你收集整理的java GUL编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 艺赛旗(RPA) 新手课堂 - 键盘与输
- 下一篇: python爬取起点vip小说章节_py