总结-swing、JFrame、JScrollPane、JTabbedPane、JEditorPane

时间:2023-03-08 22:42:16

总结-swing、JFrame、JButton、JScrollPane、JLabel、JTabbedPane、JEditorPane

1、JButton内边距(去掉按钮里的空白):setMargin
2、JLabel背景不透明:setOpaque
3、JLabel/JTextField内容居中:setHorizontalAlignment
4、字体:new Font("微软雅黑",Font.BOLD,16);
5、设置JFrame屏幕居中:先setSize,再setLocationRelativeTo(null);
6、鼠标指针(手型):setCursor(new Cursor(Cursor.HAND_CURSOR));
7、JFrame去掉装饰:setUndecorated
8、添加图片:Image img = ImageIO.read(new File("src/image/llj.jpg"));
   g.drawImage(img, left, top, width, height, fm);
   重写paint:new ImageIcon("src/image/llj.jpg").paintIcon(fm, g, x, y);
   fm.getGraphics()与fm.getContentPane().getGraphics()是有差别的
9、pane.setPreferredSize(new Dimension(0, top(0)));// 宽度设置为0时默认填满宽度
   JSrollPane:new JSrollPane(pane);
   pane要setPreferredSize才能设置在JSrollPane里的大小。长度(宽度)动态设置效果更好
10、JTabbedPane:JTabbedPane tp = new JTabbedPane(JTabbedPane.TOP);// 标题栏在上面
   tp.add("标题",new JPanel());
     JTabbedPane页签改变事件监听:addChangeListener;获得当前选定页签:getSelectedComponent
11、用JTextArea生成一个JScrollPane,就成了带滚动条的文本区域。
   JTextArea:setLineWrap(true);//自动换行
       setWrapStyleWord(true); //换行不断字
       JPanel里设置layout为GridLayout()或BorderLayout会默认填满
12、JEditorPane,HTML-Hyperlink,设置setEditorable(false);才有点击超链接的效果
   new JEditorPane("text/html",String html);
   HyperlinkListener
   e.getEventType() == HyperlinkEvent.EventType.ACTIVATED

补充:

1.重写paint方法时,先super.paint(g); 要不会出问题的。  比如设置背景色失败。。。

原创声明: 随意转载,不必注明出处,分享至上 [博客园 廖龙军 http://cnblogs.com/liaolongjun]