package com.zibo.lession04;
import javax.swing.*;
import java.awt.*;
public class TestJFrame {
public static void main(String[] args) {
//创建窗口
init();
}
//init 初始化
public static void init(){
//JFrame extends Frame
JFrame jFrame = new JFrame("TestJFrame");
jFrame.setBounds(300,300,500,500);
jFrame.setVisible(true);
JLabel jLabel = new JLabel("这是JLabel");
//水平居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jFrame.add(jLabel);
Container container = jFrame.getContentPane();
//设置背景颜色
container.setBackground(Color.cyan);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
package com.zibo.lession04;
import javax.swing.*;
import java.awt.*;
public class TestDialog {
public static void main(String[] args) {
init();
}
public static void init(){
JFrame jFrame = new JFrame("TestDialog");
JButton jButton = new JButton("显示dialog");
jFrame.add(jButton);
JDialog jDialog = new JDialog(jFrame, "这是弹窗的提示内容!");
jDialog.setBounds(400,400,200,200);
JLabel jLabel = new JLabel("jDialog里面的jLabel");
jDialog.add(jLabel);
jButton.addActionListener(e -> {
jDialog.setVisible(true);
} );
jFrame.setBounds(300,300,500,500);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
new Lable("XXX");
package com.zibo.lession04;
import javax.swing.*;
import java.awt.*;
public class TestIcon extends JFrame implements Icon {
private int width;
private int height;
public TestIcon() {}
public TestIcon(int width, int height) throws HeadlessException {
this.width = width;
this.height = height;
}
public void init(){
TestIcon icon = new TestIcon(width, height);
//图标放在标签上,也可以放在按钮上
JLabel jLabel = new JLabel("标签",icon,SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
setBounds(300,300,500,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestIcon(20,20).init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
package com.zibo.lession04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon extends JFrame {
public TestImageIcon() throws HeadlessException {
//获取图片地址
JLabel label = new JLabel("ImageIcon");
URL url = TestImageIcon.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setBounds(300,300,800,800);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
package com.zibo.Lession05;
import com.zibo.lession01.TestPanel;
import javax.swing.*;
import java.awt.*;
public class TestJPanel extends JFrame {
public TestJPanel(){
Container container = getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//行列间距
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
JPanel jPanel2 = new JPanel(new GridLayout(2, 3));
JPanel jPanel3 = new JPanel(new GridLayout(3, 3));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("2"));
jPanel1.add(new JButton("3"));
jPanel2.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("3"));
jPanel3.add(new JButton("1"));
jPanel3.add(new JButton("2"));
jPanel3.add(new JButton("3"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
setVisible(true);
setBounds(300,300,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanel();
}
}
package com.zibo.Lession05;
import javax.swing.*;
import java.awt.*;
//带滚动条的面板
public class TestJScrollPane extends JFrame {
public TestJScrollPane() {
Container container = getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("TestJScrollPane");
//面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
setVisible(true);
setBounds(300,300,300,350);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScrollPane();
}
}