javax.swing.*;
import javax.swing.*;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
new Demo();
//顶级窗口
JFrame jf = new JFrame("JFrame窗口");
jf.setBounds(100,100,300,200);
jf.setVisible(true);
//设置标签
JLabel label = new JLabel("大家好!!");
jf.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);////设置水平对齐,让文本标签居中
//获得一个容器
Container container = jf.getContentPane();
container.setBackground(Color.GREEN);//用容器设置背景颜色
//关闭窗口
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
窗口背景颜色问题:
在使用JFrame创建窗体时,直接调用setBackground(Color.)这个方法后,你看到的却不是直接的JFrame,而是JFrame.getContentPane();而JFrame上的contentPane默认是Color.WHITE的,所以,无论你对JFrame怎么设置背景颜色,你看到的都只是contentPane,我们就需要通过容器来设置背景颜色。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setSize(400,300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = jFrame.getContentPane();
container.setLayout(null);//绝对布局
JButton button = new JButton("点击弹出一个弹窗"); //创建按钮
button.setBounds(100,30,200,50);
//监听器
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
});
container.add(button);
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setBounds(150,150,300,200);
this.setVisible(true);
Container container = this.getContentPane();
container.setLayout(null);
}
}
//将图标放在标签(JLabel)上;实现这个接口需要重写它的三个方法;
import javax.swing.*;
import java.awt.*;
public class Demo implements Icon {
private int width;
private int height;
public Demo(int width,int height){
this.width = width;
this.height = height;
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
Demo demo = new Demo(15,15);
JLabel jLabel = new JLabel("icon",demo,SwingConstants.CENTER);
Container container = jFrame.getContentPane();
container.add(jLabel);
/*将图标放在按钮上
JButton button = new JButton("button", demo);
Container container = jFrame.getContentPane();
container.add(button);*/
jFrame.setVisible(true);
}
@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;
}
}
//利用URL获取图片地址
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("picture.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(10,10,300,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
//用JPanel布局一些按钮
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
public static void main(String[] args) {
new Demo();
}
public Demo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10)); //参数:行数,列数,布局之间的间距
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
public Demo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
//textArea.setText("");可以设定文本域的内容
//Scroll面板,带有滚动条
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo();
}
}
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
public Demo() {
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("Button01");
JRadioButton radioButton2 = new JRadioButton("Button02");
JRadioButton radioButton3 = new JRadioButton("Button03");
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(400,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo();
}
}
import javax.swing.*;
import java.awt.*;
/**
* @Description:
* @Author: Mr.Chen
* @CreateTime: 2019-11-30 21:59
*/
public class Demo extends JFrame {
public Demo() {
Container container = this.getContentPane();
//多选框
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
JCheckBox checkBox03 = new JCheckBox("checkBox03");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.CENTER);
container.add(checkBox03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(400,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo();
}
}
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
public Demo() {
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);//空选项
status.addItem("111111");
status.addItem("222222");
status.addItem("333333");
container.add(status);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo();
}
}
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Demo extends JFrame {
public Demo() {
Container container = this.getContentPane();
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("111111");
contents.add("222222");
contents.add("333333");
container.add(jList);
this.setVisible(true);
this.setSize(300,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo();
}
}