在 JSplitPane 上绘图不显示
Drawing on JSplitPane not showing
只是想在屏幕上画一些线条。
- 我已经检查以确保所有相关代码都在 运行
- 我试过调用重绘(并确保正在 运行)
- 由于这是一个JSplitPane,布局必须是JSplitPane布局
- 我正在设置颜色以确保它不是使用背景颜色绘制的。
- 我检查了高度和宽度以确保其大小不为 0 或其他值
- 我也试过画文字;同样的结果
- 我已经改变了所有地方的坐标,尝试了任意值和比例值
或者至少我认为。 Swing 非常古怪。我会使用 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);
}
}
简短的回答,不要从 JScrollPane
扩展,JScrollPane
包含称为 JViewport
的单个组件,它覆盖了滚动窗格的大部分(其余部分已占用)上升 JScrollBar
s
相反,请尝试从 JPanel
.
之类的内容进行扩展
我还建议你不要在你的绘画代码中使用任何类似 int xEnd = getParent().getWidth() - 30;
的东西,Graphics
上下文被转换到组件位置,使 top/left 角 0x0
并裁剪到组件的当前宽度和高度
只是想在屏幕上画一些线条。
- 我已经检查以确保所有相关代码都在 运行
- 我试过调用重绘(并确保正在 运行)
- 由于这是一个JSplitPane,布局必须是JSplitPane布局
- 我正在设置颜色以确保它不是使用背景颜色绘制的。
- 我检查了高度和宽度以确保其大小不为 0 或其他值
- 我也试过画文字;同样的结果
- 我已经改变了所有地方的坐标,尝试了任意值和比例值
或者至少我认为。 Swing 非常古怪。我会使用 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);
}
}
简短的回答,不要从 JScrollPane
扩展,JScrollPane
包含称为 JViewport
的单个组件,它覆盖了滚动窗格的大部分(其余部分已占用)上升 JScrollBar
s
相反,请尝试从 JPanel
.
我还建议你不要在你的绘画代码中使用任何类似 int xEnd = getParent().getWidth() - 30;
的东西,Graphics
上下文被转换到组件位置,使 top/left 角 0x0
并裁剪到组件的当前宽度和高度