当我使用 java.awt.Canvas 时,JSplitPane 不会调整大小?

发布于 2024-11-10 04:33:13 字数 1930 浏览 0 评论 0原文

我有一个简单的示例,其中我使用 java.awt.Canvas 作为 JSplitPane 中的组件,并且 JSplitPane 不允许调整。有人能解释一下为什么吗? (更重要的是,如何修复)

VerticalSplit.java:CanvasTest1.java

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

abstract public class VerticalSplit {
    public VerticalSplit(JPanel panel)
    {
        panel.setLayout(new BorderLayout());
        JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
            createComponent1(), createComponent2());
        jsp.setResizeWeight(0.5);
        panel.add(jsp, BorderLayout.CENTER);
    }
    abstract protected Component createComponent1();
    abstract protected Component createComponent2();
}

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CanvasTest1 extends VerticalSplit
{
    static public class PlainCanvas extends Canvas
    {
        @Override public void paint(Graphics g)
        {
            Dimension d = getSize();
            int h = getHeight();
            int w = getWidth();
            g.drawRect(0,0,w,h);
            g.drawLine(0,0,w,h);
            g.drawLine(0,h,w,0);
        }
    };

    public CanvasTest1(JPanel panel) {  super(panel); }

    @Override protected Component createComponent1() {
        return new PlainCanvas();
    }

    @Override protected Component createComponent2() {
        return new PlainCanvas();
    }

    public static void main(String[] args) {
        JPanel panel = new JPanel();
        CanvasTest1 test = new CanvasTest1(panel);

        JFrame frame = new JFrame(test.getClass().getName());
        frame.setContentPane(panel);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

I have a simple example where I'm using java.awt.Canvas for components within a JSplitPane, and the JSplitPane won't allow adjustment. Could anyone explain why? (and more importantly, how to fix)

VerticalSplit.java:

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

abstract public class VerticalSplit {
    public VerticalSplit(JPanel panel)
    {
        panel.setLayout(new BorderLayout());
        JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
            createComponent1(), createComponent2());
        jsp.setResizeWeight(0.5);
        panel.add(jsp, BorderLayout.CENTER);
    }
    abstract protected Component createComponent1();
    abstract protected Component createComponent2();
}

CanvasTest1.java:

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CanvasTest1 extends VerticalSplit
{
    static public class PlainCanvas extends Canvas
    {
        @Override public void paint(Graphics g)
        {
            Dimension d = getSize();
            int h = getHeight();
            int w = getWidth();
            g.drawRect(0,0,w,h);
            g.drawLine(0,0,w,h);
            g.drawLine(0,h,w,0);
        }
    };

    public CanvasTest1(JPanel panel) {  super(panel); }

    @Override protected Component createComponent1() {
        return new PlainCanvas();
    }

    @Override protected Component createComponent2() {
        return new PlainCanvas();
    }

    public static void main(String[] args) {
        JPanel panel = new JPanel();
        CanvasTest1 test = new CanvasTest1(panel);

        JFrame frame = new JFrame(test.getClass().getName());
        frame.setContentPane(panel);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

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

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

发布评论

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

评论(1

欲拥i 2024-11-17 04:33:13

从 Canvas 更改为 JPanel(如 @tulskiy 建议 - 谢谢!)使其正常工作。

Changing from Canvas to JPanel (as @tulskiy suggested -- thanks!) made it work properly.

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