GUI编程day07
弹窗
JDiolog,用来被弹出,默认就有关闭事件
//主窗口
public class DialogDemo01 extends JFrame {
public DialogDemo01() {
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton jbutton = new JButton("点击弹出一个对话框");//创建
jbutton.setBounds(100,100,100,50);
jbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
container.add(jbutton);
}
public static void main(String[] args) {
new DialogDemo01();
}
}
//弹窗的窗口
class MyDialog extends JDialog {
public MyDialog(){
this.setVisible(true);
this.setSize(200,100);
Container container = this.getContentPane();
//container.setLayout(null);
container.add(new JLabel("排球少年"));
}
}
标签
ICON
//图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}//无参构造
public IconDemo(int width,int height){
this.height = height;
this.width = width;
}
public void init() {
IconDemo icondemo = new IconDemo(15,15);
//图标可以放在该标签上,也可以放在按钮上
JLabel jlabel = new JLabel("icontest",icondemo,SwingConstants.CENTER);
Container container = getContentPane();
container.add(jlabel);
this.setVisible(true);
this.setSize(500,300);
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;
}
}
ImageIcon
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("sc.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);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}