My projects constists of two classes, GoBoard extends JPanel.
GoTest.java:
import javax.swing.*;
import java.awt.Graphics;
import java.io.*;
import java.awt.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
class GoTest{
private static void initGui(){
JFrame frame = new JFrame("GoBoard");
GoBoard jboard = new GoBoard();
jboard.setLayout(new BorderLayout(10,10));
jboard.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
frame.add(jboard);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
initGui();
}
});
}
}
GoBoard.java:
import javax.swing.*;
import java.awt.Graphics;
import javax.swing.border.Border;
class GoBoard extends JPanel{
private int linien;
public GoBoard(){
this(9);
}
public GoBoard(int pLinien){
this.linien = pLinien;
this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int d = 0;
int h = 0;
for(int i = 0; i < this.linien; i++){
g.drawLine(0,h, getWidth(), h);
g.drawLine(d,0,d,getHeight());
h += getHeight()/this.linien;
d +=getWidth()/this.linien;
}
}
}
I want to set a border to have padding according to the frame where I display the panel. However I get no border. Any idea?
Yes you can draw the borders around the undecorated JFrame. Just simply get the root pane of the JFrame and set its borders by setBorder(Border border) method. As far as I know, this will only shrink down the root pane so it can draw the border inside the frame itself.
JPanel jPanel = new JPanel();
jPanel.setBorder(BorderFactory.createLineBorder(Color.black));
Here not only jPanel, you can add border to any Jcomponent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With