2017-05-04 93 views
0

我正在嘗試創建一個Sudoku遊戲,並且無法創建粗體線條以將其分解爲3 x 3個塊。我最近的嘗試是使用JLabel在桌子上加一個圖像。問題是標籤完全覆蓋JTable。我瘋狂地設置標籤和桌子的不透明度,但沒有運氣。下面是一些圖片顯示是我的目標爲:創建粗體單元格邊框線JTable

當前外觀:

enter image description here

目標:

enter image description here

如果你能告訴我作爲一個我可以使用這些方法來創建這些線,這將不勝感激。沒有直接的答案需要,只是一個正確的方向。

+0

將圖像上傳至論壇,使他們能夠在你的問題中顯示。 – camickr

+0

對於任何棋盤遊戲,我都傾向於使用網格佈局(或一組網格佈局)中的按鈕,如[此掃雷遊戲](http://stackoverflow.com/a/43789823/418556)或[this國際象棋棋盤](http://stackoverflow.com/q/21142686/418556)。對於這個GUI中的寄宿生,我會使用一個3 x 3組的九個網格佈局,每個佈局都有一個'LineBorder'。默認情況下,邊框將圍繞顯示的面板的所有四邊,但是它們在邊框處的邊界將是雙倍寬度,從而接近重新創建第二個圖像。 –

回答

2

退房Table Row Rendering

它顯示瞭如何爲一行中的每個單元格提供自定義的Border

因此,您需要修改代碼以提供多個不同邊框,具體取決於要渲染的單元格。

2

對於任何棋盤遊戲,我傾向於在網格佈局(或一組網格佈局)中使用按鈕,如this mine sweeper gamethis chess board

對於此GUI中的邊框,儘管如此,我會使用9個網格佈局的3 x 3組,每個網格佈局都有一個LineBorder。默認情況下,邊框將圍繞顯示的面板的所有四邊,但是它們在邊框處的邊界將是雙倍寬度,從而接近重新創建第二個圖像。

E.G.

enter image description here

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.util.*; 

public class Soduko { 

    private JComponent ui = null; 

    Soduko() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui!=null) return; 

     ui = new JPanel(new GridLayout(3,3)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     ArrayList<Integer> values = new ArrayList<>(); 
     for (int ii=0; ii<34; ii++) { 
      values.add(0); 
     } 
     Random r = new Random(); 
     for (int ii=34; ii<81; ii++) { 
      values.add(r.nextInt(9)+1); 
     } 
     Collections.shuffle(values); 
     int count=0; 
     for (int ii=0; ii<9; ii++) { 
      JPanel p = new JPanel(new GridLayout(3, 3)); 
      p.setBorder(new LineBorder(Color.BLACK, 2)); 
      ui.add(p); 
      for (int jj=0; jj<9; jj++) { 
       int v = values.get(count++).intValue(); 
       String s = v>0 ? "" + v : ""; 
       p.add(new JButton(s)); 
      } 
     } 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       Soduko o = new Soduko(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+1

另請參閱此視圖[組件](http://stackoverflow.com/a/4151403/230513)。 – trashgod