在 Java 中的 JPanel 周围创建灰色空间

发布于 2024-09-18 07:33:04 字数 163 浏览 6 评论 0原文

我正在用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:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

明明#如月 2024-09-25 07:33:04

您是否考虑过在 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();
      }
    });
  }
}

替代文本

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();
      }
    });
  }
}

alt text

柠檬 2024-09-25 07:33:04

如何使用边框。创建一个带有 EmptyBorder 的面板,然后向该面板添加第二个 JPanel。

不要在 Swing 应用程序中混合使用 AWT 组件。 Canvas 是一个 AWT 组件。只需使用 JPanel,您仍然可以访问所有 Graphics 方法。

编辑:

不要再将 JPanel 称为 Canvas。 Canvas 是一个 AWT 组件,它的工作方式与 JPanel 不同。

如果您不希望 JPanel 调整大小,请为其指定首选大小并使用 FlowLayout 将其添加到 JPanel。您可以控制 FlowLayout 的水平/垂直间隙以提供边框的外观。

发布您的 SSCCE (http://sscce.org),展示您尝试过的操作以及遇到的问题,而不是我们发布所有代码并猜测您的问题是什么。

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.

ぇ气 2024-09-25 07:33:04

也许将画布添加到 JScrollPane

Perhaps add the canvas into a JScrollPane?

若言繁花未落 2024-09-25 07:33:04

有几种方法可以做到这一点。我想到的最简单的方法是创建一个 JPanel,然后将 Canvas 放入 JPanel 中。将画布中的插图设置为灰色空间的大小。将 JPanel 的背景设置为灰色。像这样的东西:

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);

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);
深海不蓝 2024-09-25 07:33:04

BoxLayout 非常适合这个!我最近也经历过你的处境。

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

现在是重要的部分:

outer.add(Box.createHorizontalGlue());
outer.add(inner);
outer.add(Box.createHorizontalGlue());

无论您如何缩放窗口,这都会使内部面板保持居中并保持固定大小!

这就是它的样子

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文