import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

class Main {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    private JFrame frame;
    private JPanel panel;
    private JButton buttona, buttonb;
    private JTextField text;
    Police police;

    public MyFrame() {

        police = new Police();
        Font font = new Font("楷体", Font.BOLD, 30);
        frame = new JFrame("test");
        frame.setBounds(600, 500, 800, 600);
        panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));
        buttona = new JButton("文本1");
        buttonb = new JButton("文本2");
        panel.add(buttona);
        panel.add(buttonb);
        text = new JTextField();
        text.setBounds(0, 0, 500, 200);
        text.setEditable(false);
        text.setFont(font);
        Dimension dim = new Dimension(800, 150);
        panel.setPreferredSize(dim);
        buttona.setFont(font);
        buttonb.setFont(font);
        frame.add(text, BorderLayout.CENTER);
        frame.add(panel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        buttona.addActionListener(police);
        buttonb.addActionListener(police);

    }

    class Police implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == buttona) {
                text.setText("");
                text.setText("hello java !");
            }
            else if (e.getSource() == buttonb) {
                text.setText("");
                text.setText("你好 java !");              
            }
        }

    }
}

0
Posted in Java

Leave a Comment:

电子邮件地址不会被公开。