⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinterface.java

📁 现在在国外大学里最流行的java学习软件,同时还有大量的example,在名为project的文件里.安装好后用bluej打开peoject的例子,可以进行你想要的任何变化.同时可以了解大量的源码
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;/** * A graphical user interface for the calculator. No calculation is being * done here. This class is responsible just for putting up the display on  * screen. It then refers to the "CalcEngine" to do all the real work. *  * @author David J. Barnes and Michael Kolling * @version 2006.03.30 */public class UserInterface    implements ActionListener{    private CalcEngine calc;    private boolean showingAuthor;    private JFrame frame;    private JTextField display;    private JLabel status;    /**     * Create a user interface.     * @param engine The calculator engine.     */    public UserInterface(CalcEngine engine)    {        calc = engine;        showingAuthor = true;        makeFrame();        frame.setVisible(true);    }    /**     * Set the visibility of the interface.     * @param visible true if the interface is to be made visible, false otherwise.     */    public void setVisible(boolean visible)    {        frame.setVisible(visible);    }    /**     * Make the frame for the user interface.     */    private void makeFrame()    {        frame = new JFrame(calc.getTitle());                JPanel contentPane = (JPanel)frame.getContentPane();        contentPane.setLayout(new BorderLayout(8, 8));        contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));        display = new JTextField();        contentPane.add(display, BorderLayout.NORTH);        JPanel buttonPanel = new JPanel(new GridLayout(4, 4));            addButton(buttonPanel, "7");            addButton(buttonPanel, "8");            addButton(buttonPanel, "9");            addButton(buttonPanel, "C");                        addButton(buttonPanel, "4");            addButton(buttonPanel, "5");            addButton(buttonPanel, "6");            addButton(buttonPanel, "?");                        addButton(buttonPanel, "1");            addButton(buttonPanel, "2");            addButton(buttonPanel, "3");            buttonPanel.add(new JLabel(" "));                        addButton(buttonPanel, "0");            addButton(buttonPanel, "+");            addButton(buttonPanel, "-");            addButton(buttonPanel, "=");                    contentPane.add(buttonPanel, BorderLayout.CENTER);        status = new JLabel(calc.getAuthor());        contentPane.add(status, BorderLayout.SOUTH);        frame.pack();    }    /**     * Add a button to the button panel.     * @param panel The panel to receive the button.     * @param buttonText The text for the button.     */    private void addButton(Container panel, String buttonText)    {        JButton button = new JButton(buttonText);        button.addActionListener(this);        panel.add(button);    }    /**     * An interface action has been performed.     * Find out what it was and handle it.     * @param event The event that has occured.     */    public void actionPerformed(ActionEvent event)    {        String command = event.getActionCommand();        if(command.equals("0") ||           command.equals("1") ||           command.equals("2") ||           command.equals("3") ||           command.equals("4") ||           command.equals("5") ||           command.equals("6") ||           command.equals("7") ||           command.equals("8") ||           command.equals("9")) {            int number = Integer.parseInt(command);            calc.numberPressed(number);        }        else if(command.equals("+")) {            calc.plus();        }        else if(command.equals("-")) {            calc.minus();        }        else if(command.equals("=")) {            calc.equals();        }        else if(command.equals("C")) {            calc.clear();        }        else if(command.equals("?")) {            showInfo();        }        // else unknown command.        redisplay();    }    /**     * Update the interface display to show the current value of the      * calculator.     */    private void redisplay()    {        display.setText("" + calc.getDisplayValue());    }    /**     * Toggle the info display in the calculator's status area between the     * author and version information.     */    private void showInfo()    {        if(showingAuthor)            status.setText(calc.getVersion());        else            status.setText(calc.getAuthor());        showingAuthor = !showingAuthor;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -