Swing
3.1、窗口、面板
创建一个JFrame窗口
JFrame jf = new JFrame();
设置是否显示
jf.setVisible(true);
设置位置大小
jf.setBounds(100,100,200,200);
设置颜色
jf.setBackground(Color.cyan);
设置一个标签并且有文字“欢迎”
JLabel label = new JLabel("欢迎");
将标签加上到JFrame窗口
jf.add(label);
关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
3.2、弹窗
JDialog,用来被弹出,默认就有关闭事件!
具体可以看示例
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(700,500);
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
button.addActionListener(new ActionListener() {//添加一个监听事件,当被点击时运行actionPerformed(ActionEvent e)方法
@Override
public void actionPerformed(ActionEvent e) {
JDialog jd = new JDialog();
jd.setVisible(true);
jd.setBounds(100,100,500,500);
Container contentPane1 = jd.getContentPane();
contentPane1.setLayout(null);
contentPane1.add(new Label("你好"));
}
});
Container contentPane = jf.getContentPane();
contentPane.setLayout(null);
contentPane.add(button);
}
3.3、标签
label
new JLabel("xxx");
需要实现Icon接口
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Container contentPane = jFrame.getContentPane();
Icon icon = new Icon() {
private int height = 15;
private int width = 15;
@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;
}
};
contentPane.add(new JLabel("nihao",icon,SwingConstants.CENTER));
jFrame.setVisible(true);
}
**3.4、面板**
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Container contentPane = jFrame.getContentPane();
JPanel jPanel = new JPanel();
jPanel.add(new JButton("1"));
contentPane.add(jPanel);
jFrame.setVisible(true);
}
JScrollPanel
会有混动条的效果
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Container contentPane = jFrame.getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("nihao");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
jFrame.setVisible(true);
jFrame.setBounds(100,100,300,350);
}
3.5、按钮
图片按钮
3.6、列表
下拉框
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Container contentPane = jFrame.getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("正在热映");
jComboBox.addItem("正在热映");
jComboBox.addItem("正在热映");
contentPane.add(jComboBox);
jFrame.setVisible(true);
jFrame.setSize(500,350);
}
列表框
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = this.getContentPane();
//生成列表的内容
//String[] contents = {"1","2","3"};
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("zhangsan");
contents.add("lisi");
contents.add("wangwu");
container.add(jList);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
3.7、文本框
文本框
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Container contentPane = jFrame.getContentPane();
JTextField jTextField = new JTextField("sad");
contentPane.add(jTextField);
jFrame.setVisible(true);
jFrame.setSize(500,350);
}
密码框
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Container contentPane = jFrame.getContentPane();
JPasswordField passwordField = new JPasswordField(); //****
passwordField.setEchoChar('*');
contentPane.add(passwordField);
jFrame.setVisible(true);
jFrame.setSize(500,350);
}
文本域(有滚动条的)
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Container container = jFrame.getContentPane();
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习狂神说Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
jFrame.setVisible(true);
jFrame.setSize(500,350);
}