之前已经用到了一些组件,下面列出一些常用组件,掌握这些就可以写一些简单的图形应用程序了。
- 文本,图片,按钮,弹窗,单选框,复选框,下拉框完整使用代码:
public class TestComponent1 {
public static void main(String[] args) {
JFrame jFrame = new JFrame("LoL");
jFrame.setSize(400, 450);
jFrame.setLocation(200, 200);
jFrame.setLayout(null);
JLabel jLabelText = new JLabel("LOL文字");
//文字颜色
jLabelText.setForeground(Color.red);
jLabelText.setBounds(50, 50, 280, 30);
jFrame.add(jLabelText);
JLabel jLabelImg = new JLabel();
//根据图片创建ImageIcon对象
ImageIcon i = new ImageIcon(Objects.requireNonNull(TestGUI2.class.getClassLoader().getResource("1.png")));
//设置ImageIcon
jLabelImg.setIcon(i);
//label的大小设置为ImageIcon,否则显示不完整
jLabelImg.setBounds(10, 200, i.getIconWidth(), i.getIconHeight());
jFrame.add(jLabelImg);
JButton jButton = new JButton("按钮");
jButton.setBounds(10, 10, 80,40);
jFrame.add(jButton);
JCheckBox bCheckBox = new JCheckBox("物理英雄");
//设置 为 默认被选中
bCheckBox.setSelected(true);
bCheckBox.setBounds(150, 20, 130, 30);
JCheckBox bCheckBox2 = new JCheckBox("魔法英雄");
bCheckBox2.setBounds(150, 50, 130, 30);
JRadioButton jRadioButton1 = new JRadioButton("物理英雄");
jFrame.add(bCheckBox);
jFrame.add(bCheckBox2);
// 设置 为 默认被选中
jRadioButton1.setSelected(true);
jRadioButton1.setBounds(50, 80, 130, 30);
JRadioButton jRadioButton2 = new JRadioButton("魔法英雄");
jRadioButton2.setBounds(50, 130, 130, 30);
System.out.println(jRadioButton2.isSelected());
jFrame.add(jRadioButton1);
jFrame.add(jRadioButton2);
//判断 是否 被 选中
System.out.println(bCheckBox2.isSelected());
// 按钮分组
ButtonGroup bg = new ButtonGroup();
// 把单选框放在同一个按钮分组对象里这样同一时间,只有一个单选框会被选中
bg.add(jRadioButton1);
bg.add(jRadioButton2);
String[] heros = new String[] { "卡特琳娜", "库奇" };
JComboBox comboBox = new JComboBox(heros);
comboBox.setBounds(200, 150, 80, 30);
jFrame.add(comboBox);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
//JOptionPane用于弹出对话框
int option = JOptionPane.showConfirmDialog(jFrame, "是否 使用外挂 ?");
if (JOptionPane.OK_OPTION == option) {
String answer = JOptionPane.showInputDialog(jFrame, "请输入yes,表明使用外挂后果自负");
if ("yes".equals(answer))
JOptionPane.showMessageDialog(jFrame, "你使用外挂被抓住! 罚拣肥皂3次!");
}
}
}
- 输入框,密码框,多行文本框以及进度条完整使用代码:
public class TestComponent2 {
public static void main(String[] args) {
JFrame f = new JFrame("title");
f.setSize(600, 500);
f.setLocation(150, 150);
f.setLayout(new FlowLayout());
JLabel lName = new JLabel("账号:");
// 输入框
JTextField tfName = new JTextField("");
tfName.setText("请输入账号");
tfName.setPreferredSize(new Dimension(80, 30));
f.add(lName);
f.add(tfName);
JLabel l = new JLabel("密码:");
// 密码框
JPasswordField pf = new JPasswordField("");
pf.setText("&48kdh4@#");
pf.setPreferredSize(new Dimension(80, 30));
// 与文本框不同,获取密码框里的内容,推荐使用getPassword,该方法会返回一个字符数组,而非字符串
char[] password = pf.getPassword();
String p = String.valueOf(password);
System.out.println(p);
f.add(l);
f.add(pf);
JLabel label = new JLabel("文本域:");
JTextArea ta = new JTextArea();
ta.setPreferredSize(new Dimension(200, 150));
//\n换行符
ta.setText("抢人头!\n抢你妹啊抢!\n");
//追加数据
ta.append("我去送了了了了了了了了了了了了了了了了了了了了了了了了");
//设置自动换行
ta.setLineWrap(true);
f.add(label);
f.add(ta);
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);
}
}
3.打开和保存文件完整代码:
public class TestComponent3 {
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 Auto-generated method stub
return ".txt";
}
@Override
public boolean accept(File f) {
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.showMessageDialog(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());
}
}
});
}
}
Q.E.D.