Swing简单图形常识备忘
【摘要】 Swing设置窗口图标ImageIcon imageIcon = new ImageIcon(DocMessMainFrame.class.getResource("/icon.png"));frame.setIconImage(imageIcon.getImage());Swing固定窗口大小frame.setExtendedState(Frame.MAXIMIZED_BOTH);fram...
Swing设置窗口图标
ImageIcon imageIcon = new ImageIcon(DocMessMainFrame.class.getResource("/icon.png")); frame.setIconImage(imageIcon.getImage());
Swing固定窗口大小
frame.setExtendedState(Frame.MAXIMIZED_BOTH); frame.setPreferredSize(new Dimension(800, 550)); frame.setResizable(false);
Swing设置窗口关闭退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Swing窗口居中
frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
Swing窗口线程创建和显示UI
javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } });
Swing null布局
panel.setLayout(null) xxx.setBounds(10,20,80,25);
Swing GridBagLayout布局
JPanel panel = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag);
Swing文本框添加滚动
JTextArea tarea = new JTextArea(20, 30);
tarea.setEditable(false);
JScrollPane scroll = new JScrollPane(tarea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Swing文本框只读不灰显
JTextField outDir = new JTextField(OutDir); outDir.setEditable(false); inDir.setBackground(UIManager.getColor("TextField.background"));
Swing进度条结合SwingWorker
JProgressBar jpb = new JProgressBar(); buttonStart.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(!OutDir.isEmpty() && !OutDir.isEmpty() && new File(OutDir).exists() && new File(InDir).exists()) { buttonStart.setEnabled(false); tarea.setText(""); File inFiles = new File(InDir); int max = 1000; if(inFiles.exists() && inFiles.isDirectory()) { max = inFiles.list().length; }else { max = 1000; } jpb.setMaximum(max); new PWorker(InDir,OutDir,buttonStart,jpb,tarea,isAddCom.isSelected()).execute(); } } });
Swing文件夹选择
buttonIn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int option = fileChooser.showOpenDialog(frame); if(option == JFileChooser.APPROVE_OPTION){ File file = fileChooser.getSelectedFile(); String se = file.getAbsolutePath(); inDir.setText(se); InDir = se; try { if(ini!=null) { ini.put("set", "indir", se); ini.store(); } } catch (IOException e1) { e1.printStackTrace(); } } } });
GridBagLayout布局固定Cell大小
jpb.setPreferredSize(new Dimension(700, 40));
GridBagLayout布局设置边距
c.insets = new Insets(3,3,3,3);
GridBagLayout布局设置宽度为当前到行尾
c.gridwidth = GridBagConstraints.REMAINDER; panel.add(buttonOut); gridbag.setConstraints(buttonOut, c);
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)