JAVA的图形界面下有两组控件,一组是awt,一组是swing。
一般都是使用swing
标签
Label用于显示文字
package gui;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Text {
public static void main(String[] args) {
JFrame f=new JFrame("LOL");
f.setSize(400,300);
f.setLocation(200,200);
f.setLayout(null);
JLabel l=new JLabel("LOL文字");
l.setForeground(Color.red);
l.setBounds(50,50,280,30);
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
使用JLabel显示图片
java GUI 显示图片是通过在label上设置图标实现的
注: 这里的图片路径是D:/shana.png,所以要确保这里有图片,不然不会显示
注: 图片的后缀名和真实格式,必须保持一致,否则很有可能无法正常显示。 什么意思呢?就是文件本来是jpg的,但是仅仅是把后缀名修改成了png,那么会导致显示失败。
package gui;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Text {
public static void main(String[] args) {
JFrame f=new JFrame("LOL");
f.setSize(400,300);
f.setLocation(500,200);
f.setLayout(null);
JLabel l=new JLabel();
ImageIcon i=new ImageIcon("D:/shana.png");
l.setIcon(i);
l.setBounds(50,50,i.getIconWidth(),i.getIconHeight());
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
按钮
JButton 普通按钮
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("秒杀");
b.setBounds(50, 50, 280, 30);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
复选框
JCheckBox 复选框
使用isSelected来获取是否选中了
package gui;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JCheckBox jcb1=new JCheckBox("物理英雄");
//设置默认选中
jcb1.setSelected(true);
jcb1.setBounds(50,50,130,30);
JCheckBox jcb2=new JCheckBox("魔法英雄");
jcb2.setBounds(50,100,130,30);
System.out.println(jcb2.isSelected());
f.add(jcb1);
f.add(jcb2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
单选框
JRadioButton 单选框
使用isSelected来获取是否选中了
在这个例子里,两个单选框可以被同时选中,为了实现只能选中一个,还需要用到ButtonGroup
package gui;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JRadioButton jrb1=new JRadioButton("物理英雄");
//设置默认选中
jrb1.setSelected(true);
jrb1.setBounds(50,50,130,30);
JRadioButton jrb2=new JRadioButton("魔法英雄");
jrb2.setBounds(50,100,130,30);
System.out.println(jrb2.isSelected());
f.add(jrb1);
f.add(jrb2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
按钮组
ButtonGroup 对按钮进行分组,把不同的按钮,放在同一个分组里 ,同一时间,只有一个 按钮 会被选中
package gui;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JRadioButton jrb1=new JRadioButton("物理英雄");
//设置默认选中
jrb1.setSelected(true);
jrb1.setBounds(50,50,130,30);
JRadioButton jrb2=new JRadioButton("魔法英雄");
jrb2.setBounds(50,100,130,30);
System.out.println(jrb2.isSelected());
ButtonGroup bg=new ButtonGroup();
// 把b1,b2放在 同一个 按钮分组对象里 ,这样同一时间,只有一个 按钮 会被选中
bg.add(jrb1);
bg.add(jrb2);
f.add(jrb1);
f.add(jrb2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
下拉框
JComboBox 下拉框
使用getSelectedItem来获取被选中项
使用setSelectedItem() 来指定要选中项
package gui;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
String[] heros=new String[]{"安妮","盖伦"};
JComboBox cb=new JComboBox(heros);
cb.setBounds(50,50,80,30);
f.add(cb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
对话框
JOptionPane 用于弹出对话框
JOptionPane.showConfirmDialog(f, “是否 开飞机 ?”));
表示询问,第一个参数是该对话框以哪个组件对齐
JOptionPane.showInputDialog(f,“请输入OK,准备起飞”);
接受用户的输入
JOptionPane.showMessageDialog(f, “坠机了”);
显示消息
package gui;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
int option=JOptionPane.showConfirmDialog(f, "是否 开飞机 ?");
if(JOptionPane.OK_OPTION==option){
String answer=JOptionPane.showInputDialog(f,"请输入OK,准备起飞");
if("OK".equals(answer))
JOptionPane.showMessageDialog(f, "坠机了");
}
}
}
文本框
JTextField 输入框
setText 设置文本
getText 获取文本
JTextField 是单行文本框,如果要输入多行数据,请使用JTextArea
tfPassword.grabFocus(); 表示让密码输入框获取焦点
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(new FlowLayout());
JLabel lname=new JLabel("账号:");
JTextField tname =new JTextField("");
tname.setText("请输入账号");
tname.setPreferredSize(new Dimension(80,30));
JLabel lpassword=new JLabel("密码:");
JTextField tpassword=new JTextField();
tpassword.setText("请输入密码");
tpassword.setPreferredSize(new Dimension(80,30));
f.add(lname);
f.add(tname);
f.add(lpassword);
f.add(tpassword);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
密码框
JPasswordField 密码框
与文本框不同,获取密码框里的内容,推荐使用getPassword,该方法会返回一个字符数组,而非字符串
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(new FlowLayout());
JLabel l=new JLabel("密码:");
//密码框
JPasswordField ps=new JPasswordField();
ps.setPreferredSize(new Dimension(80,30));
ps.setText("asd51asdas");
// 与文本框不同,获取密码框里的内容,推荐使用getPassword,该方法会返回一个字符数组,而非字符串
char[] password = ps.getPassword();
String p = String.valueOf(password);
System.out.println(p);
f.add(l);
f.add(ps);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
文本域
JTextArea:文本域。
和文本框JTextField不同的是,文本域可以输入多行数据
如果要给文本域初始文本,通过\n来实现换行效果
JTextArea通常会用到append来进行数据追加
如果文本太长,会跑出去,可以通过setLineWrap(true) 来做到自动换行
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(new FlowLayout());
JLabel l=new JLabel("文本域:");
JTextArea ta=new JTextArea();
ta.setPreferredSize(new Dimension(200,150));
ta.setText("冲啊!!!!!!!!\n骑兵连!!!!!!\n");
ta.append("进攻!!!!");
ta.setLineWrap(true);
f.add(l);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
进度条
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(new FlowLayout());
JProgressBar pb=new JProgressBar();
//进度条最大100
pb.setMaximum(100);
//当前进度是50
pb.setValue(50);
//显示当前进度
pb.setStringPainted(true);
f.add(pb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
文件选择器
JFileChooser 表示文件选择器
使用FileFilter用于仅选择.txt文件
fc.setFileFilter(new FileFilter() {
public String getDescription() {
return ".txt";
}
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt");
}
});
package gui;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
public class Text {
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setLayout(new FlowLayout());
JFileChooser fc=new JFileChooser();
fc.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
// TODO 自动生成的方法存根
return ".txt";
}
@Override
public boolean accept(File f) {
// TODO 自动生成的方法存根
return f.getName().toLowerCase().endsWith(".txt");
}
});
JButton bOpen = new JButton("打开文件");
JButton bSave = new JButton("保存文件");
f.add(bOpen);
f.add(bSave);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250, 150);
f.setLocationRelativeTo(null);
f.setVisible(true);
bOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(f);
File file=fc.getSelectedFile();
if(returnVal==JFileChooser.APPROVE_OPTION){
JOptionPane.showConfirmDialog(f, "计划打开文件"+file.getAbsolutePath());
}
}
});
bSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showSaveDialog(f);
File file = fc.getSelectedFile();
if (returnVal == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(f, "计划保存到文件:" + file.getAbsolutePath());
}
}
});
}
}
更多内容,点击了解: SWING 中的组件列表