java - 与 JSplitPane 斗争

标签 java swing layout paintcomponent jsplitpane

好吧,我有一些关于使用拆分 Pane 将我的框架分成两个区域的技巧,但我无法让它显示一些有用的东西。代码如下所示:

public class Whiteboard extends JPanel {

int width = 600;
int sidePanelWidth = 200;
int lineHeight = 120;
int numberOfLines = 5;
JFrame frame = null;
Glyph glyph = null;
//java.awt.Rectangle bounds = new java.awt.Rectangle();
Bounds bounds = null;
JSplitPane splitPane = null;
JPanel tools = null;


public Whiteboard() {
    frame = new JFrame();
    frame.setSize(width + sidePanelWidth, getFullHeight());
    FlowLayout simpleLayout = new FlowLayout();
    frame.setLayout(simpleLayout);

    tools = new JPanel();
    tools.setSize(new Dimension(sidePanelWidth, getFullHeight()));
    this.setSize(width, getFullHeight());

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this, tools);
    splitPane.setPreferredSize(new Dimension(width + sidePanelWidth, getFullHeight()));
    splitPane.setOneTouchExpandable(false);
    splitPane.setDividerLocation(150);

    frame.add(splitPane);
    this.setBackground(Color.white);
    java.awt.Rectangle rectBounds = this.getBounds();
    bounds = new Bounds((int)rectBounds.getX(), (int)rectBounds.getY(), (int)(rectBounds.getX() + rectBounds.getWidth()), (int)(rectBounds.getY() + rectBounds.getHeight()));

}

public int getFullHeight() {
    return lineHeight * numberOfLines;
}

我现在把代码改成这样:

    public static void main(String[] args) {
    int sidePanelWidth = 200;

    JFrame frame = new JFrame();        
    Whiteboard whiteboard = new Whiteboard();         

    JPanel sidePanel = new JPanel();
    sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight()));


    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.add(whiteboard, JSplitPane.LEFT);
    splitPane.add(sidePanel, JSplitPane.RIGHT);

    frame.setLayout(new FlowLayout());
    frame.getContentPane().add(splitPane);


    frame.pack();
    frame.setVisible(true);        
    whiteboard.repaint();
}

以及这个的构造函数:

public Whiteboard() {
    this.setPreferredSize(new Dimension(width, getFullHeight()));
    this.setBackground(Color.red);   

现在不知道是哪里出了问题,可能是没有调用paintComponent方法吧。我尝试通过调用 repaint() 来强制它,但它没有帮助,它只是不调用这个组件

编辑:好吧,现在看来它毕竟是在调用 paintComponent 方法,但我仍然得到这样的屏幕: enter image description here

如您所见,它看起来不太好。那么我目前主要方法的代码:

public static void main(String[] args) {
    int sidePanelWidth = 200;

    JFrame frame = new JFrame();        
    Whiteboard whiteboard = new Whiteboard();         

    JPanel sidePanel = new JPanel();
    sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight()));


    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.add(whiteboard, JSplitPane.LEFT);
    splitPane.add(sidePanel, JSplitPane.RIGHT);

    frame.setLayout(new FlowLayout());
    frame.getContentPane().add(splitPane);

    frame.pack();
    frame.setVisible(true);        
    whiteboard.repaint();
}

知道如何更改它来解决问题吗?我需要发布其他方法吗?

最佳答案

JPanel 的构造函数中创建一个 JFrame 真的不应该这样做。

这是我创建的示例:

Screen shot

import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;

public class JavaApplication100 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JavaApplication100().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(Container contentPane) {
        JPanel mainPanel = new JPanel();

        //create our 2 seperate panels (could be custom ones)
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();

        //add labels for viewing
        leftPanel.add(new JLabel("LEFT"));
        rightPanel.add(new JLabel("RIGHT"));

        //just so you can see em or they would be small
        leftPanel.setPreferredSize(new Dimension(300, 300));
        rightPanel.setPreferredSize(new Dimension(300, 300));

        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        //add panels to split pane
        jsp.add(rightPanel, JSplitPane.RIGHT);
        jsp.add(leftPanel, JSplitPane.LEFT);

        mainPanel.add(jsp);//add splitpane to mainpanel
        contentPane.add(mainPanel);

    }
}

编辑/更新:

根据您的评论,如果您想在扩展 JPanelWhiteBoard 中为背景覆盖 paintComponent(Graphics g) 着色,如下所示:

public class WhiteBoard extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}

关于java - 与 JSplitPane 斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12482786/

相关文章:

java - GWT 上的 JsonP 响应代码

java - 如何设置垂直排列的元素之间的距离?

css - 如何在基于 Bootstrap 的固定宽度网站上启用响应能力?

java - scala 类中的 println 不会在 STDOUT 上显示输出

java - 在 JAVA 中将列表对象添加到另一个列表中

java - spring 3、hibernate 3、maven 和 mysql 的集成

java - JComboBox 的问题

java - 构建我的 Java Swing 应用程序。 IE。何时何地使用类和不使用类

java - 你能帮我解决一些 SWT 列表布局和渲染问题吗

python - wxpython + matplotlib : autoresizing a matplotlib figure