GUI的核心技术:Swing AWT
GUI缺点:
1. 界面不美观 2. 需要jre环境
为什么要学习GUI?
import java.awt.*; //GUI的第一个界面 public class TestFrame { public static void main(String[] args) { //Frame窗口 Frame frame = new Frame("一起来飞车2,等你来战"); //设置窗口可见性 setVisible默认是false frame.setVisible(true); //设置窗口大小 frame.setSize(400,400); //设置窗口背景颜色 Color属性 frame.setBackground(new Color(144, 41, 39)); //设置窗口位置 frame.setLocation(12,24); //设置窗口大小不变 setResizable默认是true frame.setResizable(false); } }
运行效果:
问题:运行的窗口无法关闭,只能通过关闭java运行机制。
import java.awt.*; public class TestFrame2 { public static void main(String[] args) { //展示多个窗口 MyFrame myFrame1 = new MyFrame(100,100,300,300,Color.BLUE); MyFrame myFrame2 = new MyFrame(400,100,300,300,Color.yellow); MyFrame myFrame3 = new MyFrame(100,400,300,300,Color.red); MyFrame myFrame4 = new MyFrame(400,400,300,300,Color.black); } } class MyFrame extends Frame{ static int id=0;//可能存在多个窗口,就需要一个计数器解决 public MyFrame(int x,int y,int w,int h,Color color){ super("MyFrame+"+(++id)); setBackground(color); setBounds(x,y,w,h); setVisible(true); } }
运行效果如下:
解决了关闭事件的问题:
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //panel 面板 不能单独存在 public class TestPanel { public static void main(String[] args) { //窗口 Frame frame = new Frame(); //面板--布局 Panel panel = new Panel(); //布局 默认为null frame.setLayout(null); //设置坐标 frame.setBounds(300,300,500,500); //设置颜色 frame.setBackground(new Color(148, 172, 219)); //panel设置 相对于frame panel.setBounds(50,50,400,400); panel.setBackground(new Color(220, 42, 91)); //添加 frame.add(panel); //设置窗口可见 frame.setVisible(true); //设置监听事件 监听窗口结束事件----->addWindowListener //适配器模式: frame.addWindowListener(new WindowAdapter() { /** * Invoked when a window is in the process of being closed. * The close operation can be overridden at this point. * * @param e */ @Override public void windowClosing(WindowEvent e) {//窗口关闭 // super.windowClosing(e); System.exit(0); } }); } }
运行效果:
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //布局 public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //定义组件--->按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置大小 frame.setSize(500,500); //设置布局 流式布局 frame.setLayout(new FlowLayout());//居中 默认 //frame.setLayout(new FlowLayout(FlowLayout.LEFT));//左边 //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//右边 //添加组件-->按钮 frame.add(button1); frame.add(button2); frame.add(button3); //设置窗口可见 frame.setVisible(true); //关闭 frame.addWindowListener(new WindowAdapter() { /** * Invoked when a window is in the process of being closed. * The close operation can be overridden at this point. * * @param e */ @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame(); Button east = new Button("East"); Button west = new Button("West"); Button south = new Button("South"); Button north = new Button("North"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setSize(500,500); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { /** * Invoked when a window is in the process of being closed. * The close operation can be overridden at this point. * * @param e */ @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame(); Button b1 = new Button("b1"); Button b2 = new Button("b2"); Button b3 = new Button("b3"); Button b4 = new Button("b4"); Button b5 = new Button("b5"); Button b6 = new Button("b6"); frame.setLayout(new GridLayout(2,3)); frame.add(b1); frame.add(b2); frame.add(b3); frame.add(b4); frame.add(b5); frame.add(b6); frame.pack();//Java函数,可以自定义布局 frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
实践操作:
代码:
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Test { public static void main(String[] args) { //总 Frame frame = new Frame(); frame.setSize(400,300); frame.setLocation(300,400); frame.setBackground(new Color(220, 42, 91)); frame.setVisible(true); frame.setLayout(new GridLayout(2,1));//表示全局观察 分为两个部分 //面板 Panel panel1 = new Panel(new BorderLayout()); Panel panel2 = new Panel(new GridLayout(2,1)); Panel panel3 = new Panel(new BorderLayout()); Panel panel4 = new Panel(new GridLayout(2,2)); //上面部分 //两边 panel1.add(new Button("east-01"),BorderLayout.EAST); panel1.add(new Button("west-01"),BorderLayout.WEST); //中间 panel2.add(new Button("panel2-btu-01")); panel2.add(new Button("panel2-btu-02")); panel1.add(panel2,BorderLayout.CENTER); //下面部分 //两边 panel3.add(new Button("east-02"),BorderLayout.EAST); panel3.add(new Button("west-02"),BorderLayout.WEST); //中间部分面板 for (int i = 1; i <=4 ; i++) { panel4.add(new Button("panel4-btu-0"+i)); } panel3.add(panel4,BorderLayout.CENTER); //窗口添加面板 frame.add(panel1); frame.add(panel3); //关闭窗口 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
运行效果:
总结:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestActionEvent { public static void main(String[] args) { //定义按钮 触发事件 Frame frame = new Frame(); Button button = new Button(); MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); frame.add(button,BorderLayout.CENTER); frame.pack();//自定义填充界面 windowClose(frame);//关闭监听 frame.setVisible(true); } //提取出来 监听结束事件 private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { System.out.println("郭牛逼"); } }
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestTextField { public static void main(String[] args) { //文本框 main方法应该只需要有启动功能即可 MyFrame myFrame = new MyFrame(); windowClose(myFrame); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyFrame extends Frame{ public MyFrame(){ TextField textField = new TextField(); add(textField); //监听这个文本框输入的文字 MyActionListener2 myActionListener2 = new MyActionListener2(); //按下enter键 就会触发输入框的事件 textField.addActionListener(myActionListener2); //设置替换编码 把输入框中的文字替换 textField.setEchoChar('#'); setBounds(200,400,500,500); setVisible(true); //pack(); } } class MyActionListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { TextField field= (TextField) e.getSource();//获取一些资源 返回一个对象 System.out.println(field.getText());//获得文本框中的内容,输出在控制台上 field.setText("");//不能是null 只可以使 "" } }
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //简易计算器 public class TestCalc { public static void main(String[] args) { Calculator calculator = new Calculator(); windowClose(calculator); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } //计算器类 class Calculator extends Frame{ public Calculator(){ TextField textField1 = new TextField(10);//加数 TextField textField2 = new TextField(10);//被加数 TextField textField3 = new TextField(20);//和 Button button = new Button("="); button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3)); Label label = new Label("+"); //布局 setLayout(new FlowLayout()); add(textField1); add(label); add(textField2); add(button); add(textField3); pack(); setVisible(true); } } //监听事件 class MyCalculatorListener implements ActionListener{ //获取三个变量 private TextField textField1,textField2,textField3; public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3){ this.textField1=textField1; this.textField2=textField2; this.textField3=textField3; } @Override public void actionPerformed(ActionEvent e) { //获取数值 int parseInt1 = Integer.parseInt(textField1.getText()); int parseInt2 = Integer.parseInt(textField2.getText()); textField3.setText(""+(parseInt1+parseInt2));//计算结果 textField1.setText("");//清除 textField2.setText(""); } }
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //简易计算器 public class TestCalc { public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.loadFrame(); windowClose(calculator); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } //计算器类 class Calculator extends Frame { //属性 TextField textField1, textField2, textField3; //方法 public void loadFrame() { textField1 = new TextField(10);//加数 textField2 = new TextField(10);//被加数 textField3 = new TextField(20);//和 Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener(this)); //布局 setLayout(new FlowLayout()); add(textField1); add(label); add(textField2); add(button); add(textField3); pack(); setVisible(true); } } //监听事件 class MyCalculatorListener implements ActionListener{ //获取计算器对象 在一个类中组合另一个类 Calculator calculator=null; public MyCalculatorListener(Calculator calculator){ this.calculator=calculator; } @Override public void actionPerformed(ActionEvent e) { //获取数值 int parseInt1 = Integer.parseInt(calculator.textField1.getText()); int parseInt2 = Integer.parseInt(calculator.textField2.getText()); calculator.textField3.setText(""+(parseInt1+parseInt2));//计算结果 calculator.textField1.setText("");//清除 calculator.textField2.setText(""); } }
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //简易计算器 public class TestCalc { public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.loadFrame(); windowClose(calculator); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } //计算器类 class Calculator extends Frame { //属性 TextField textField1, textField2, textField3; //方法 public void loadFrame() { textField1 = new TextField(10);//加数 textField2 = new TextField(10);//被加数 textField3 = new TextField(20);//和 Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener()); //布局 setLayout(new FlowLayout()); add(textField1); add(label); add(textField2); add(button); add(textField3); pack(); setVisible(true); } //监听事件 private class MyCalculatorListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //获取数值 int parseInt1 = Integer.parseInt(textField1.getText()); int parseInt2 = Integer.parseInt(textField2.getText()); textField3.setText(""+(parseInt1+parseInt2));//计算结果 textField1.setText("");//清除 textField2.setText(""); } } }
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //画笔 public class TestPaint { public static void main(String[] args) { MyPaint myPaint = new MyPaint(); myPaint.loadFrame(); windowClose(myPaint); } //监听事件 关闭 private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyPaint extends Frame{ //窗口 public void loadFrame(){ setBounds(200,200,600,500); setVisible(true); } //画笔 @Override public void paint(Graphics g) { //super.paint(g); //首先画笔要有颜色,其次要有画画 注意:每次结束画笔之后需要将图画还原为原来的颜色 //g.setColor(Color.yellow); g.fillOval(100,100,100,100); //g.setColor(Color.ORANGE); g.fillRect(200,200,300,200); } }
思路如下图:
代码如下:
import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.Iterator; public class TestMouseListener { public static void main(String[] args) { MyFrame myFrame = new MyFrame("画图"); windowClose(myFrame); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } //定义自己的类 class MyFrame extends Frame{ //定义鼠标点数量的属性 ArrayList points; public MyFrame(String string){ super(string); setBounds(200,200,400,300); //存放鼠标的 "点" points = new ArrayList<>(); setVisible(true); //鼠标监听器 对应窗口 this.addMouseListener(new MyMouseListener()); } //画图 @Override public void paint(Graphics g) { //画画 设置鼠标监听事件 Iterator iterator = points.iterator(); while (iterator.hasNext()){ Point point = (Point) iterator.next(); g.setColor(Color.pink); g.fillOval(point.x,point.y,10,10); } } //添加鼠标点 public void addPoint(Point point){ points.add(point); } //适配器模式 private class MyMouseListener extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { MyFrame frame= (MyFrame) e.getSource();//获取鼠标点的信息 frame.addPoint(new Point(e.getX(),e.getY())); //获取鼠标点的当前位置 frame.repaint();//刷新 每一次点击鼠标就会刷新 } } }
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestWindow { public static void main(String[] args) { new WindowFrame(); } } class WindowFrame extends Frame{ public WindowFrame(){ setBounds(100,200,400,300); setBackground(Color.BLUE); setVisible(true); // addWindowListener(new MyWindowListener()); this.addWindowListener(new WindowAdapter() { //匿名内部类 @Override public void windowClosing(WindowEvent e) { // super.windowClosing(e); System.out.println("关闭按钮"); System.exit(0); } @Override public void windowActivated(WindowEvent e) { // super.windowActivated(e); WindowFrame source = (WindowFrame) e.getSource(); source.setTitle("激活成功"); System.out.println("激活按钮"); } }); } // class MyWindowListener extends WindowAdapter{ // @Override // public void windowClosing(WindowEvent e) { // setVisible(false);//通过点击按钮,隐藏窗口 // System.exit(0); // } // } }
import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestKeyListener { public static void main(String[] args) { KeyFrame keyFrame = new KeyFrame(); windowClose(keyFrame); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class KeyFrame extends Frame{ public KeyFrame(){ setBounds(10,10,200,200); setVisible(true); addKeyListener(new KeyAdapter() { //按下键盘上的不同按钮机会产生不同的结果 @Override public void keyPressed(KeyEvent e) { //首先要获取当前键盘要按下的键的值(ASCII码) int keyCode = e.getKeyCode(); System.out.println(keyCode);//输出的是ASCII码值 if (keyCode==KeyEvent.VK_DOWN){ System.out.println("你按下了下贱"); } } }); } }
import javax.swing.*; import java.awt.*; public class JFrameDemo { public void init(){ JFrame jFrame = new JFrame(); jFrame.setBounds(100,100,200,300); jFrame.setVisible(true); jFrame.setBackground(Color.yellow); JLabel jLabel = new JLabel("欢迎光临Grul"); jFrame.add(jLabel); //关闭窗口 jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JFrameDemo().init(); } }
标签居中:
import javax.swing.*; import java.awt.*; public class JFrameDemo2 { public static void main(String[] args) { MyJFrame myJFrame = new MyJFrame(); myJFrame.init(); myJFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } } class MyJFrame extends JFrame{ public void init(){ setVisible(true); setBounds(12,12,200,300); //设置一个标签 JLabel jLabel = new JLabel("欢迎光临!"); add(jLabel);//窗口添加 //设置文本内容位置 居中对齐 jLabel.setHorizontalAlignment(SwingConstants.CENTER); //获得一个容器 Container container = this.getContentPane(); container.setBackground(Color.YELLOW);//设置容器颜色 } }
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; //主窗口 public class DialogDemo extends JFrame { public DialogDemo() { setVisible(true); setBounds(10,10,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置容器 存放东西 Container container = this.getContentPane(); //绝对布局 container.setLayout(null); //创建一个弹窗按钮 JButton jButton = new JButton("这是一个弹窗按钮,你试试看"); jButton.setBounds(30,30,400,50); //点击按钮 弹出弹窗 jButton.addActionListener(new ActionListener() {//监听器 @Override public void actionPerformed(ActionEvent e) { //调用弹窗 new MyDialogDemo(); } }); container.add(jButton); } public static void main(String[] args) { new DialogDemo(); } } //设置一个弹窗的窗口 class MyDialogDemo extends JDialog{ public MyDialogDemo() { setVisible(true); //setBackground(Color.yellow); setBounds(100,100,200,200); Container container = this.getContentPane(); container.setLayout(null); //container.add(new Label("老子带你学道德经")); } }
label标签
图标 ICON
import javax.swing.*; import java.awt.*; //图标 是一个接口需要实现类 Frame来继承 public class IconDemo extends JFrame implements Icon { private int width; private int height; public IconDemo(){}//无参构造 public IconDemo(int width,int height){ this.width=width; this.height=height; } public void init(){ IconDemo iconDemo = new IconDemo(20,20); //把图标放在标签上 也可以放在按钮上 格式:自己命名+代码名称+位置 JLabel label = new JLabel("肖像",iconDemo,SwingConstants.CENTER); Container container = this.getContentPane(); container.add(label); setVisible(true); setBounds(100,100,500,400); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new IconDemo().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
import javax.swing.*; import java.awt.*; public class ImageDemo1 extends JFrame { public ImageDemo1(){ JLabel label = new JLabel("小事情"); //获取图片的地址 ImageIcon imageIcon = new ImageIcon("src/com/ggl/lesson04/ye.jpg"); label.setIcon(imageIcon); label.setHorizontalAlignment(SwingConstants.CENTER); Container container = getContentPane(); container.add(label); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ImageDemo1(); } }
JPanel
import javax.swing.*; import java.awt.*; public class JPanelDemo extends JFrame { public JPanelDemo() { Container container = this.getContentPane(); container.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")); container.add(panel); this.setVisible(true); this.setBounds(100,100,500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JPanelDemo(); } }
JScrollPanel
import javax.swing.*; import java.awt.*; public class JScrollDemo extends JFrame { public JScrollDemo() { Container container = this.getContentPane(); //文本域 JTextArea textArea = new JTextArea(10,20); textArea.setText("欢迎光临"); //Scroll 面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); setVisible(true); setBounds(100,100,200,300); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); } }
图片按钮
import javax.swing.*; import java.awt.*; public class JButtonDemo01 extends JFrame { public JButtonDemo01() { Container container = this.getContentPane(); //将图片变成图标 Icon imageIcon = new ImageIcon("src/com/ggl/lesson05/ye.jpg"); JButton button = new JButton(); button.setIcon(imageIcon); button.setToolTipText("图片按钮"); container.add(button); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo01(); } }
单选按钮
import javax.swing.*; import java.awt.*; public class JButtonDemo02 extends JFrame { public JButtonDemo02() { Container container = this.getContentPane(); //单选框 JRadioButton radioButton1 = new JRadioButton("迪迦"); JRadioButton radioButton2 = new JRadioButton("泰罗"); JRadioButton radioButton3 = new JRadioButton("欧布"); //单选框进行分组才能在选择时是单项选择 分组之后,一组之中只能选一个 ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(radioButton1); buttonGroup.add(radioButton2); buttonGroup.add(radioButton3); //添加到容器中 container.add(radioButton1,BorderLayout.CENTER); container.add(radioButton2,BorderLayout.SOUTH); container.add(radioButton3,BorderLayout.NORTH); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //pack(); } public static void main(String[] args) { new JButtonDemo02(); } }
复选按钮
import javax.swing.*; import java.awt.*; public class JButtonDemo03 extends JFrame { public JButtonDemo03() { Container container = this.getContentPane(); //多选框 JCheckBox checkBox1 = new JCheckBox("假面骑士空我"); JCheckBox checkBox2 = new JCheckBox("假面骑士电王"); JCheckBox checkBox3 = new JCheckBox("假面骑士零一"); container.add(checkBox1,BorderLayout.CENTER); container.add(checkBox2,BorderLayout.NORTH); container.add(checkBox3,BorderLayout.SOUTH); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //pack(); } public static void main(String[] args) { new JButtonDemo03(); } }
下拉框
import javax.swing.*; import java.awt.*; public class ComboBoxDemo01 extends JFrame { public ComboBoxDemo01() { Container container = this.getContentPane(); //下拉框 JComboBox JComboBox comboBox = new JComboBox(); comboBox.addItem(null); comboBox.addItem("热映中"); comboBox.addItem("已停播"); comboBox.addItem("已禁播"); container.add(comboBox); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ComboBoxDemo01(); } }
import javax.swing.*; import java.awt.*; import java.util.Vector; public class ComboBoxDemo02 extends JFrame { public ComboBoxDemo02() { Container container = this.getContentPane(); //生成列表中的内容 数组类型 静态添加 //String[] contents={"1","2","3"}; //不常用 Vector contents = new Vector(); //动态添加 contents.add("迪迦"); contents.add("泰迦"); contents.add("泰罗"); contents.add("赛文"); contents.add("泽塔"); //创建一个列表存放数据 JList jList = new JList(contents); container.add(jList); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ComboBoxDemo02(); } }
import javax.swing.*; import java.awt.*; import java.util.Vector; public class TextDemo01 extends JFrame { public TextDemo01() { Container container = this.getContentPane(); JTextField textField1 = new JTextField("hello"); JTextField textField2 = new JTextField("world"); container.add(textField1,BorderLayout.SOUTH); container.add(textField2,BorderLayout.NORTH); // container.setLayout(null); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextDemo01(); } }
import javax.swing.*; import java.awt.*; public class TextDemo02 extends JFrame { public TextDemo02() { Container container = this.getContentPane(); //面板 密码框 JPasswordField passwordField = new JPasswordField(); passwordField.setEchoChar('*');//设置密码不可见 container.add(passwordField); setVisible(true); setBounds(100,100,700,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextDemo02(); } }
import javax.swing.*; import java.awt.*; public class JScrollDemo extends JFrame { public JScrollDemo() { Container container = this.getContentPane(); //文本域 JTextArea textArea = new JTextArea(10,20); textArea.setText("欢迎光临"); //Scroll 面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); setVisible(true); setBounds(100,100,200,300); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); } }