美文网首页
Java图形界面

Java图形界面

作者: 滴答大 | 来源:发表于2018-10-19 10:37 被阅读10次

    1、计算器

    public class LayoutOutDemo02 extends JFrame {
    
    public LayoutOutDemo02(){
        JPanel panel1 = new JPanel();
        GridLayout layout1 = new GridLayout(4,3);
        panel1.setLayout(layout1);
    
        for (int i = 1; i <=9; i++) {
            panel1.add(new JButton(""+i));
       }
        panel1.add(new JButton("0"));
        panel1.add(new JButton("start"));
        panel1.add(new JButton("stop"));
    
    
        JPanel panel2 = new JPanel(new BorderLayout());
    
        panel2.add(new JTextField("请输入。。。"),BorderLayout.NORTH);
    
        panel2.add(panel1,BorderLayout.CENTER);
    
        add(panel2,BorderLayout.EAST);//右
    
        //左边加button
        panel2.add(new JButton("确认"),BorderLayout.WEST);
    
    }
    public static void main(String[] args) {
        LayoutOutDemo02 frame = new LayoutOutDemo02();
        frame.setSize(400,250);
        frame.setTitle("LayoutExciseDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    }
    

    效果:


    Snip20181019_7.png

    2、设置颜色(java.awt.Color)

        JButton leftButton = new JButton("确认");
        leftButton.setBackground(Color.blue);
        leftButton.setForeground(new Color(122,34,58));
        add(leftButton,BorderLayout.WEST);
    

    3、设置字体(java.awt.Font)

    构造方法:public Font(String name,int style,int size);

        Font font1 = new Font("Malayalam Sangam MN",Font.BOLD+Font.ITALIC,20);
        JLabel lable = new JLabel("12345");
        add(lable,BorderLayout.SOUTH);
        lable.setFont(font1);
    

    4、获取字体家族

         //获取字体数组
        GraphicsEnvironment envirment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String [] fontName = envirment.getAvailableFontFamilyNames();
        for (int i = 0; i < fontName.length; i++) {
            System.out.println(fontName[i]);
        }
    

    5、练习二

     public class TestSwingCommonFeatures extends JFrame {
    
    
    public TestSwingCommonFeatures(){
    
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
    
        JButton leftButton = new JButton("left");
        leftButton.setBackground(Color.white);
        JButton centerButton = new JButton("center");
        centerButton.setForeground(Color.GREEN);
        JButton rightButton = new JButton("right");
        //设置工具提示
        rightButton.setToolTipText("right button");
    
        panel.add(leftButton);
        panel.add(centerButton);
        panel.add(rightButton);
        //给panel添加文字边界
        panel.setBorder(new TitledBorder("ThreeButton"));
    
    
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,2,5,5));
    
        //定义边界线
        Border lineborder = new LineBorder(Color.BLACK,2);
    
        JLabel lable = new JLabel("red");
        lable.setFont(new Font("",Font.BOLD,22));
        lable.setBackground(Color.WHITE);
        lable.setForeground(Color.GREEN);
        lable.setBorder(lineborder);
    
        panel2.add(lable);
    
    
    
        JLabel lable2 = new JLabel("red");
        lable2.setFont(new Font("",Font.BOLD,22));
        lable2.setBackground(Color.WHITE);
        lable2.setForeground(Color.blue);
        lable2.setBorder(lineborder);
        panel2.add(lable2);
    
        //给Panel2 添加边界线
        panel2.setBorder(new TitledBorder("z这是Panel2"));
    
        //添加画板
        setLayout(new GridLayout(2,1,5,5));
        add(panel);
        add(panel2);
    
    
    
    }
    
    
    public static void main(String[] args) {
    
        TestSwingCommonFeatures frame = new TestSwingCommonFeatures();
        frame.setTitle("TestSwingCommonFeatures");
        frame.setSize(500,  300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    
    }
    

    }

    效果: Snip20181019_8.png

    相关文章

      网友评论

          本文标题:Java图形界面

          本文链接:https://www.haomeiwen.com/subject/kdhyzftx.html