继续(二)的内容
查看上篇内容:
继续(一)的内容
查看上篇内容:
跟着大神学java第十五课,GUI编程(一):
https://blog.csdn.net/weixin_42681553/article/details/121356227
跟着大神学java第十五课,GUI编程(二):
https://blog.csdn.net/weixin_42681553/article/details/121606751
3、Swing
3.1、窗口、面板
设置 JFrame 颜色、文本居中
public class JFrameDemo {
//init(); //初始化
public static void init() {
JFrame jFrame = new JFrame("JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(100, 100, 200, 200);
jFrame.setBackground(Color.BLUE); //只设置这个颜色不生效,没有定义容器,jFrame、jLabel 都是容器
jFrame.getContentPane().setBackground(Color.CYAN); //这个生效,获取 jFrame 中的容器并设定颜色
//设置文字并居中
// JLabel jLabel = new JLabel("这是内容");
JLabel jLabel = new JLabel("这是内容", SwingConstants.CENTER);
jFrame.add(jLabel);
// //设置文本水平对其(居中)
// jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//关闭事件,使用 JFrame 的方法,默认有关闭的监听器,无需自己写
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
//与 Frame 对比,使用方法相似,JFrame 继承 Frame
3.2、弹窗
//弹窗
//需要主窗口和一个按钮
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//获取 JFrame 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹框");
button.setBounds(30, 30, 200, 50);
//监听点击按钮
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
//添加按钮到主窗口
container.add(button);
}
public static void main(String[] args) {
//主窗口
new DialogDemo();
}
}
//弹出的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,100);
Container container = this.getContentPane();
container.setLayout(null);
JLabel label = new JLabel("弹窗内容", SwingConstants.CENTER);
label.setBounds(0, 0, 500, 50); //文字会按当前设置的大小分布
container.add(label);
}
}
3.3、标签
标签
new Lable("标签内容");
图形标签
//图标,需要实现类,还需要继承Frame
public class IconDemo extends JFrame implements Icon {
//属性
private int width, height;
//无参构造
public IconDemo(){ }
//有参构造
public IconDemo(int width, int height){
this.height = height;
this.width = width;
}
public void init(){
//基本属性
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//图标内容
IconDemo iconDemo = new IconDemo(15, 15); //设置图标大小
//图形放在标签上,也可以放在按钮等其他地方
JLabel label = new JLabel("Icon", iconDemo, SwingConstants.CENTER); //设置图标、文字内容、居中展示(图标在文字前面)
Container container = this.getContentPane();
container.add(label);
}
public static void main(String[] args) {
//调用初始化生成图形标签
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.drawOval(x, y, width, height); //画圆当图标标签
}
@Override
public int getIconWidth() { return this.width; }
@Override
public int getIconHeight() { return this.height; }
}
图片标签
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
//获取图片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("rfgc.png");//获取当前类的统计目录下的rfgc.png
//图片标签
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER); //居中
//添加到窗口中
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(100,100,300,300);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
Ps:初始项目启动,存在报错指向 url 空指针,再次启动正常,原因暂时未找到
3.4、面板
JPanel 面板布局
//面板间隔和布局
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 2, 10, 10)); //(int rows, int cols, int hgap, int vgap) 行、列,行间距,列检局
JPanel panel1 = new JPanel(new GridLayout(2, 1));
JPanel panel2 = new JPanel(new GridLayout(1, 2));
JPanel panel3 = new JPanel(new GridLayout(2, 2));
JPanel panel4 = new JPanel(new GridLayout(1, 1));
JPanel panel5 = new JPanel(new GridLayout(1, 1)); //虽然container 设置的为两行两列,但依旧可以放第五个 panel
panel1.add(new JButton("点击"));
panel1.add(new JButton("点击"));
panel2.add(new JButton("点击"));
panel2.add(new JButton("点击"));
panel3.add(new JButton("点击"));
panel3.add(new JButton("点击"));
panel3.add(new JButton("点击"));
panel3.add(new JButton("点击"));
panel4.add(new JButton("点击"));
panel5.add(new JButton("点击"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
container.add(panel5);
//整体分布会按照行数,然后将 panel 按照等分,排列在表格中
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScroll 滚动条
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//通过 文本域 展示内容,可以换行
TextArea textArea = new TextArea();
textArea.setText("跟着大佬一起学 JAVA。");
/* 下面部分内容为个人终端展示效果时出现的问题,未对其他终端进行验证
注意:当文本域中的内容过多时,会出现另外一条滚动条,用于滚动查看上下文内容, scrollPane 滚动条无法滚动查看上下内容
*/
//给文本域增加右侧滚动条,当窗口大小小于 container 大小时,部分pane 会被隐藏,滚动条会出来
JScrollPane scrollPane = new JScrollPane(textArea); //滚动条在 textArea 的大小被隐藏时展示
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100, 100, 500, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按钮
普通图片按钮:
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//将图片变为图标
URL resource = JButtonDemo01.class.getResource("rfgc.png");
Icon icon = new ImageIcon(resource);
//图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮"); //鼠标停留显示文字
container.add(button);
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮:
//单选按钮
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container container = this.getContentPane();
//单选按钮
JRadioButton radioButton01 = new JRadioButton("radioButton01");
JRadioButton radioButton02 = new JRadioButton("radioButton02");
JRadioButton radioButton03 = new JRadioButton("radioButton03");
//按钮通过组,实现单选,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);
//布局,上中下各一个,同一行流式布局(FlowLayout),不同行边界布局(BorderLayout)或者表格布局(GridLayout)
container.add(radioButton01, BorderLayout.NORTH);
container.add(radioButton02, BorderLayout.CENTER);
container.add(radioButton03, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,400,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
复选按钮:
//多选框
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container container = this.getContentPane();
//多选框
Checkbox checkbox01 = new Checkbox("checkbox01");
Checkbox checkbox02 = new Checkbox("checkbox02");
Checkbox checkbox03 = new Checkbox("checkbox03");
container.add(checkbox01, BorderLayout.NORTH);
container.add(checkbox02, BorderLayout.EAST);
container.add(checkbox03, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,400,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
下拉框:
//下拉框
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
int item = 0;
Container container = this.getContentPane();
JPanel panel = new JPanel();
JComboBox status = new JComboBox();
//设置下拉框选项
status.addItem(null);
status.addItem("下拉选项一");
status.addItem("下拉选项二");
status.addItem("下拉选项三");
status.addItem("下拉选项四");
panel.add(status);
panel.setSize(200,50);
container.add(panel);
//todo:可以尝试增加监听,显示下拉框的变动
//todo:可以尝试增加多个下拉框进行联动(例: 中国-山东-济南)
this.setVisible(true);
this.setBounds(100, 100, 400, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
列表框:
//列表框,只展示
public class TextComboboxDemo02 extends JFrame {
public TextComboboxDemo02() {
Container container = this.getContentPane();
// //静态数组
// //创建列数组
// String[] contens = {" 张三"," 李四", "王五"};
// //将数组放入列表
// JList list = new JList(contens);
// //将列表加入到面板中
// container.add(list);
//动态数组
Vector contens = new Vector();
JList jList = new JList(contens);
contens.add("zhangsan");
contens.add("lisi");
contens.add("wangwu");
//todo:可以尝试动态增加或减少列表框的内容
container.add(jList);
this.setVisible(true);
this.setBounds(100,100,400,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextComboboxDemo02();
}
}
3.7、文本框
普通文本框:
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world");
container.add(textField1, BorderLayout.NORTH);
container.add(textField2, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100, 100, 400, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
密码文本框:
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = this.getContentPane();
//密码框
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*'); //设置加密形式,不设置则默认为黑点
container.add(passwordField);
this.setVisible(true);
this.setSize(500,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
文本域:
public class TestTextDemo03 extends JFrame {
public TestTextDemo03() {
Container container = this.getContentPane();
//通过 文本域 展示内容,可以换行
TextArea textArea = new TextArea();
textArea.setText("跟着大佬一起学 JAVA。");
container.add(textArea);
this.setVisible(true);
this.setBounds(100, 100, 500, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo03();
}
}
之前在 面板–JScroll 滚动条 处使用过
小结
涉及到的内容:
- HTML
- 后期学习前端界面绘制时会用到的面板、布局等
- Serviet
- 监听器