📄 swing 小应用程序和应用程序.txt
字号:
Swing 小应用程序和应用程序 List.1 SwingApplet/SwingApplet.java
Swing 小应用程序和应用程序 List.2 SwingApp/SwingApp.java
Swing 小应用程序和应用程序 List.3 SwingMenuDemo/SwingMenuDemo.java
Swing 小应用程序和应用程序 List.4 FlowDemo/FlowDemo.java
Swing 小应用程序和应用程序 List.5 BorderDemo/BorderDemo.java
Swing 小应用程序和应用程序 List.6 GridDemo/GridDemo.java
Swing 小应用程序和应用程序 List.7 GridBagDemo/GridBagDemo.java
Swing 小应用程序和应用程序 List.8 BoxDemo/BoxDemo.java
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.1 SwingApplet/SwingApplet.java
Return to top
001: import javax.swing.*;
002: import java.awt.*;
003:
004: public class SwingApplet extends JApplet {
005: public void init() {
006: JLabel label =
007: new JLabel("Simple Java 2 Swing Applet", JLabel.CENTER);
008: label.setBorder(BorderFactory.createLineBorder(Color.black));
009: getContentPane().add(label, BorderLayout.CENTER);
010: }
011: }
Return to top
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.2 SwingApp/SwingApp.java
Return to top
001: import javax.swing.*;
002: import java.awt.*;
003: import java.awt.event.*;
004:
005: public class SwingApp {
006:
007: // Create application component pane as a JPanel container
008: public static JPanel createPane() {
009: JPanel pane = new JPanel();
010: JLabel label = new JLabel("Simple Swing Application");
011: pane.setBorder(
012: BorderFactory.createEmptyBorder(30, 30, 50, 75));
013: pane.add(label);
014: return pane;
015: }
016:
017: public static void main(String[] args) {
018: // Use system look and feel
019: try {
020: UIManager.setLookAndFeel(
021: UIManager.getCrossPlatformLookAndFeelClassName());
022: } catch (Exception e) { }
023:
024: // Create the top-level frame and its components
025: JFrame frame = new JFrame("Simple Swing Application");
026: JPanel components = createPane(); // Create components pane
027: frame.getContentPane().add(components, BorderLayout.CENTER);
028:
029: // End program when window closes
030: frame.addWindowListener(new WindowAdapter() {
031: public void windowClosing(WindowEvent e) {
032: System.exit(0);
033: }
034: });
035:
036: // Engage layout manager and display window
037: frame.pack();
038: frame.setVisible(true);
039: }
040: }
Return to top
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.3 SwingMenuDemo/SwingMenuDemo.java
Return to top
001: import javax.swing.*;
002: import java.awt.*;
003: import java.awt.event.*;
004:
005: public class SwingMenuDemo extends JFrame {
006:
007: // Constructor does all the setup work
008: public SwingMenuDemo() {
009: JMenuBar menuBar; // Menu bar (contains all menus)
010: JMenu menu; // Pulldown menus
011: JMenuItem menuItem; // Items inside pulldown menus
012:
013: // End program when window closes
014: addWindowListener(new WindowAdapter() {
015: public void windowClosing(WindowEvent e) {
016: System.exit(0);
017: }
018: });
019:
020: // Create the menu bar, menu, and menu item
021: menuBar = new JMenuBar();
022: setJMenuBar(menuBar);
023: menu = new JMenu("Demo");
024: menuBar.add(menu);
025: menuItem = new JMenuItem("Exit");
026: menu.add(menuItem);
027:
028: // Attach listener for the menu item
029: menuItem.addActionListener(new ActionListener() {
030: public void actionPerformed(ActionEvent e) {
031: System.exit(0);
032: }
033: });
034: }
035:
036: // Because SwingMenuDemo is a JFrame, main() is much simpler!
037: public static void main(String[] args) {
038: SwingMenuDemo app = new SwingMenuDemo();
039: app.setTitle("Swing Menu Demo");
040: app.setSize(400, 300);
041: app.show();
042: }
043: }
Return to top
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.4 FlowDemo/FlowDemo.java
Return to top
001: import javax.swing.*;
002: import java.applet.*;
003: import java.awt.*;
004:
005: public class FlowDemo extends JApplet {
006: int alignment; // Current FlowLayout alignment
007:
008: public void init() {
009: JPanel pane = new JPanel();
010: alignment = FlowLayout.LEFT;
011: // alignment = FlowLayout.CENTER;
012: // alignment = FlowLayout.RIGHT;
013: pane.setLayout(new FlowLayout(alignment));
014: pane.add(new JButton("Button1"));
015: pane.add(new JButton("Button2"));
016: pane.add(new JButton("Button3"));
017: pane.add(new JButton("Button4"));
018: pane.add(new JButton("Button5"));
019: getContentPane().add(pane, BorderLayout.CENTER);
020: }
021: }
Return to top
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.5 BorderDemo/BorderDemo.java
Return to top
001: import javax.swing.*;
002: import java.applet.*;
003: import java.awt.*;
004:
005: public class BorderDemo extends JApplet
006: {
007: public void init() {
008: JPanel pane = new JPanel();
009: pane.setLayout(new BorderLayout());
010: pane.add("North", new JButton("North"));
011: pane.add("South", new JButton("South"));
012: pane.add("East", new JButton("East"));
013: pane.add("West", new JButton("West"));
014: pane.add("Center", new JButton("Center"));
015: getContentPane().add(pane, BorderLayout.CENTER);
016: }
017: }
Return to top
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.6 GridDemo/GridDemo.java
Return to top
001: import javax.swing.*;
002: import java.applet.*;
003: import java.awt.*;
004:
005: public class GridDemo extends JApplet {
006:
007: public void init() {
008: JPanel pane = new JPanel();
009: pane.setLayout(new GridLayout(4, 3, 8, 16));
010: pane.add(new JButton(" 1"));
011: pane.add(new JButton("ABC 2"));
012: pane.add(new JButton("DEF 3"));
013: pane.add(new JButton("GHI 4"));
014: pane.add(new JButton("JKL 5"));
015: pane.add(new JButton("MNO 6"));
016: pane.add(new JButton("PRS 7"));
017: pane.add(new JButton("TUV 8"));
018: pane.add(new JButton("WXY 9"));
019: pane.add(new JButton(" * "));
020: pane.add(new JButton("Opr 0"));
021: pane.add(new JButton(" # "));
022: getContentPane().add(pane, BorderLayout.CENTER);
023: }
024: }
Return to top
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.7 GridBagDemo/GridBagDemo.java
Return to top
001: import javax.swing.*;
002: import java.applet.*;
003: import java.awt.*;
004:
005: public class GridBagDemo extends JApplet {
006:
007: protected void makeButton(String name, GridBagLayout gridbag,
008: GridBagConstraints c, JPanel pane)
009: {
010: JButton button = new JButton(name);
011: gridbag.setConstraints(button, c);
012: pane.add(button);
013: }
014:
015: // Initialize applet and GUI buttons
016: public void init() {
017: JPanel pane = new JPanel(); // Create content pane
018: // Create GridBagLayout and Constraints objects
019: GridBagLayout gridbag = new GridBagLayout();
020: GridBagConstraints c = new GridBagConstraints();
021: pane.setLayout(gridbag); // Tell pane to use gridbag layout
022:
023: // Create four "normal" buttons on the top row
024: c.fill = GridBagConstraints.NONE;
025: c.weightx = 1.0;
026: makeButton("Button 1", gridbag, c, pane);
027: makeButton("Button 2", gridbag, c, pane);
028: makeButton("Button 3", gridbag, c, pane);
029: c.gridwidth = GridBagConstraints.REMAINDER;
030: makeButton("Button 4", gridbag, c, pane);
031:
032: // Create a long button filling entire row
033: c.fill = GridBagConstraints.BOTH;
034: c.weightx = 0.0;
035: makeButton("Button 5", gridbag, c, pane);
036:
037: // Create two buttons that fill the row
038: c.gridwidth = GridBagConstraints.RELATIVE;
039: makeButton("Button 6", gridbag, c, pane);
040: c.gridwidth = GridBagConstraints.REMAINDER;
041: makeButton("Button 7", gridbag, c, pane);
042:
043: // Create a vertical button
044: c.gridwidth = 1;
045: c.gridheight = 2;
046: c.weighty = 1.0;
047: makeButton("Button 8", gridbag, c, pane);
048: c.weighty = 0.0;
049:
050: // Create buttons to right of vertical Button 8
051: c.gridwidth = GridBagConstraints.REMAINDER;
052: c.gridheight = 1;
053: makeButton("Button 9", gridbag, c, pane);
054: makeButton("Button 10", gridbag, c, pane);
055:
056: // Add content pane to applet top-level container
057: getContentPane().add(pane, BorderLayout.CENTER);
058: setSize(325, 250);
059: }
060: }
Return to top
--------------------------------------------------------------------------------
Swing 小应用程序和应用程序 List.8 BoxDemo/BoxDemo.java
Return to top
001: import javax.swing.*;
002: import java.applet.*;
003: import java.awt.*;
004:
005: public class BoxDemo extends JApplet
006: {
007: // Add new button to pane, with center alignment
008: protected void addButton(String label, JPanel pane) {
009: JButton button = new JButton(label);
010: button.setAlignmentX(Component.CENTER_ALIGNMENT);
011: pane.add(button);
012: }
013:
014: public void init() {
015: JPanel pane = new JPanel();
016: pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
017: addButton("Small", pane);
018: addButton("tiny", pane);
019: addButton("Really Big Button", pane);
020: addButton("Bottom Button", pane);
021: getContentPane().add(pane, BorderLayout.CENTER);
022: }
023: }
Return to top
? 2003 by ChinaITLab.com All rights reserved.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -