Java Swing程序设计实战

作者:Tomas·Wilson 时间:2023-04-09 07:05:42 

一、按钮组件

1.1 提交按钮组件


package swing;

import java.awt.*;
import java.awt.event.*;
import java.net.*;

import javax.swing.*;

public class JButtonTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;

public JButtonTest() {
URL url = JButtonTest.class.getResource("imageButton.jpg");
Icon icon = new ImageIcon(url);
setLayout(new GridLayout(3, 2, 5, 5)); // 设置网格布局管理器
Container c = getContentPane(); // 创建容器
for (int i = 0; i < 5; i++) {
// 创建按钮,同时设置按钮文字与图标
JButton J = new JButton("button" + i, icon);
c.add(J); // 在容器中添加按钮
if (i % 2 == 0) {
J.setEnabled(false); // 设置其中一些按钮不可用
}
}
JButton jb = new JButton(); // 实例化一个没有文字与图片的按钮
jb.setMaximumSize(new Dimension(90, 30)); // 设置按钮与图片相同大小
jb.setIcon(icon); // 为按钮设置图标
jb.setHideActionText(true);
jb.setToolTipText("图片按钮"); // 设置按钮提示为文字
jb.setBorderPainted(false); // 设置按钮边界不显示
jb.addActionListener(new ActionListener() { // 为按钮添加监听事件
public void actionPerformed(ActionEvent e) {
// 弹出确认对话框
JOptionPane.showMessageDialog(null, "弹出对话框");
}
});
c.add(jb); // 将按钮添加到容器中

setTitle("创建带文字与图片的按钮");
setSize(350, 150);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}

public static void main(String args[]) {
new JButtonTest();
}
}

Java Swing程序设计实战

1.2 复选框组件


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CheckBoxTest extends JFrame {

/**
    *
    */
   private static final long serialVersionUID = 1L;
   private JPanel panel1 = new JPanel();
   private JPanel panel2 = new JPanel();
   private JTextArea jt = new JTextArea(3, 10);
   private JCheckBox jc1 = new JCheckBox("1");
   private JCheckBox jc2 = new JCheckBox("2");
   private JCheckBox jc3 = new JCheckBox("3");
   public CheckBoxTest() {
       Container c = getContentPane();

c.setLayout(new BorderLayout());

c.add(panel1, BorderLayout.NORTH);
       final JScrollPane scrollPane = new JScrollPane(jt);
       panel1.add(scrollPane);

c.add(panel2, BorderLayout.SOUTH);
       panel2.add(jc1);
       jc1.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (jc1.isSelected())
                   jt.append("复选框1被选中\n");
           }
       });

panel2.add(jc2);
       jc2.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (jc2.isSelected())
                   jt.append("复选框2被选中\n");
           }
       });

panel2.add(jc3);
       jc3.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (jc3.isSelected())
                   jt.append("复选框3被选中\n");
           }
       });

setSize(200, 160);
       setVisible(true);
       setTitle("复选框的使用");
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }

public static void main(String[] args) {
       new CheckBoxTest();

}

}

Java Swing程序设计实战

二、列表组件

2.1 JComboBox类


import java.awt.*;
import javax.swing.*;

public class JComboBoxModelTest extends JFrame {
private static final long serialVersionUID = 1L;
JComboBox<String> jc = new JComboBox<>(new MyComboBox());
JLabel jl = new JLabel("请选择证件");

public JComboBoxModelTest() {
setSize(new Dimension(160, 180));
setVisible(true);
setTitle("在窗口中设置下拉列表框");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jl);
cp.add(jc);
}

public static void main(String[] args) {
new JComboBoxModelTest();

}

}

class MyComboBox extends AbstractListModel<String> implements ComboBoxModel<String> {
/**
*
*/
private static final long serialVersionUID = 1L;
String selecteditem = null;
String[] test = { "身份证", "军人证", "学生证", "工作证" };

public String getElementAt(int index) {
return test[index];
}

public int getSize() {
return test.length;
}

public void setSelectedItem(Object item) {
selecteditem = (String) item;
}

public Object getSelectedItem() {
return selecteditem;
}

public int getIndex() {
for (int i = 0; i < test.length; i++) {
if (test[i].equals(getSelectedItem()))
return i;
}
return 0;
}
}

Java Swing程序设计实战

2.2 列表框组件


import java.awt.*;

import javax.swing.*;

public class JListTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;

public JListTest() {
Container cp = getContentPane();

cp.setLayout(null);
JList<String> jl = new JList<>(new MyListModel());
JScrollPane js = new JScrollPane(jl);
js.setBounds(10, 10, 100, 100);
cp.add(js);
setTitle("在这个窗体中使用了列表框");
setSize(200, 150);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}

public static void main(String args[]) {
new JListTest();
}
}

class MyListModel extends AbstractListModel<String> {
/**
*
*/
private static final long serialVersionUID = 1L;
private String[] contents = { "列表1", "列表2", "列表3", "列表4", "列表5", "列表6" };

public String getElementAt(int x) {
if (x < contents.length)
return contents[x++];
else
return null;
}

public int getSize() {
return contents.length;
}
}

Java Swing程序设计实战

三、文本组件

3.1 文本框组件


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class JTextFieldTest extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

public JTextFieldTest() {
setSize(250, 100);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp = getContentPane();
getContentPane().setLayout(new FlowLayout());
final JTextField jt = new JTextField("aaa", 20);
final JButton jb = new JButton("清除");
cp.add(jt);
cp.add(jb);
jt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
jt.setText("触发事件");
}
});
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
jt.requestFocus();
}
});
setVisible(true);
}

public static void main(String[] args) {
new JTextFieldTest();
}
}

Java Swing程序设计实战

3.2 密码框


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class JTextFieldTest extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

public JTextFieldTest() {
setSize(250, 100);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp = getContentPane();
getContentPane().setLayout(new FlowLayout());
//final JTextField jt=new JTextField("aaa",20);
JPasswordField jp = new JPasswordField("", 20);
jp.setEchoChar('*');
final JButton jb = new JButton("清除");

cp.add(jp);
cp.add(jb);
jp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
jp.setText("触发事件");
}
});
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jp.setText("");
jp.requestFocus();
}
});
setVisible(true);
}

public static void main(String[] args) {
new JTextFieldTest();
}
}

Java Swing程序设计实战

3.3 文本域组件


import java.awt.*;
import javax.swing.*;

public class JTextAreaTest extends JFrame {
public JTextAreaTest() {
setSize(200, 100);
setTitle("定义自动换行的文本域");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cp = getContentPane();
JTextArea jt = new JTextArea("文本域", 6, 6);
jt.setLineWrap(true);
cp.add(jt);
setVisible(true);
}

public static void main(String[] args) {
new JTextAreaTest();

}

}

Java Swing程序设计实战

四、常用时间 *

4.1 动作事件 *


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SimpleEvent extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
private JButton jb = new JButton("我是按钮,点击我");

public SimpleEvent() {
setLayout(null);
setSize(200, 100);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp = getContentPane();
cp.add(jb);
jb.setBounds(10, 10, 100, 30);
jb.addActionListener(new jbAction());
setVisible(true);
}

class jbAction implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
jb.setText("我被单击了");
}
}

public static void main(String[] args) {
new SimpleEvent();
}
}

Java Swing程序设计实战

Java Swing程序设计实战

4.2 焦点事件 *


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class FocusEventTest extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

public FocusEventTest() {
setSize(250, 100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cp = getContentPane();
getContentPane().setLayout(new FlowLayout());

final JLabel label = new JLabel();
getContentPane().add(label);
JTextField jt = new JTextField("请单击其他文本框", 10);
JTextField jt2 = new JTextField("请单击我", 10);
cp.add(jt);
cp.add(jt2);
jt.addFocusListener(new FocusListener() {
// 组件失去焦点时调用的方法
public void focusLost(FocusEvent arg0) {
JOptionPane.showMessageDialog(null, "文本框失去焦点");
}

// 组件获取键盘焦点时调用的方法
public void focusGained(FocusEvent arg0) {
}
});
setVisible(true);
}

public static void main(String[] args) {
new FocusEventTest();
}

}

Java Swing程序设计实战
Java Swing程序设计实战

来源:https://blog.csdn.net/weixin_46322367/article/details/116887998

标签:Java,Swing,程序,设计
0
投稿

猜你喜欢

  • C# TextBox控件实现只能输入数字的方法

    2022-03-07 01:57:27
  • 深入java内存查看与分析详解

    2021-06-14 06:48:45
  • SpringBoot 集成 activiti的示例代码

    2023-01-22 10:22:03
  • C#异常捕获机制图文详解

    2023-05-22 19:39:20
  • java后台接受到图片后保存方法

    2023-06-03 09:23:04
  • C#中的timer与线程使用

    2023-08-21 00:13:11
  • Java程序顺序结构中逻辑控制语句详解流程

    2023-06-10 17:54:08
  • Java class文件格式之访问标志信息_动力节点Java学院整理

    2022-10-31 18:57:29
  • 基于spring中的aop简单实例讲解

    2023-10-16 21:01:10
  • interrupt()和线程终止方式_动力节点Java学院整理

    2021-09-27 08:50:53
  • SpringBoot + validation 接口参数校验的思路详解

    2023-10-09 11:59:12
  • C#中的where泛型约束介绍

    2022-11-26 12:21:34
  • Java Stream流零基础教程

    2023-08-15 19:33:20
  • Java编程实现从尾到头打印链表代码实例

    2021-12-28 12:13:41
  • 浅谈Silverlight 跨线程的使用详解

    2021-10-16 01:23:05
  • Android自定义轮播图效果

    2022-10-27 06:31:26
  • C#动态编译并执行字符串样例

    2022-02-10 22:26:53
  • java绘制五子棋棋盘

    2022-05-10 09:37:36
  • Java设计模式编程之解释器模式的简单讲解

    2022-01-24 16:03:32
  • Android Activity通用悬浮可拖拽View封装的思路详解

    2023-08-08 15:31:48
  • asp之家 软件编程 m.aspxhome.com