一、窗口和面板
public class JFrameDemo { //init() 初始化 public void init(){ //顶级窗口 JFrame frame = new JFrame("这个一个JFrame窗口"); frame.setVisible(true); frame.setBounds(100,100,200,200); frame.setBackground(Color.cyan); //设置文字 label JLabel label = new JLabel("刘铁锤"); frame.add(label); //关闭事件 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { //建立一个窗口 new JFrameDemo().init(); } } public class JFrameDemo02 { public static void main(String[] args) { new MyJFrame2().init(); } } class MyJFrame2 extends JFrame{ public void init(){ //获得一个容器 setBounds(10,10,200,300); setVisible(true); //设置文字 label JLabel label = new JLabel("刘铁锤"); add(label); //让文本标签居中,设置水平对齐 label.setHorizontalAlignment(SwingConstants.CENTER); Container contentPane = this.getContentPane(); contentPane.setBackground(Color.blue); } }
二、弹窗
-
JDialog默认就有关闭弹窗事件,重复写会报错
//主窗口 public class DialogDemo extends JFrame { public static void main(String[] args) { new DialogDemo(); } public DialogDemo() { this.setVisible(true); this.setSize(700,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //JFrame放东西,容器 Container contentPane = this.getContentPane(); //绝对布局 contentPane.setLayout(null); //按钮 JButton jButton = new JButton("点击弹出一个对话框");//创建 jButton.setBounds(30,30,200,50); //点击这个按钮的时候,弹出一个弹窗 jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //弹窗 new MyDialogDemo(); } }); contentPane.add(jButton); } } //弹窗的窗口 class MyDialogDemo extends JDialog{ public MyDialogDemo() { this.setVisible(true); this.setBounds(100,100,500,500); // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container contentPane = this.getContentPane(); contentPane.setLayout(null); contentPane.add(new Label("刘铁锤真的棒")); } }
三、标签
label
new Label("xxx");
图标ICON
public class ImageIconDemo extends JFrame { public ImageIconDemo(){ //获取图片的地址 JLabel label = new JLabel("ImageIcon"); URL url = ImageIconDemo.class.getResource("迦南.png"); ImageIcon imageIcon = new ImageIcon(url);//命名不要和系统类冲突 label.setIcon(imageIcon); label.setHorizontalAlignment(SwingConstants.CENTER); Container contentPane = getContentPane(); contentPane.add(label); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,200,200); } public static void main(String[] args) { new ImageIconDemo(); } }
四、面板
public class TestJPanel extends JFrame { public static void main(String[] args) { new TestJPanel(); } public TestJPanel(){ Container contentPane = this.getContentPane(); contentPane.setLayout(new GridLayout(2,1,10,10));//后面的参数意思是间距 JPanel jPanel = new JPanel(new GridLayout(1, 3)); jPanel.add(new JButton("1")); jPanel.add(new JButton("1")); jPanel.add(new JButton("1")); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setSize(500,500); contentPane.add(jPanel); } }
-
JScrollPanel
public class JScrollDemo extends JFrame { public static void main(String[] args) { new JScrollDemo(); } public JScrollDemo(){ Container contentPane = getContentPane(); //文本域 JTextArea jTextArea = new JTextArea(20, 50); jTextArea.setText("刘铁锤真的棒"); //Scroll面板 JScrollPane jScrollPane = new JScrollPane(jTextArea); contentPane.add(jScrollPane); setVisible(true); setBounds(100,100,300,350); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }