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

📄 layouttester.java

📁 一本有关Java图形界面设计的一本书,英文的,附带有源码,
💻 JAVA
字号:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.*;

/**
 * LayoutTester is a test window that displays rotatable RLabels
 * and switches between different rotation-aware layout managers.
 */
public class LayoutTester extends JFrame {
    //  panel containing the RLabels
    JPanel panel;
    
    // poem we use for testing
    private static final String POEM[] = {
            "Turning and turning<br>in the widening gyre",
            "The falcon cannot<br>hear the falconer;", 
            "Things fall apart;", 
            "the centre cannot hold;", 
            "Mere anarchy is<br>loosed upon the world,",
            "The blood-dimmed tide<br>is loosed,", 
            "and everywhere", 
            "The ceremony of<br>innocence is drowned;",
            "The best lack<br>all conviction,",
            "while the worst", 
            "Are full of<br>passionate intensity.",
    };
            
    
    /**
     * Make a LayoutTester.
     */
    public LayoutTester() {
        super("The Second Coming (by William Butler Yeats)");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        // create the UI
        Container c = getContentPane();
        
        // make buttons that change the layout of the board
        JPanel buttons = new JPanel();
        buttons.add(new JButton(new AbstractAction("Circle") {
            public void actionPerformed(ActionEvent event) {
                panel.setLayout(new CircleLayout());
                panel.validate();
            }
        }));
        buttons.add(new JButton(new AbstractAction("Tilt") {
            public void actionPerformed(ActionEvent event) {
                panel.setLayout(new TiltLayout());
                panel.validate();
            }
        }));
        buttons.add(new JButton(new AbstractAction("Add Label") {
            public void actionPerformed(ActionEvent event) {
                int n = panel.getComponentCount();
                if (n < POEM.length) {
                    System.err.println("adding " + POEM[n]);
                    panel.add(makePoemLine(n));
                    panel.validate();
                }
            }
        }));
        buttons.add(new JButton(new AbstractAction("Remove Label") {
            public void actionPerformed(ActionEvent event) {
                int n = panel.getComponentCount();
                if (n > 0) {
                    panel.remove(n-1);
                    panel.validate();
                    panel.repaint();                    
                }
            }
        }));
        
        c.add(buttons, BorderLayout.NORTH);
        
        panel = new JPanel();
        panel.setLayout(new CircleLayout());
        panel.add(makePoemLine(0));
        panel.add(makePoemLine(1));
        panel.add(makePoemLine(2));
        c.add(panel, BorderLayout.CENTER);
        
        pack();
    }
    
    /*
     * Make an RLabel containing the ith line of the poem.
     */
    private RLabel makePoemLine(int i) {
        return new RLabel("<html>" + POEM[i] + "</html>", 0);
    }
    
    /**
     * Main method.  Makes and displays a LayoutTester window.
     * @param args Command-line arguments, ignored.
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run () {
                new LayoutTester().show();
            }
        });
    }
}

⌨️ 快捷键说明

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