Creating a gray space around a JPanel in Java
I'm developing an simple art program in Java and I was curious if it was possible 开发者_如何转开发to create an gray empty space around the canvas like is most art programs(Meaning, an empty space that isn't locked down and is scrollable). Is this possible and how could I go about doing this?
Example:
Have you considered using a Line Border within the JPanel.
import javax.swing.*;
import java.awt.*;
class Testing
{
public void buildGUI()
{
JFrame f = new JFrame();
JPanel o_panel = new JPanel();
JPanel panel = new JPanel();
panel.setMinimumSize(new Dimension(500,500));
panel.setPreferredSize(new Dimension(500,500));
panel.setMaximumSize(new Dimension(500,500));
panel.setBackground(Color.RED);
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY,25));
o_panel.add(panel);
f.getContentPane().add(o_panel);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
How to Use Borders. Create a panel with an EmptyBorder and then add a second JPanel to the panel.
Don't mix AWT components in a Swing application. Canvas is an AWT component. Just use a JPanel, you still have access to all the Graphics methods.
Edit:
Quit calling your JPanel a Canvas. A Canvas is an AWT component and it works differently than a JPanel.
If you don't want your JPanel to resize then give it a preferred size and add it to a JPanel using a FlowLayout. You can control the horizontal/vertical gaps of a FlowLayout to give the appearance of a Border.
Post your SSCCE (http://sscce.org) showing what you have tried and the problems you have encountered rather than us posting all the code and guessing what your problems are.
Perhaps add the canvas into a JScrollPane?
There are several ways to do this. The easiest that comes to my head is to create a JPanel and then put the Canvas in the JPanel. Set the insets in the Canvas to the size of your gray space. Set the background of the JPanel to Gray. Something like this:
Canvas canvas = new Canvas();
JPanel panel = new JPanel();
panel.setBackground(Color.GRAY);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
gc.fill = BOTH;
gc.weightx = 1.0;
gc.weighty = 1.0;
gc.insets = new Insets(30, 30, 30, 30); // Your gray space
layout.setConstraints(canvas, gc);
panel.add(canvas);
panel.setLayout(layout);
BoxLayout is great for this! I've been in your shoes very recently.
JPanel outer = new JPanel();//this will be our grey area
outer.setLayout(new BoxLayout(outer, BoxLayout.X_AXIS));
JPanel inner = new JPanel();//here's the drawing area
Dimension receiptSize = new Dimension(400, 400);//making a fixed size for the drawing area
inner.setPreferredSize(receiptSize);
inner.setMinimumSize(receiptSize);
inner.setMaximumSize(receiptSize);
inner.setBackground(new Color(255,255,255));//and making the drawing area white, just for clarity in this example
And now for the important part:
outer.add(Box.createHorizontalGlue());
outer.add(inner);
outer.add(Box.createHorizontalGlue());
And that will keep the inner panel centered and at a fixed size no matter how you scale the window!
Here's what it looks like
精彩评论