frame 框架 Pane面板
frame.getContenPane()得到面板。
改变布局管理器:
JPanel background = new JPanel();
background.setLayout(new BoxLayout(background, BoxLayout.Y_AXIS));
frame.getContentPane().add(background);
frame.pack()函数让窗口自动调整为合适的size。
为事件源(含有方法add*****Listener()方法的类。如button)注册监听器(实现了*****Listener接口的类),这样在事件源被事件(***event)触发时,事件源将事件发送到监听器,让监听器的*****performed(****event e)方法处理。
创建JPanel的子类并覆盖掉paintComponent(Graphics g)这这个方法,如果用户缩小window或选择最小化,Java虚拟机也会知道要调用它来重新绘制(即调用paintComponent(Graphics g)),自己不要调用这个函数,想要重新绘制可以用repaint。
Graphics g是一个抽象类,实际上是个Graphics2Dd实例。
内部类可以使用外部所有的方法与变量,包括私有的。
使用外部类的实例来创建内部类的实例。
labelButton.addActionListener(new (LabelListener());
任何时候你需要一个独立却又好像另一个类成员之一的类时,内部类可能是唯一的解。如监听器。
两个不同的类表示两项不同的事物才是好的面向对象。面向对象代表重用与维护,显然内部类的重用性差。
存盘
JFileChooser fileSave = new FfileChooser();
fileSavve.showSaveDialog(frame);
saveFile(fileSave.getSelectedFile());
一组RadioButton,只能同时选中一个
一组CheckBox,能同时选中多个
JButton按下去释放的时候不想会马上恢复到原来的图像,JToggleButton按下去释放的时候图像不会变化,只有你再次点击按钮的时候才会变化
lblNewLabel.setIcon(new ImageIcon("C:\\Windows\\Web\\Wallpaper\\Windows\\img0.jpg"));
对话框
JOptionPane.showConfirmDialog(null,
"标题", "侧边信息", JOptionPane.YES_NO_OPTION, 0, ScreenCapture.Cap());
border 设置边框 bevelborder 凹凸
opaque 是否透明(Jlabel默认 透明)
设置皮肤:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
组件的TEXT可以输入html(利用html编辑器)
按钮添加图片:
String path="3.png";
Icon icon=new ImageIcon(path);
ClientButton clientbutton = new ClientButton(IP.split("\\.")[3],
Clientaccount,icon);
截屏并设置图片大小
public class ScreenCapture {
public static Icon Cap() {
Icon cap=null;
try {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage simage = robot.createScreenCapture(screenRectangle);
BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(simage, 0, 0, 1300, 700, null);
// image.setImage(image.getImage().getScaledInstance(300,300,Image.SCALE_DEFAULT));
cap = new ImageIcon(image);
} catch (Exception e) {
e.printStackTrace();
}
return cap;
}
}
添加窗口关闭事件
this.addWindowListener(new WindowAdapter() {
为Jtextarea添加滚动条
JScrollPane jScrollPane = new JScrollPane(textArea);
jScrollPane.setBounds(647, 92, 251, 330);
//contentPane.add(textArea);
contentPane.add(jScrollPane);
自动换行linewrap
在panel上添加或者删除按钮:
public void addbutton(ClientButton clientbutton){
this.panel.add(clientbutton);
this.panel.revalidate();
Overall.ClientButtons.put(clientbutton.account.getIp(),clientbutton);
}
public void removebutton(ClientButton clientbutton){
this.panel.remove(clientbutton);
this.panel.setVisible(false);
this.panel.setVisible(true);
}
}