一、创建一个JFrame窗口
package GUI;
import java.awt.*;
import javax.swing.*;
public class new2_4 {
public void init(){
JFrame jframe = new JFrame("这是一个JFrame窗口");
jframe.setVisible(true);
jframe.setBounds(100,100,200,200);
jframe.setBackground(Color.cyan);
JLabel jlabel = new JLabel("欢迎来到Java学习");
jframe.add(jlabel);
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new new2_4().init();
}
}
二、继承一个JFrame窗口
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class new2_41 {
public static void main(String[] args) {
new frame2().init();
}
}
class frame2 extends JFrame{
public void init() {
this.setBounds(200,200,500,500);
this.setVisible(true);
Container container = this.getContentPane();
container.setBackground(Color.BLUE);
JLabel label = new JLabel("欢迎来到java的学习");
label.setHorizontalAlignment(SwingConstants.CENTER);
container.add(label);
}
}
三、弹窗JDialog
package GUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class new2_5 extends JFrame {
public new2_5(){
this.setVisible(true);
this.setBounds(500, 500, 300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JButton b1 = new JButton("点击弹出一个对话框");
b1.setBounds(60, 60, 150, 150);
container.add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialogDemoo();
}
});
}
public static void main(String[] args) {
new new2_5();
}
}
class MyDialogDemoo extends JDialog{
public MyDialogDemoo(){
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
Container container = getContentPane();
JLabel label = new JLabel("老师带你学JAVA");
container.add(label);
}
}