3.3 -3.4 标签+面板-2022-12-12

3.3 标签-

      new  Labe("XXX");

      图标

import javax.swing.*;
import java.awt.*;

public class IconDemo extends JFrame implements Icon {

private int width;
private int height;
public IconDemo(){};

public IconDemo(int width,int height){
this.width= width;
this.height = height;
}

public void init(){
IconDemo icon1 = new IconDemo(150, 150);
//图标放标签,也可以放按钮上
JLabel label = new JLabel("icontest", icon1, SwingConstants.CENTER);

Container container = getContentPane();
container.setBackground(Color.RED);
container.add(label);
this.setBounds(100,100,500,400);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new IconDemo().init();
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}

@Override
public int getIconWidth() {
return this.width;
}

@Override
public int getIconHeight() {
return this.height;
}
}

 

      图片ICON(需要把图片资源拉到IDEA的SRC根目录下)

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {

public ImageIconDemo() {
// 获取图片地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("tx.jpg");

ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);

Container container = getContentPane();
container.add(label);

setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,600,600);
}

public static void main(String[] args) {
new ImageIconDemo();
}
}

3.4 面板

package Lesson05;

import javax.swing.*;
import java.awt.*;

class JPanelDemo01 extends JFrame{

public JPanelDemo01() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(4,2 ,10,10)); //后面两个参数为间距

JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
JPanel panel5 = new JPanel(new GridLayout(1,1));


panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel5.add(new JButton("5"));


container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
container.add(panel5);

this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new JPanelDemo01();
}
}

滚动条面板 ScrollPanel
package Lesson05;

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {

public JScrollDemo(){
Container container = this.getContentPane();

//文本域
TextArea textArea = new TextArea(20, 50);
textArea.setText("欢迎学习JAVA");

JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);

this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new JScrollDemo();
}
}
posted @ 2022-12-12 13:50  Rui2022  阅读(13)  评论(0编辑  收藏  举报