如何使用 Java 设置线条边框颜色和宽度?


若要设置线条边框颜色和宽度,请使用 LineBorder。首先,设置面板,之后我们需要在面板中设置线条边框 -

JPanel panel = new JPanel();

现在,创建边框并设置在上面创建的面板上 -

Border border = new LineBorder(Color.ORANGE, 4, true);
panel.setBorder(border);

以下是一个设置 LineBorder 颜色和宽度的示例 -

package my;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      Border border = new LineBorder(Color.ORANGE, 4, true);
      panel.setBorder(border);
      String[][] rec = {
         { "1", "Steve", "AUS" },
         { "2", "Virat", "IND" },
         { "3", "Kane", "NZ" },
         { "4", "David", "AUS" },
         { "5", "Ben", "ENG" },
         { "6", "Eion", "ENG" },
      };
      String[] header = { "Rank", "Player", "Country" };
      JTable table = new JTable(rec, header);
      table.setShowHorizontalLines(true);
      table.setGridColor(Color.orange);
      table.setCellSelectionEnabled(true);
      panel.add(new JScrollPane(table));
      frame.add(panel);
      frame.setSize(550, 400);
      frame.setVisible(true);
   }
}

输出


更新于: 30-Jul-2019

4K+ 次观看

开启你的 职业

完成课程即可获得认证

开始
广告