Java by API/javax.swing/ScrollPaneConstants — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED

 
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class MainClass extends JPanel {
  public MainClass() {
    setLayout(new BorderLayout());
    JPanel jp = new JPanel();
    jp.setLayout(new GridLayout(20, 20));
    int b = 0;
    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        jp.add(new JButton("Button " + b));
        ++b;
      }
    }
    // Add panel to a scroll pane.
    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
    JScrollPane jsp = new JScrollPane(jp, v, h);
    // Add scroll pane to the content pane.
    add(jsp, BorderLayout.CENTER);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}





ScrollPaneConstants.LOWER_LEFT_CORNER

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class Main extends JFrame {
  private JLabel label = new JLabel(new ImageIcon("largejexpLogo.gif"));
  public Main() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);
    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
      corners[i] = new JLabel();
      corners[i].setBackground(Color.white);
      corners[i].setOpaque(true);
    }
    JLabel rowheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle rect = g.getClipBounds();
        for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
          g.drawLine(0, rect.y + i, 3, rect.y + i);
          g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension(25, (int) label.getPreferredSize()
            .getHeight());
      }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);
    JLabel columnheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle r = g.getClipBounds();
        for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
          g.drawLine(r.x + i, 0, r.x + i, 3);
          g.drawString("" + (r.x + i), r.x + i - 10, 16);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension((int) label.getPreferredSize().getWidth(),
            25);
      }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);
    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);
    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
  }
  public static void main(String[] args) {
    new Main();
  }
}





ScrollPaneConstants.LOWER_RIGHT_CORNER

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class Main extends JFrame {
  private JLabel label = new JLabel(new ImageIcon("largejexpLogo.gif"));
  public Main() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);
    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
      corners[i] = new JLabel();
      corners[i].setBackground(Color.white);
      corners[i].setOpaque(true);
    }
    JLabel rowheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle rect = g.getClipBounds();
        for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
          g.drawLine(0, rect.y + i, 3, rect.y + i);
          g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension(25, (int) label.getPreferredSize()
            .getHeight());
      }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);
    JLabel columnheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle r = g.getClipBounds();
        for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
          g.drawLine(r.x + i, 0, r.x + i, 3);
          g.drawString("" + (r.x + i), r.x + i - 10, 16);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension((int) label.getPreferredSize().getWidth(),
            25);
      }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);
    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);
    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
  }
  public static void main(String[] args) {
    new Main();
  }
}





ScrollPaneConstants.UPPER_LEFT_CORNER

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class Main extends JFrame {
  private JLabel label = new JLabel(new ImageIcon("largejexpLogo.gif"));
  public Main() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);
    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
      corners[i] = new JLabel();
      corners[i].setBackground(Color.white);
      corners[i].setOpaque(true);
    }
    JLabel rowheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle rect = g.getClipBounds();
        for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
          g.drawLine(0, rect.y + i, 3, rect.y + i);
          g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension(25, (int) label.getPreferredSize()
            .getHeight());
      }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);
    JLabel columnheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle r = g.getClipBounds();
        for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
          g.drawLine(r.x + i, 0, r.x + i, 3);
          g.drawString("" + (r.x + i), r.x + i - 10, 16);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension((int) label.getPreferredSize().getWidth(),
            25);
      }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);
    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);
    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
  }
  public static void main(String[] args) {
    new Main();
  }
}





ScrollPaneConstants.UPPER_RIGHT_CORNER

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class Main extends JFrame {
  private JLabel label = new JLabel(new ImageIcon("largejexpLogo.gif"));
  public Main() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);
    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
      corners[i] = new JLabel();
      corners[i].setBackground(Color.white);
      corners[i].setOpaque(true);
    }
    JLabel rowheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle rect = g.getClipBounds();
        for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
          g.drawLine(0, rect.y + i, 3, rect.y + i);
          g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension(25, (int) label.getPreferredSize()
            .getHeight());
      }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);
    JLabel columnheader = new JLabel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Rectangle r = g.getClipBounds();
        for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
          g.drawLine(r.x + i, 0, r.x + i, 3);
          g.drawString("" + (r.x + i), r.x + i - 10, 16);
        }
      }
      public Dimension getPreferredSize() {
        return new Dimension((int) label.getPreferredSize().getWidth(),
            25);
      }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);
    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);
    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
  }
  public static void main(String[] args) {
    new Main();
  }
}





ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS

  

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
public class Main extends JFrame {
  JTextArea textArea;
  public Main() {
    super();
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();
    textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    contentPane.add(pane, BorderLayout.CENTER);
  }
  public static void main(String[] args) {
    JFrame f = new Main();
    f.setSize(300, 200);
    f.setVisible(true);
  }
}





ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED

 
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class MainClass extends JPanel {
  public MainClass() {
    setLayout(new BorderLayout());
    JPanel jp = new JPanel();
    jp.setLayout(new GridLayout(20, 20));
    int b = 0;
    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        jp.add(new JButton("Button " + b));
        ++b;
      }
    }
    // Add panel to a scroll pane.
    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
    JScrollPane jsp = new JScrollPane(jp, v, h);
    // Add scroll pane to the content pane.
    add(jsp, BorderLayout.CENTER);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}