GUI編程-3

3.Swing

3.1.窗口

public class JFrameDemo {
    public static void main(String[] args) {
        new MyJFrame();
    }
}
class MyJFrame extends JFrame {
    public MyJFrame(){
        setBounds(200,200,200,200);
        setVisible(true);

        JLabel jLabel = new JLabel("歡迎你");
        add(jLabel);
        //讓文本標籤居中 設置水平對齊
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

//        setBackground(Color.BLUE);在JFrame中不行
        //獲得一個容器
        Container container = getContentPane();
        container.setBackground(Color.black);

//        關閉事件
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.2.彈窗

public class MyDialog extends JFrame {
    public void init(){
        setVisible(true);
        setBounds(200,200,200,200);

        Container container = getContentPane();
        container.setBackground(Color.BLUE);

        JButton jButton = new JButton("按鈕");
        add(jButton);

        //點這個按鈕的時候,彈出一個窗口
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJDialog();
            }
        });
    }
    
    //彈出的窗口
    private class MyJDialog extends JDialog{
        public MyJDialog(){
            setVisible(true);
            setBounds(200,200,200,200);
            Container contentPane = getContentPane();
            contentPane.setBackground(Color.BLACK);
        }
    }

    public static void main(String[] args) {
        new MyDialog().init();
    }
}

3.3.標籤

label

new JLabel("xxx");

圖標icon

//圖標接口,需要實現類,JFrame繼承
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(15,15);
        //圖標放在標籤上,也可以放在按鈕上
        JLabel label = new JLabel("a", iconDemo, SwingConstants.CENTER);

        Container contentPane = getContentPane();
        contentPane.add(label);
        setVisible(true);
        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

public class IconDemo1 extends JFrame {
    public static void main(String[] args) {
        new IconDemo1();
    }
    public IconDemo1(){
        JLabel jLabel = new JLabel("abc");
        URL url = IconDemo1.class.getResource("1.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        int i =1;
        jLabel.setIcon(imageIcon);

        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}

在這裏插入圖片描述

3.4.面板

public class JPanelTest extends JFrame{
    public static void main(String[] args) {
        new JPanelTest();
    }
    public JPanelTest(){
        JPanel jPanel = new JPanel();
        JButton jButton = new JButton("我是一個按鈕");
        jPanel.add(jButton);
        Container contentPane = getContentPane();
        contentPane.add(jPanel);
        setBounds(200,200,200,200);
        setVisible(true);
    }
}
public class JTextTest extends JFrame {
    public JTextTest(){
        Container container = getContentPane();

        //文本域
        JTextArea area = new JTextArea(10,10);
        area.setText("hello");

        //Scroll下拉麪板
        JScrollPane pane = new JScrollPane(area);
        //pane.add(area);這樣不行


        container.add(pane);

        setBounds(200,200,200,200);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextTest();
    }
}

3.5.按鈕

圖片按鈕

public class TestIconButton extends JFrame {
    public TestIconButton(){
        Container container = getContentPane();

        JButton button = new JButton();

        URL url = TestIconButton.class.getResource("1.jpg");

        ImageIcon imageIcon = new ImageIcon(url);
        button.setIcon(imageIcon);
        
        container.add(button);
        setVisible(true);
        setBounds(200,200,200,200);
    }

    public static void main(String[] args) {
        new TestIconButton();
    }
}

單選按鈕

public class TestIconButton extends JFrame {
    public TestIconButton(){
        Container container = getContentPane();

        //單選按鈕
        JRadioButton radioButton1 = new JRadioButton();
        JRadioButton radioButton2 = new JRadioButton();

        //由於單選框只能選擇一個,分組,一個組中只能選擇一個
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);

        container.add(radioButton1,BorderLayout.NORTH);
        container.add(radioButton2,BorderLayout.SOUTH);

        setVisible(true);
        setBounds(200,200,200,200);
    }

    public static void main(String[] args) {
        new TestIconButton();
    }
}

在這裏插入圖片描述

多選按鈕

public class TestIconButton extends JFrame {
    public TestIconButton(){
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
        JCheckBox jCheckBox1 = new JCheckBox();
        JCheckBox jCheckBox2 = new JCheckBox();

        container.add(jCheckBox1);
        container.add(jCheckBox2);
        setVisible(true);
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestIconButton();
    }
}

在這裏插入圖片描述

3.6.列表

下拉列表

public class TestComboBox extends JFrame {
    public static void main(String[] args) {
        new TestComboBox();
    }
    public TestComboBox(){
        Container container = getContentPane();
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("準備上映");
        comboBox.addItem("已經上映");
        comboBox.addItem("上映完畢");

        container.add(comboBox);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}

在這裏插入圖片描述

列表框

public class TestJlist extends JFrame {
    public TestJlist(){
        Container container = getContentPane();
        
        //生成列表的內容
        String[] sr = {"1","2","3"};
        //列表中需要放入內容
        JList list = new JList(sr);

        container.add(list);

        setVisible(true);
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestJlist();
    }
}

在這裏插入圖片描述

3.7.文本框

public class TestText extends JFrame {
    public static void main(String[] args) {
        new TestText();
    }
    public TestComboBox(){
        Container container = getContentPane();
        JTextField textField1 = new JTextField();
        JTextField textField2 = new JTextField();
        container.add(textField1,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}
public class JTextTest extends JFrame {
    public JTextTest(){
        Container container = getContentPane();

        //文本域
        JTextArea area = new JTextArea(10,10);
        area.setText("hello");

        //Scroll下拉麪板
        JScrollPane pane = new JScrollPane(area);
        //pane.add(area);這樣不行


        container.add(pane);

        setBounds(200,200,200,200);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextTest();
    }
}

3.8.密碼框

public class TestComboBox extends JFrame {
    public static void main(String[] args) {
        new TestPW();
    }
    public TestPW(){
        Container container = getContentPane();
        
        JPasswordField passwordField = new JPasswordField();
        //密碼框默認爲..... 也可通過passwordField.setEchoChar('*');手動設置爲*
        
        container.add(passwordField);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(200,200,200,200);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章