4.imageIcon (标签_图片)
基本思路: 1.获取地址: 类名.class.getResource(); //获取地址 2.创建imageIcon : ImageIcon imageIcon=new ImageIcon(url); 3.设置成标签: 标签对象.setIcon(); 4.水平居中: 标签对象.setHorizontalAlignment(SwingConstants.CENTER);
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; public class first extends JFrame { public first(){ JLabel label_one=new JLabel("吉士先生背景图片"); //获取图片地址; URL url = first.class.getResource("imag.jpg"); //从指定的URL创建一个ImageIcon。 ImageIcon imageIcon=new ImageIcon(url); //把图片设置成标签 label_one.setIcon(imageIcon); //标签水平线居中 label_one.setHorizontalAlignment(SwingConstants.CENTER); //把标签添加到窗体中去 this.add(label_one); //进行窗体的设置效果 this.setSize(400,400); this.setVisible(true); this.setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
5.Container (容器)
基本思路: 1.返回此框架的 contentPane对象。 Container container=this.getContentPane(); 2.设置间距: container.setLayout(new GridLayout(2,1,10,10));
package KuangStudy; import javax.swing.*; import java.awt.*; public class first extends JFrame { public first(){ Container container=this.getContentPane();//框体的容器 container.setLayout(new GridLayout(2,1,10,10)); JPanel jPanel_one=new JPanel(new GridLayout(1,3)); JPanel jPanel_two=new JPanel(new GridLayout(1,2)); JPanel jPanel_three=new JPanel(new GridLayout(2,3)); JPanel jPanel_four=new JPanel(new GridLayout(3,2)); jPanel_one.add(new JButton("1")); jPanel_one.add(new JButton("1")); jPanel_one.add(new JButton("1")); jPanel_two.add(new JButton("1")); jPanel_two.add(new JButton("1")); jPanel_three.add(new JButton("1")); jPanel_three.add(new JButton("1")); jPanel_three.add(new JButton("1")); jPanel_three.add(new JButton("1")); jPanel_three.add(new JButton("1")); jPanel_three.add(new JButton("1")); jPanel_four.add(new JButton("1")); jPanel_four.add(new JButton("1")); jPanel_four.add(new JButton("1")); jPanel_four.add(new JButton("1")); jPanel_four.add(new JButton("1")); jPanel_four.add(new JButton("1")); container.add(jPanel_one); container.add(jPanel_two); container.add(jPanel_three); container.add(jPanel_four); setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
6.ScorllPanel (滑动块_记事本)
基本思路: 1.文本域添加到滑动块中去 JScrollPane jScrollPane_one=new JScrollPane(textArea_one); 2.滑动块添加到画板中去
package KuangStudy; import javax.swing.*; import java.awt.*; public class first extends JFrame { public first(){ Container container=this.getContentPane();//框体的容器 //文本域 TextArea textArea_one=new TextArea(20,30); //设置默认的文字 textArea_one.setText("欢迎来到我的世界! "); //把文本域添加到滚动块中去 JScrollPane jScrollPane_one=new JScrollPane(textArea_one); //把滚动块添加到窗体中去 container.add(jScrollPane_one); setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
7.ICON (按钮_图标)
基本思路: 1.导入图片 地址url 2.利用url生成图标 3.按钮设置成图标: button.setIcon(imag); 4.按钮添加到窗体.
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); URL url = first.class.getResource("imag.jpg"); //url导入变成ICon ImageIcon Icon_one=new ImageIcon(url); JButton button=new JButton(); //把这个图标放在按钮上 button.setIcon(Icon_one); //显示图片标题 button.setToolTipText("图片按钮"); container.add(button); setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
·
8.JRadioButton (单选按钮)
基本思路: 1.单选框: JRadioButton 2.分组: ButtonGroup 3.把单选按钮添加到分组中,进行分组 buttonGroup_one.add(jRadioButton_one); 4.单选按钮添加到窗体
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); //单选框 JRadioButton jRadioButton_one=new JRadioButton("A 你是大傻瓜"); JRadioButton jRadioButton_two=new JRadioButton("B 我是大傻瓜"); JRadioButton jRadioButton_three=new JRadioButton("C 我俩都是大傻瓜"); //由于单选框只能选择一个,所以我们要对其分组 ButtonGroup buttonGroup_one=new ButtonGroup(); //单选按钮分组添加 buttonGroup_one.add(jRadioButton_one); buttonGroup_one.add(jRadioButton_two); buttonGroup_one.add(jRadioButton_three); //添加到窗体中 container.add(jRadioButton_one,BorderLayout.NORTH); container.add(jRadioButton_two,BorderLayout.CENTER); container.add(jRadioButton_three,BorderLayout.SOUTH); setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }