Swing
窗口,面板
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame02().init();
}
}
class MyJFrame02 extends JFrame {
public void init() {
this.setBounds(200, 200, 500, 500);
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.CYAN);
JLabel jLabel = new JLabel("akita");
this.add(jLabel);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
setVisible(true);
}
}
弹窗
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setSize(500, 350);
this.setLocation(300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
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 MyDialogDD();
}
});
contentPane.add(jButton);
this.setVisible(true);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialogDD extends JDialog {
public MyDialogDD() {
this.setBounds(100, 100, 500, 500);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
Label akita = new Label("akita");
akita.setSize(50, 50);
contentPane.add(akita);
this.setVisible(true);
}
}
标签
JLabel jLabel = new JLabel("标签");
package com.akita.lesson03;
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 iconDemo = new IconDemo(15, 15);
JLabel jLabel = new JLabel("icon_test", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
this.setBounds(200,200,400,340);
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;
}
}
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
JLabel akita = new JLabel("akita");
URL url = ImageIconDemo.class.getResource("icon.png");
ImageIcon imageIcon = new ImageIcon(url);
akita.setIcon(imageIcon);
akita.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(akita);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(200, 200, 500, 500);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
按钮
列表
文本框
|