GUI编程-Day3
Swing之JFrame
public class JFrame extends Frame...
JFrame是Frame的扩展类,丰富了Frame窗体
package GUIDay3;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//初始化
public void init(){
JFrame jFrame = new JFrame("JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(0, 0, 300, 300);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = jFrame.getContentPane();//这是一个窗口的方法,返回Container类
container.setBackground(Color.YELLOW);
jFrame.add(new Label("Today is the day"));
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
JDialog弹窗
package GUIDay3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new FlowLayout());//把布局设为null,则添加的Label不显示。
container.add(new Label("Base",Label.LEFT));
JButton jButton = new JButton("弹窗");
jButton.setBounds(30, 30, 200, 50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(jButton);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new FlowLayout());//把布局设为null,则添加的Label不显示。
container.add(new Label("JAVA Learning"));
container.setVisible(true);
}
}
标签
new JLabel
Icon
package GUIDay3;
import javax.swing.*;
import java.awt.*;
import static javax.swing.SwingConstants.CENTER;
public class IconDemo extends JFrame implements Icon {//注意此种写法
private int width;
private int height;
public IconDemo(String title) {
super(title);
}
public IconDemo(int width,int height) {
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
iconDemo.setSize(300,300);
//图标放在标签上
JLabel label = new JLabel("icon", iconDemo,SwingConstants.CENTER);
Container container = this.getContentPane();//不能写成iconDemo.getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo("MyWindow").init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.YELLOW);
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
ImageIcon
package GUIDay3;
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("Koala.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(200, 200, 200, 200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
JScrollPane
package GUIDay3;
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);
// jScrollPane.add(textArea);
container.add(jScrollPane);
setBounds(200, 200, 200, 200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
单选框/复选框
JRadionButton / JCheckBox
列表
- 下拉框
- 列表框
文本框/域
JTextField / JTextArea
狂神说