在JSplitPane上绘图没有显示

时间:2015-04-10 05:36:56

标签: java swing jsplitpane

试图在屏幕上绘制一些线条。

  • 我已经检查过以确保所有相关代码都在运行
  • 我尝试过调用重绘(并确保正在运行)
  • 由于这是一个JSplitPane,因此布局必须是JSplitPane布局
  • 我正在设置颜色,以确保它不使用背景颜色进行绘制。
  • 我检查了高度和宽度,以确保其大小不是0或
  • 我也尝试过绘制文字;相同的结果
  • 我已经改变了整个地方的坐标,尝试了任意和比例值

或者至少我认为。摇摆是不直观的古怪。我使用AWT,但我需要Swing提供的特性。无论如何,代码。它只是一个分割窗格,实际上显示 - 可调整大小和全部 - 但顶部窗格(我试图放入任何东西的唯一一个)的内容不显示。

package derange;

import java.awt.Dimension;


import java.awt.GridLayout;

import javax.swing.*;        

public class Derange {

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Derange");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Display the window.
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.pack();
        frame.setVisible(true);


        //Create a split pane with the two scroll panes in it.
        PanelScore scorePane = new PanelScore();
        JScrollPane instrumentPane = new JScrollPane();
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                   scorePane, instrumentPane);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation((frame.getHeight() / 4) * 3 );// Three-quarters of the way down
        splitPane.setDividerSize(20);

        //Provide minimum sizes for the two components in the split pane
        Dimension minimumSize = new Dimension(frame.getWidth(), frame.getHeight()/ 2);//width, height
        scorePane.setMinimumSize(minimumSize); //Score takes up at least half the screen
        instrumentPane.setMinimumSize(new Dimension(0,0));//no minimum size on the instrument panel; collapsible

        frame.getContentPane().add(splitPane);
    }


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

package derange;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JScrollPane;

@SuppressWarnings("serial")//wtf is this needed for?

public class PanelScore extends JScrollPane{

    public int strings = 6;

    public void drawStaffTablature(Graphics g){
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.black);

        int xStart = 30;//insets.left;
        int xEnd = getParent().getWidth() - 30;
        int yCoord = this.getHeight() / 2;
        System.out.println(this.isShowing());

        //Space between tablature lines
        int lineSpacing = 15;
        //Space between staffs.
        int staffSpacing = 60;`enter code here`
        for(int x = 0; x < strings; x++){
            g2d.drawLine(xStart, yCoord + (lineSpacing * x), xEnd, yCoord + (lineSpacing * x));
            //System.out.println("String: " + (x + 1));
             g.drawString("Test", xStart, yCoord); //change the co-odrinates
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawStaffTablature(g);
    }
}

1 个答案:

答案 0 :(得分:2)

简短回答,不要从JScrollPane延伸,JScrollPane包含称为JViewport的单个组件,它涵盖了滚动窗格的大部分内容(其余部分都是由JScrollBar s

提升

enter image description here

相反,请尝试从JPanel

等扩展

我还建议您不要在您的彩绘代码中使用int xEnd = getParent().getWidth() - 30;之类的内容,Graphics上下文会转换为组件位置,从而形成左上角0x0并剪裁到组件当前的宽度和高度