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

📄 framedemo2.java

📁 这是一个英文版的《Java程序设计与问题解决》现在好多大学都当成教材
💻 JAVA
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;/* * FrameDemo2.java is a 1.4 example that shows off the window decoration * features added in 1.4, plus some window positioning code. * It requires no other files. */public class FrameDemo2 extends WindowAdapter                        implements ActionListener {    private Point lastLocation = null;    private int maxX = 500;    private int maxY = 500;    //constants for action commands    protected static String NO_DECORATIONS = "no_dec";    protected static String LF_DECORATIONS = "laf_dec";    protected static String WS_DECORATIONS = "ws_dec";    protected static String CREATE_WINDOW = "new_win";    //true if the next frame created should have no window decorations    protected boolean noDecorations = false;    //Perform some initialization.    public FrameDemo2() {        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();        maxX = screenSize.width - 50;        maxY = screenSize.height - 50;    }    //Create a new MyFrame object and show it.    public void showNewWindow() {        JFrame frame = new MyFrame();        //Take care of the no window decorations case.        //NOTE: Unless you really need the functionality        //provided by JFrame, you would usually use a        //Window or JWindow instead.        if (noDecorations) {            frame.setUndecorated(true);        }        //Set window location.        if (lastLocation != null) {            //Move the window over and down 40 pixels.            lastLocation.translate(40, 40);            if ((lastLocation.x > maxX) || (lastLocation.y > maxY)) {                lastLocation.setLocation(0, 0);            }            frame.setLocation(lastLocation);        } else {            lastLocation = frame.getLocation();        }        //Show window.        frame.setSize(new Dimension(170, 100));        frame.setVisible(true);    }    // Create the window-creation controls that go in the main window.    protected JComponent createOptionControls() {        JLabel label = new JLabel("Options for subsequently created frames:");        ButtonGroup bg = new ButtonGroup();        //Create the buttons        JRadioButton rb1 = new JRadioButton();        rb1.setText("Look and feel decorated");        rb1.setActionCommand(LF_DECORATIONS);        rb1.addActionListener(this);        rb1.setSelected(true);        bg.add(rb1);        //        JRadioButton rb2 = new JRadioButton();        rb2.setText("Window system decorated");        rb2.setActionCommand(WS_DECORATIONS);        rb2.addActionListener(this);        bg.add(rb2);        //        JRadioButton rb3 = new JRadioButton();        rb3.setText("No decorations");        rb3.setActionCommand(NO_DECORATIONS);        rb3.addActionListener(this);        bg.add(rb3);        //Add everything to a container.        javax.swing.Box box = Box.createVerticalBox();        box.add(label);        box.add(Box.createVerticalStrut(5)); //spacer        box.add(rb1);        box.add(rb2);        box.add(rb3);        //Add some breathing room.        box.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        return box;    }    //Create the button that goes in the main window.    protected JComponent createButtonPane() {        JButton button = new JButton("New window");        button.setActionCommand(CREATE_WINDOW);        button.addActionListener(this);        //Center the button in a panel with some space around it.        JPanel pane = new JPanel();        pane.setLayout(new FlowLayout());        pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));        pane.add(button);        return pane;    }    //Handle action events from all the buttons.    public void actionPerformed(ActionEvent e) {        String command = e.getActionCommand();        //Handle the New window button.        if (CREATE_WINDOW.equals(command)) {            showNewWindow();        //Handle the radio buttons.        } else if (NO_DECORATIONS.equals(command)) {            noDecorations = true;            JFrame.setDefaultLookAndFeelDecorated(false);        } else if (WS_DECORATIONS.equals(command)) {            noDecorations = false;            JFrame.setDefaultLookAndFeelDecorated(false);        } else if (LF_DECORATIONS.equals(command)) {            noDecorations = false;            JFrame.setDefaultLookAndFeelDecorated(true);        }    }    //Start the demo.    public static void main(String[] args) {        FrameDemo2 demo = new FrameDemo2();        //Make sure we have nice window decorations.        JFrame.setDefaultLookAndFeelDecorated(true);        JDialog.setDefaultLookAndFeelDecorated(true);        //Create the main window.        JFrame frame = new JFrame("FrameDemo2");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Add components to it.        Container contentPane = frame.getContentPane();        contentPane.add(demo.createOptionControls(),                        BorderLayout.CENTER);        contentPane.add(demo.createButtonPane(),                        BorderLayout.PAGE_END);        //Display the window.        frame.pack();        frame.setLocationRelativeTo(null); //center it        frame.setVisible(true);    }    class MyFrame extends JFrame implements ActionListener {        //Create a frame with a button.        public MyFrame() {            super("A window");            setDefaultCloseOperation(DISPOSE_ON_CLOSE);            //This button lets you close even an undecorated window.            JButton button = new JButton("Close window");            button.addActionListener(this);            //Place the button near the bottom of the window.            Container contentPane = getContentPane();            contentPane.setLayout(new BoxLayout(contentPane,                                                BoxLayout.Y_AXIS));            contentPane.add(Box.createVerticalGlue()); //takes all extra space            contentPane.add(button);            button.setAlignmentX(Component.CENTER_ALIGNMENT); //horizontally centered            contentPane.add(Box.createVerticalStrut(5)); //spacer        }        //Make the button do the same thing as the default close operation        //(DISPOSE_ON_CLOSE).        public void actionPerformed(ActionEvent e) {            setVisible(false);            dispose();        }    }}

⌨️ 快捷键说明

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