Java中Swing類實例講解
JFrame jFrame=new JFrame('標題名字');jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置用戶在此窗體上發起 'close' 時默認執行的操作。//有兩種選擇,默認是 HIDE_ON_CLOSE即點擊關閉時隱藏界面。jFrame.setBounds(0,0,1200,1200);//設置框架的大小jFrame.setVisible(true);//設置框架為可見將背景圖片加載進入程序中
ImageIcon image=new ImageIcon('文件地址');JLabel label=new JLabel(image);label.setBounds(0,0,image.getIconWidth(),image.getIconHeight());//定義標簽的大小為圖片大小jFrame.getLayeredPane().add(label);//將標簽添加進入JFrame框架中添加按鈕到框架中
JButton button=new JButton('hello'); button.setFont(new Font('宋體',Font.PLAIN,70)); //設置字體的(種類,格式,大小) button.setForeground(Color.red); //設置字體的顏色 button.setBackground(Color.GREEN); //設置按鈕框的顏色 button.setBounds(400,0,400,400); //設置按鈕的大小 button.addActionListener(new ActionListener() { int n=1; @Override public void actionPerformed(ActionEvent e) { System.out.println('click'+n+'次'); n++; } }); //添加按鈕的事件監聽jFrame.getLayeredPane().add(button);//將按鈕添加到框架中JPanel類
JPanel panel = new JPanel();panel.setLayout(null);//定義布局frame.add(panel);//添加到面板。文本框運用
//文本框JTextField userText = new JTextField(要求的字數限制);userText.setBounds(100,20,165,25);panel.add(userText);//密碼文本框(里面內容為*)JPasswordField passwordText = new JPasswordField(20);passwordText.setBounds(100,50,165,25);panel.add(passwordText);
到此這篇關于Java中Swing類實例講解的文章就介紹到這了,更多相關Java中Swing類內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: