本文共 9881 字,大约阅读时间需要 32 分钟。
//标签居中class Myframe2 extends JFrame{ public void init(){ this.setBounds(10,10,200,300); this.setVisible(true); JLabel lable=new JLabel("欢迎来到狂神"); this.add(lable); //让文本标签居中,设置水平对齐 lable.setHorizontalAlignment(SwingConstants.CENTER); //获得一个容器 Container container=this.getContentPane(); container.setBackground(Color.yellow); }}
//初始化public class javaPractice { //init();初始化 public void init(){ //顶级窗口 JFrame jf=new JFrame("这是一个JFrame窗口"); jf.setBounds(10,10,200,300); jf.setVisible(true); jf.setBackground(Color.blue); JLabel lable=new JLabel("欢迎来到狂神"); jf.add(lable); //关闭事件 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { //建立一个窗口 new javaPractice().init(); }}
public class javaPractice extends JFrame { public javaPractice(){ this.setVisible(true); this.setBounds(100,100,700,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //JFrame 放东西 容器 Container container=this.getContentPane(); //绝对布局 container.setLayout(null); //按钮 JButton button=new JButton("点击弹出一个对话框"); button.setBounds(30,30,200,50); //点击这个按钮的时候,弹出一个窗口 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //弹窗 new MyDialog(); } }); container.add(button); } public static void main(String[] args) { new javaPractice(); }}class MyDialog extends JDialog{ public MyDialog() { this.setVisible(true); this.setBounds(200,200,400,400); //默认就有this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container container=this.getContentPane(); container.setLayout(null); container.add(new Label("学习java")); }}
label
new TLabel("xxx")
图标 icon
//图标、需要实现的类,Frame继承public class javaPractice extends JFrame implements Icon { private int width; private int height; public javaPractice() { //无参构造 } public javaPractice(int width,int height) { this.width=width; this.height=height; } public void init(){ javaPractice iconDemo=new javaPractice(15,15); //图标放在标签上,也可以放在按钮上! JLabel label =new JLabel("icontest",iconDemo,SwingConstants.CENTER); Container container=getContentPane(); container.add(label); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice().init(); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,width,height); } @Override public int getIconWidth() { return this.width; } @Override public int getIconHeight() { return this.height; }}
图片 icon
public class javaPractice extends JFrame { public javaPractice(){ //获取图片的地址 JLabel label=new JLabel("Imageicon"); URL url=javaPractice.class.getResource("tx.jpg"); ImageIcon imageIcon=new ImageIcon(url); label.setIcon(imageIcon); label.setHorizontalAlignment(SwingConstants.CENTER); Container container=getContentPane(); container.add(label); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,200,200); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); container.setLayout(new GridLayout(2,1,10,10)); JPanel panel1=new JPanel(new GridLayout(1,3)); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel1.add(new JButton("1")); container.add(panel1); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //文本域 JTextArea textArea=new JTextArea(20,50); textArea.setText("欢迎学习狂神说java"); //Scroll面板 JScrollPane scrollPane=new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //将一个图片变为图标 URL resource=javaPractice.class.getResource("tx.jpg"); Icon icon=new ImageIcon(resource); //把这个图标放在按钮上 JButton button=new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); container.add(button); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //将一个图片变为图标 URL resource=javaPractice.class.getResource("tx.jpg"); Icon icon=new ImageIcon(resource); //单选框 JRadioButton radioButton1=new JRadioButton("button1"); JRadioButton radioButton2=new JRadioButton("button2"); JRadioButton radioButton3=new JRadioButton("button3"); //单选只能选择一个,所以一个组中只能选择一个 container.add(radioButton1,BorderLayout.CENTER); container.add(radioButton2,BorderLayout.NORTH); container.add(radioButton3,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //多选框 JCheckBox checkBox01=new JCheckBox("checkBox01"); JCheckBox checkBox02=new JCheckBox("checkBox02"); container.add(checkBox01,BorderLayout.NORTH); container.add(checkBox02,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPracti ce(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); JComboBox status=new JComboBox(); status.addItem(null); status.addItem("正在热映"); status.addItem("已下架"); status.addItem("即将上映"); container.add(status); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //String[] content={"1","2","3"}; Vector contents=new Vector(); //列表中需要加入内容 JList jList=new JList(contents); contents.add("zhangsan"); contents.add("lisi"); contents.add("wangwu"); container.add(jList); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); JTextField textField=new JTextField("hello"); JTextField textField0=new JTextField("word",20); container.add(textField,BorderLayout.SOUTH); container.add(textField0,BorderLayout.NORTH); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //画板 JPasswordField passwordField=new JPasswordField(); passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //文本域 JTextArea textArea=new JTextArea(20,50); textArea.setText("欢迎学习狂神说java"); //Scroll面板 JScrollPane scrollPane=new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); }}
转载地址:http://stgji.baihongyu.com/