1. 程式人生 > >java 圖形使用者介面

java 圖形使用者介面

1.程式設計包含一個標籤和一個按鈕,單擊按鈕時,標籤的內容在”你好”和”再見”之間切換。

package s1;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class MyWin extends
JFrame implements ActionListener {
JButton b=new JButton("點選"); JLabel l=new JLabel("你好"); public MyWin(){ FlowLayout f=new FlowLayout(); this.setLayout(f); this.add(l); this.add(b); b.addActionListener(this); this.setBounds(10,10, 500, 400); this
.setVisible(true); this.validate(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b){ if(l.getText().equals("你好")) l.setText("再見"); else if(l.getText().equals("再見"
)) l.setText("你好"); } } } package s1; public class Test { public static void main(String[] a){ new MyWin(); } }

2,程式設計包含一個文字框和一個文字區域,在文字框中按回車鍵時,把文字框的內容寫入文字區域。

package s2;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class MyWin extends JFrame implements ActionListener{
    JTextField b;
    JTextArea a;
    public MyWin(){
        FlowLayout l=new FlowLayout();
        this.setLayout(l);
        JLabel l2=new JLabel("input:");
        this.add(l2);
        b=new JTextField(30);
        b.addActionListener(this);
        this.add(b);
        a=new JTextArea(20,20);
        this.add(a);
        this.setBounds(100,100,500,400);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b){
            if(a.getText().equals(""))
                a.setText(b.getText());
            else a.setText(a.getText()+"\n"+b.getText());
            b.setText("");
        }
    }
}
package s2;

public class Test {
    public static void main(String[] a){
        new MyWin();
    }
}

3,試設計一個視窗,視窗介面如下圖。包含Style選單、Color選單和Exit選單,Style選單設計字型的樣式(包括Plane、Bold、Italic),Color(紅、綠、藍、自定義)選單設計字型的顏色、Exit選單(CloseWindow)退出系統。
這裡寫圖片描述

package s3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Ys extends JFrame{
    JMenuBar mb;
    JMenu me1,me2,me3;
    JMenuItem m1,m2,m3,m4,m5,m6,m7,m8;
    JLabel la;
    Font f;
    Ys(){
        f=new Font("楷體",Font.BOLD,30);
        la=new JLabel("See me?",JLabel.CENTER);
        la.setFont(f);
        add(la);
        mb=new JMenuBar();
        me1=new JMenu("Style");
        me2=new JMenu("Color");
        me3=new JMenu("Exit");
        m1=new JMenuItem("Plain");
        m2=new JMenuItem("Bold");
        m3=new JMenuItem("Italic");
        m4=new JMenuItem("紅");
        m5=new JMenuItem("綠");
        m6=new JMenuItem("藍");
        m7=new JMenuItem("自定義");
        m8=new JMenuItem("CloseWindow");
        setJMenuBar(mb);
        mb.add(me1);
        mb.add(me2);
        mb.add(me3);
        me1.add(m1);
        me1.addSeparator();
        me1.add(m2);
        me1.addSeparator();
        me1.add(m3);
        me2.add(m4);
        me2.addSeparator();
        me2.add(m5);
        me2.addSeparator();
        me2.add(m6);
        me2.addSeparator();
        me2.add(m7);
        me3.add(m8);
        m1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                f=new Font("楷體",Font.PLAIN,30);
                la.setFont(f);
            }

        });
        m2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                f=new Font("楷體",Font.BOLD,30);
                la.setFont(f);
            }

        });
        m3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                f=new Font("Italic",Font.ITALIC,30);
                la.setFont(f);
            }

        });
        m4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.red);
            }

        });
        m5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.green);
            }

        });
        m6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.blue);
            }

        });
        m7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.pink);
            }

        });
        m8.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
               System.exit(0);
            }

        });
        setBounds(100,100,400,300);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
package s3;

public class Test {

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

}