1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
| import javax.swing.*; import javax.swing.filechooser.FileFilter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File;
public class 组件 { public static void main(String[] args) {
new 文件选择器(); } }
class 标签{ public 标签() { JFrame f = new JFrame("label"); f.setSize(200, 100); f.setLocation(200, 200); f.setLayout(null);
JLabel l = new JLabel("这是一个标签"); l.setForeground(Color.RED); l.setBounds(50,10,280,30);
f.add(l); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
class 图片{ public 图片() { JFrame f = new JFrame("icon"); f.setSize(200, 220); f.setLocation(200, 200); f.setLayout(null);
JLabel l = new JLabel(); ImageIcon img = new ImageIcon("src/main/resources/GUI/组件/avj.jpg"); l.setIcon(img); l.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
f.add(l); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
class 复选框{ public 复选框() { JFrame f = new JFrame("复选框"); f.setSize(400, 300); f.setLocation(580, 200); f.setLayout(null);
JCheckBox bCheckBox = new JCheckBox("键盘"); bCheckBox.setSelected(true); bCheckBox.setBounds(50, 50, 130, 30);
JCheckBox bCheckBox1 = new JCheckBox("鼠标"); bCheckBox1.setBounds(50, 100, 130, 30); JLabel l = new JLabel(); l.setBounds(300,50,150,50); l.setForeground(Color.BLUE); new Thread(() -> { while (true) { l.setText(String.valueOf(bCheckBox1.isSelected())); ; } }).start();
f.add(bCheckBox); f.add(bCheckBox1); f.add(l); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); } }
class 单选框{ public 单选框() { JFrame f = new JFrame("单选框"); f.setSize(400, 300); f.setLocation(580, 200); f.setLayout(null);
JRadioButton b1 = new JRadioButton("有线"); b1.setSelected(true); b1.setBounds(50, 50, 130, 30);
JRadioButton b2 = new JRadioButton("无线鼠标"); b2.setBounds(50, 100, 130, 30);
JRadioButton b3 = new JRadioButton("键盘"); b3.setBounds(50,150,130,30);
ButtonGroup bg = new ButtonGroup(); bg.add(b1); bg.add(b2);
f.add(b1); f.add(b2); f.add(b3); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); } }
class 下拉框{
public 下拉框() { JFrame f = new JFrame("涩涩方法"); f.setSize(400, 300); f.setLocation(580, 240); f.setLayout(null);
String[] control = new String[]{"阴纹Slave", "傀儡仆人", "糖果超甜", "Command Card"}; JComboBox cb = new JComboBox(control);
cb.setBounds(50,50,160,30);
f.add(cb); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
class 对话框{
public 对话框() { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(580, 240); f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
int option = JOptionPane.showConfirmDialog(f, "是否对目标使用"); if (option == JOptionPane.OK_OPTION) { String answer = JOptionPane.showInputDialog("请输入 ok"); if (answer !=null && answer.equals("ok")) { JOptionPane.showMessageDialog(f,"目标执行成功"); f.dispose(); } else { JOptionPane.showMessageDialog(f,"目标执行失败"); f.dispose(); } } else { System.exit(0); } } }
class 文本框{ public 文本框() { JFrame f = new JFrame("文本框"); f.setSize(400, 300); f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel lName = new JLabel("账号:"); JTextField tfName = new JTextField(""); tfName.setText("请输入账号"); tfName.setPreferredSize(new Dimension(80, 30));
JLabel lPassword = new JLabel("密码:"); JTextField tfPassword = new JTextField(""); tfPassword.setText("请输入密码"); tfPassword.setPreferredSize(new Dimension(80, 30));
f.add(lName); f.add(tfName); f.add(lPassword); f.add(tfPassword);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
tfPassword.grabFocus(); } }
class 密码框{
public 密码框() { JFrame f = new JFrame("密码框"); f.setSize(200, 100); f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel l = new JLabel("密码:"); JPasswordField pf = new JPasswordField(""); pf.setText("&48kdh4@#"); pf.setPreferredSize(new Dimension(80, 30));
char[] password = pf.getPassword(); String p = String.valueOf(password); System.out.println(p);
f.add(l); f.add(pf);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); } }
class 文本域{
public 文本域() { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(200, 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); } }
class 进度条{ public 进度条() { JFrame f = new JFrame("进度条"); f.setSize(400, 300); f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JProgressBar pb = new JProgressBar(); pb.setMaximum(100); pb.setValue(50); pb.setStringPainted(true);
f.add(pb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); } }
class 文件选择器{ public 文件选择器() { JFrame f = new JFrame("文件选择器"); f.setLayout(new FlowLayout());
JFileChooser fc = new JFileChooser(); fc.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".txt"); }
@Override public String getDescription() { return ".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 open = fc.showOpenDialog(f); File file = fc.getSelectedFile(); if (open == JFileChooser.APPROVE_OPTION) { JOptionPane.showMessageDialog(f,"准备打开"+file.getAbsolutePath()); } } }); bSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int open = fc.showSaveDialog(f); File file = fc.getSelectedFile(); if (open == JFileChooser.APPROVE_OPTION) { JOptionPane.showMessageDialog(f,"准备保存"+file.getAbsolutePath()); } } }); } }
|