2017-06-03 63 views
0

我想獲得與DrawCanvas(960x960)框架(640x640)內是這樣的: enter image description here認沽分量較小的一個

所以DrawCanvas的部分必須被隱藏,並且畫面中心。我應該選擇什麼佈局?現在我把DrawCanvas框架內是這樣的:

class extends JFrame { 
     canvas = new DrawCanvas(); 
     canvas.setPreferredSize(new Dimension(960, 960)); 
     Container cp = getContentPane(); 
     cp.add(canvas); 
     setPreferredSize(new Dimension(640, 640)); 
    } 
+0

我認爲你可以通過使用[Boxlayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html)來完成,用網格區域填充空格。 – Articuno

回答

4

同樣在你前面的問題說,一個辦法做到這一點是嵌入JScrollPane中,內內組成部分,但擺脫滾動條的滾動窗格。

例如,假設您有一個名爲DrawPanel的類,它擴展了JPanel並擁有一個12 x 12的網格單元格,但您只想顯示8 x 8個單元格。可以使用這個類實現滾動界面,有其getPreferredScrollableViewportSize方法返回的要顯示什麼樣的尺寸,而其getPreferredSize方法將返回組件的實際尺寸:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.Rectangle; 

import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Scrollable; 

@SuppressWarnings("serial") 
public class DrawPanel extends JPanel implements Scrollable { 
    public static final int VISIBLE_ROW_COUNT = 8; 
    private static final int ROW_COUNT = 12; 
    private static final Color DARK_COLOR = Color.DARK_GRAY; 
    private static final Color LIGHT_COLOR = Color.WHITE; 
    private int cellWidth; 

    public DrawPanel(int cellWidth) { 
     super(new GridLayout(ROW_COUNT, ROW_COUNT)); 
     this.cellWidth = cellWidth; 
     Dimension prefSize = new Dimension(cellWidth, cellWidth); 
     for (int i = 0; i < ROW_COUNT; i++) { 
      for (int j = 0; j < ROW_COUNT; j++) { 
       JPanel cell = new JPanel(); 
       cell.setPreferredSize(prefSize); 
       Color c = i % 2 == j % 2 ? DARK_COLOR : LIGHT_COLOR; 
       cell.setBackground(c); 
       add(cell); 

       // TODO: delete the code below. For demo/debug purposes only 
       String text = String.format("[%d, %d]", j, i); 
       JLabel label = new JLabel(text); 
       c = c == DARK_COLOR ? LIGHT_COLOR : DARK_COLOR; 
       label.setForeground(c); 
       cell.add(label); 
       // TODO: end delete block 
      } 
     } 
    } 

    @Override 
    public Dimension getPreferredScrollableViewportSize() { 
     Dimension viewportSize = new Dimension(VISIBLE_ROW_COUNT * cellWidth, VISIBLE_ROW_COUNT * cellWidth); 
     return viewportSize; 
    } 

    @Override 
    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, 
      int direction) { 
     return cellWidth; 
    } 

    @Override 
    public boolean getScrollableTracksViewportHeight() { 
     return false; 
    } 

    @Override 
    public boolean getScrollableTracksViewportWidth() { 
     return false; 
    } 

    @Override 
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, 
      int direction) { 
     return 1; 
    } 

    // note that getPreferredSize already returns the size of the 12 x 12 grid 
    // since it is composed of a grid sized JPanels 

} 

然後,您可以內把這個你JScrollPane中,

private static final int CELL_WIDTH = 80; 
private DrawPanel drawPanel = new DrawPanel(CELL_WIDTH); 
private JScrollPane drawPanelScrollPane = new JScrollPane(drawPanel); 

擺脫滾動條,

drawPanelScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
    drawPanelScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 

,並滾動到SPO

int x = 2 * CELL_WIDTH; 
    int y = x; 
    Point pt = new Point(x, y); 
    drawPanelScrollPane.getViewport().setViewPosition(pt); 

的例子GUI:

import java.awt.BorderLayout; 
import java.awt.Point; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class FrameAGrid extends JPanel { 
    private static final int CELL_WIDTH = 80; 
    private DrawPanel drawPanel = new DrawPanel(CELL_WIDTH); 
    private JScrollPane drawPanelScrollPane = new JScrollPane(drawPanel); 

    public FrameAGrid() { 
     drawPanelScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     drawPanelScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 
     setLayout(new BorderLayout()); 
     add(drawPanelScrollPane); 

     int x = 2 * CELL_WIDTH; 
     int y = x; 
     Point pt = new Point(x, y); 
     drawPanelScrollPane.getViewport().setViewPosition(pt); 
    } 


    private static void createAndShowGui() { 
     FrameAGrid mainPanel = new FrameAGrid(); 

     JFrame frame = new JFrame("FrameAGrid"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

這將顯示爲: enter image description here

注意,左上角是2,2不是你想要的左上點t在0,0

+0

非常感謝您的工作!我認爲JScrollPane,根據它的名字,需要滾動條,我不能刪除它們。我會試試這個。 – Russiancold

+0

您可以在不使用JScrollPane的情況下將任何組件嵌入到任何其他組件中,但是這樣做的好處是,如果您稍後想要顯示未顯示的區域,則只需移動視口即可。 –

+0

爲什麼不在0,0? – Articuno