第二十七次学习Java
1、
package demo01;
import java.awt.Button;
import java.awt.Container;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class DialogDemo01 extends JDialog{
public DialogDemo01(JFrame jframe){
super(jframe,false);
this.setBounds(100, 100, 500, 500);
Container container1= this.getContentPane();
container1.setLayout(null);
container1.add(new Label("大数据五班"));
JButton button1= new JButton("我要五班");
button1.setBounds(30, 30, 200, 50);
container1.add(button1);
}
public static void main(String[] args) {
JFrame j=new JFrame();
j.setVisible(true);
j.setBounds(100, 100, 700, 500);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container= j.getContentPane();
container.setLayout(null);
JButton button= new JButton("我要登录");
button.setBounds(30, 30, 200, 50);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
DialogDemo01 s=new DialogDemo01(j);
s.setVisible(true);
System.out.println("我被点击了");
}
});
container.add(button);
}
}
2、
package Demo02;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JPaneDemo extends JFrame{
public JPaneDemo(){
Container container1= this.getContentPane();
container1.setLayout(new GridLayout(2,1,10,10));
JPanel panel= new JPanel(new GridLayout(1,3));
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
container1.add(panel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
}
public static void main(String[] args) {
new JPaneDemo();
}
}
3、
package Demo02;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JScrollDemo extends JFrame{
public JScrollDemo(){
Container container1= this.getContentPane();
JTextArea textarea= new JTextArea(20,50);
textarea.setText("大数据五班");
JScrollPane scrollpane= new JScrollPane(textarea);
//JPanel scrollpane =new JPanel();
//scrollpane.add(textarea);
container1.add(scrollpane);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
4、
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class TestFlowLayout2 {
public static void main(String[] args) {
JFrame j=new JFrame();
j.setVisible(true);
j.setBounds(100, 100, 700, 500);
j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//j.setLayout(new FlowLayout());
//j.setLayout(new FlowLayout(FlowLayout.RIGHT));
j.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton button1= new JButton("button1");
JButton button2= new JButton("button2");
JButton button3= new JButton("button3");
JButton button4= new JButton("button4");
JButton button5= new JButton("button5");
JButton button6= new JButton("button6");
j.add(button1);
j.add(button2);
j.add(button3);
j.add(button4);
j.add(button5);
j.add(button6);
}
}
5、
package Demo03;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class TestBorderLayout {
public static void main(String[] args) {
JFrame j=new JFrame();
j.setVisible(true);
j.setBounds(100, 100, 700, 500);
j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton east= new JButton("east");
JButton west= new JButton("west");
JButton south= new JButton("south");
JButton north= new JButton("north");
JButton center= new JButton("center");
j.add(east,BorderLayout.EAST);
j.add(west,BorderLayout.WEST);
j.add(south,BorderLayout.SOUTH);
j.add(north,BorderLayout.NORTH);
j.add(center,BorderLayout.CENTER);
}
}
6、
package Demo03;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class TestGrid {
public static void main(String[] args) {
JFrame j=new JFrame();
j.setVisible(true);
j.setBounds(100, 100, 700, 500);
j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
j.setLayout(new GridLayout(3,2));
JButton button1= new JButton("button1");
button1.setSize(50,50);
JButton button2= new JButton("button2");
button2.setSize(50, 50);
JButton button3= new JButton("button3");
JButton button4= new JButton("button4");
JButton button5= new JButton("button5");
//JButton button6= new JButton("button6");
j.add(button1);
j.add(button2);
j.add(button3);
j.add(button4);
j.add(button5);
//j.add(button6);
//j.pack();
}
}
7、
package Demo04;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class CheckBox extends JFrame{
public CheckBox() {
Container container1= this.getContentPane();
JCheckBox checkbox1= new JCheckBox("自动登录");
JCheckBox checkbox2= new JCheckBox("记住密码");
JCheckBox checkbox3= new JCheckBox("checkbox3");
container1.add(checkbox1,BorderLayout.CENTER);
container1.add(checkbox2,BorderLayout.EAST);
container1.add(checkbox3,BorderLayout.WEST);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
}
public static void main(String[] args) {
new CheckBox();
}
}
8、
package Demo04;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class RadioButon extends JFrame{
public RadioButon() {
Container container1= this.getContentPane();
JRadioButton radiobutton1= new JRadioButton("单选1");
JRadioButton radiobutton2= new JRadioButton("JRadioButton3");
JRadioButton radiobutton3= new JRadioButton("JRadioButton3");
JRadioButton radiobutton4= new JRadioButton("JRadioButton4");
ButtonGroup group =new ButtonGroup();
group.add(radiobutton1);
group.add(radiobutton2);
group.add(radiobutton3);
container1.add(radiobutton1,BorderLayout.CENTER);
container1.add(radiobutton2,BorderLayout.NORTH);
container1.add(radiobutton3,BorderLayout.SOUTH);
container1.add(radiobutton4,BorderLayout.EAST);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
}
public static void main(String[] args) {
new RadioButon();
}
}
9、
package Demo05;
import java.awt.Container;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class TextComBoxDemo01 extends JFrame{
public TextComBoxDemo01() {
Container container1= this.getContentPane();
JComboBox status= new JComboBox();
status.addItem(null);
status.addItem("正在学习");
status.addItem("准备睡觉");
status.addItem("看电视");
container1.add(status);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
}
public static void main(String[] args) {
new TextComBoxDemo01();
}
}
10、
package Demo05;
import java.awt.Container;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
public class TextComBoxDemo2 extends JFrame{
public TextComBoxDemo2() {
Container container1= this.getContentPane();
Vector<Object> contents = new Vector();
JList jlist= new JList(contents);
contents.add("五班");
contents.add("爱学习");
contents.add("打游戏");
contents.add("兼得");
container1.add(jlist);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new TextComBoxDemo2();
}
}