3、Swing(AWT的子类)
3.1窗口、面板
public class myJFrame extends JFrame {
public myJFrame() {
setBounds(100, 100, 400, 400);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class Demo01JFrame extends myJFrame{
public Demo01JFrame(){
Container container = this.getContentPane();
container.setBackground(new Color(172, 26, 9));
container.setLayout(null);
JPanel jP = new JPanel();
jP.setBackground(new Color(1,50,1));
jP.setBounds(100,100,200,200);
add(jP);
}
}
3.2弹窗
class Demo02Frame extends myJFrame{
public Demo02Frame(){
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
Button btn1 = new Button("btn1");
btn1.setBounds(100,100,70,30);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
new myJDialog();
}
});
contentPane.add(btn1);
}
}
class myJDialog extends JDialog {
public myJDialog() {
setVisible(true);
setBounds(500,500,300,300);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container container = this.getContentPane();
Label label = new Label("成功弹出弹窗");
label.setFont(new Font("MS Song", Font.PLAIN, 9));
container.add(label);
}
}
3.3 imageIcon
public class IconJFrame extends myJFrame {
public IconJFrame(){
setSize(1000,1000);
JLabel jlabel = new JLabel();
URL url = IconJFrame.class.getResource("tp.png");
ImageIcon imageIcon = new ImageIcon(url);
jlabel.setIcon(imageIcon);
add(jlabel);
setVisible(true);
}
public static void main(String[] args) {
new IconJFrame();
}
}
3.4 JScrollPane 滚动条面板
public class JScrollDemo extends myJFrame{
public JScrollDemo(){
Container container = this.getContentPane();
JTextArea jtArea = new JTextArea(50,50);
jtArea.setText("hello");
JScrollPane scrollPane = new JScrollPane(jtArea);
scrollPane.setBounds(100,100,200,100);
container.add(scrollPane);
setVisible(true);
}
public static void main(String[] args) {
new JScrollDemo();
}
}