在 JScrollpane 中放置 JPanel
Placing JPanels in JScrollpane
我无法将 JScrollPane 中的 JPanel 一个放在另一个下面。我什至在 JScrollPane 中显示面板都没有成功,当我显示它们时,它们会全部挤在一起。
paintComponent 似乎也被完全忽略了。
您可以看到我正在尝试重新创建 Microsoft PowerPoint 之类的东西。
这是代码
public class PresentationView extends JPanel {
private Presentation presentation;
private JScrollPane jsp;
private BoxLayout layout;
public PresentationView(Presentation presentation){
this.presentation = presentation;
jsp = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
layout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(layout);
for(RuNode s: presentation.getChildren()){
jsp.add(new SlideView((Slide) s));
System.out.println(s.getName());
}
add(jsp);
}
}
这是 SlideView class
public class SlideView extends JPanel {
private Slide slide;
private Image img;
public SlideView(Slide slide){
this(new ImageIcon(((Presentation)slide.getParent()).getImgPath()).getImage());
setMaximumSize(new Dimension(400, 400));
setMinimumSize(new Dimension(400, 400));
setBackground(Color.PINK);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
this.slide = slide;
}
public SlideView(Image img){
this.img = img;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, (int)(this.getSize().getWidth()-img.getWidth(null))/2,
(int)(this.getSize().getHeight()-img.getHeight(null))/2, null);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
setLayout(new BorderLayout());
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
for (int index = 0; index < 100; index++) {
contentPane.add(new SlidePane());
}
JScrollPane sp = new JScrollPane(contentPane);
add(sp);
}
}
public class SlidePane extends JPanel {
public SlidePane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Color c1 = new Color(0, 0, 255);
Color c2 = new Color(0, 255, 255);
GradientPaint gp = new GradientPaint(0, 0, c1, 0, getHeight(), c2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setPaint(null);
g2d.setColor(Color.BLUE);
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g2d.dispose();
}
}
}
我无法将 JScrollPane 中的 JPanel 一个放在另一个下面。我什至在 JScrollPane 中显示面板都没有成功,当我显示它们时,它们会全部挤在一起。 paintComponent 似乎也被完全忽略了。
您可以看到我正在尝试重新创建 Microsoft PowerPoint 之类的东西。
这是代码
public class PresentationView extends JPanel {
private Presentation presentation;
private JScrollPane jsp;
private BoxLayout layout;
public PresentationView(Presentation presentation){
this.presentation = presentation;
jsp = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
layout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(layout);
for(RuNode s: presentation.getChildren()){
jsp.add(new SlideView((Slide) s));
System.out.println(s.getName());
}
add(jsp);
}
}
这是 SlideView class
public class SlideView extends JPanel {
private Slide slide;
private Image img;
public SlideView(Slide slide){
this(new ImageIcon(((Presentation)slide.getParent()).getImgPath()).getImage());
setMaximumSize(new Dimension(400, 400));
setMinimumSize(new Dimension(400, 400));
setBackground(Color.PINK);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
this.slide = slide;
}
public SlideView(Image img){
this.img = img;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, (int)(this.getSize().getWidth()-img.getWidth(null))/2,
(int)(this.getSize().getHeight()-img.getHeight(null))/2, null);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
setLayout(new BorderLayout());
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
for (int index = 0; index < 100; index++) {
contentPane.add(new SlidePane());
}
JScrollPane sp = new JScrollPane(contentPane);
add(sp);
}
}
public class SlidePane extends JPanel {
public SlidePane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Color c1 = new Color(0, 0, 255);
Color c2 = new Color(0, 255, 255);
GradientPaint gp = new GradientPaint(0, 0, c1, 0, getHeight(), c2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setPaint(null);
g2d.setColor(Color.BLUE);
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g2d.dispose();
}
}
}