本文主要是介绍JFrame:简单应用2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import javax.swing.*;public class JFrameDome {public void init(){JFrame frame=new JFrame();frame.setVisible(true);frame.setBounds(100,100,200,300);//关闭frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {//build a framenew JFrameDome().init();}
}
import javax.swing.*;
import java.awt.*;public class JFrameDome02 {public void init() {JFrame jf = new JFrame("这是一个JFrame窗口");jf.setVisible(true);jf.setBounds(100, 100, 200, 200);jf.setBackground(Color.cyan);JLabel label=new JLabel("welcome to qz's world");jf.add(label);jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JFrameDome02().init();}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//如果想要实现icon,不能直接getContentPane()
public class DialogDemo extends JFrame{public DialogDemo(){this.setVisible(true);this.setSize(700,500);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//JFrame 放东西的容器Container container=this.getContentPane();//绝对布局container.setLayout(null);//按钮JButton button=new JButton("点击弹出弹窗");button.setBounds(30,30,200,50);//点击弹出弹窗button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new MyDialogDemo();}});container.add(button);}public static void main(String[] args) {new DialogDemo();//测试 new MyDialogDemo();}}//弹窗
class MyDialogDemo extends JDialog{public MyDialogDemo(){this.setVisible(true);this.setBounds(100,100,200,300);// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//获得容器Container container=this.getContentPane();container.setLayout(null);//设置绝对布局(清除默认布局)后添加布局需要明确添加的位置JLabel label=new JLabel("study java!");label.setBounds(20,20,100,50);container.add(label);}
}
这篇关于JFrame:简单应用2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!