我试图运行Java Swing 2nd Edition 7.1.3 ScrollPaneLayout中的示例。图像和滚动工作得很好,但统治者没有。这是我的代码:
public class ScrollPaneLayoutDemo extends JFrame{
JLabel label = new JLabel(new ImageIcon("img.jpg"));
public ScrollPaneLayoutDemo() {
super("ScrollPaneLayout Demo");
JScrollPane jsp = new JScrollPane(label);
JLabel[] corners = new JLabel[4];
for(int i=0; i<4; i++)
{
corners[i] = new JLabel();
corners[i].setBackground(Color.YELLOW);
corners[i].setOpaque(true);
corners[i].setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2), BorderFactory.createLineBorder(Color.RED, 1)));
}
JLabel rowheader = new JLabel() {
Font f = new Font("Serif", Font.ITALIC | Font.BOLD, 10);
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Rectangle r = g.getClipBounds();
g.setFont(f);
g.setColor(Color.RED);
for (int i = 50 - (r.y % 50); i < r.height; i += 50) {
g.drawLine(0, r.y + i, 3, r.y + i);
g.drawString("" + (r.y + i), 6, r.y + i + 3);
}
}
public Dimension getPreferredSize()
{
return new Dimension(25, (int) label.getPreferredSize().getHeight());
}
};
rowheader.setBackground(Color.YELLOW);
rowheader.setOpaque(true);
JLabel columnheader = new JLabel() {
Font f = new Font("Serif", Font.ITALIC | Font.BOLD, 10);
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Rectangle r = g.getClipBounds();
g.setFont(f);
g.setColor(Color.RED);
for (int i = 50 - (r.x % 50); i < r.width; i += 50)
{
g.drawLine(r.x + i, 0, r.x + i, 3);
g.drawString("" + (r.x + i), r.x + i - 10, 16);
}
}
public Dimension getPreferredSize()
{
return new Dimension((int) label.getPreferredSize().getWidth(),25);
}
};
columnheader.setBackground(Color.YELLOW);
columnheader.setOpaque(true);
jsp.setRowHeaderView(rowheader);
jsp.setColumnHeaderView(columnheader);
jsp.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
jsp.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
jsp.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
jsp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);
getContentPane().add(jsp, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args) {
new JScrollPaneDemo();
}
}
如果有人能帮我解决这个问题,我很感激。
答案 0 :(得分:3)
来自child的维度必须大于JViewports Dimension(来自JScrollPane的可见矩形),然后可以看到JScrollBars或自定义装饰,更多内容在Oracle教程中如何使用ScrollPanes
搜索@Annotations
e.g。
不知道为什么,但我无法在此处添加图片: - )
这
import java.awt.*;
import javax.swing.*;
public class ScrollPaneLayoutDemo extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel label = new JLabel(new ImageIcon("img.jpg")) {
@Override
public Dimension getPreferredSize() {
return new Dimension(new Dimension(800, 600));/*icon.getIconWidth(), icon.getIconHeight()*/
}
};
public ScrollPaneLayoutDemo() {
super("ScrollPaneLayout Demo");
label.setPreferredSize(new Dimension(800, 600));
JScrollPane jsp = new JScrollPane(label);
JLabel[] corners = new JLabel[4];
for (int i = 0; i < 4; i++) {
corners[i] = new JLabel();
corners[i].setBackground(Color.YELLOW);
corners[i].setOpaque(true);
corners[i].setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2), BorderFactory.createLineBorder(Color.RED, 1)));
}
JLabel rowheader = new JLabel() {
private static final long serialVersionUID = 1L;
Font f = new Font("Serif", Font.ITALIC | Font.BOLD, 10);
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle r = g.getClipBounds();
g.setFont(f);
g.setColor(Color.RED);
for (int i = 50 - (r.y % 50); i < r.height; i += 50) {
g.drawLine(0, r.y + i, 3, r.y + i);
g.drawString("" + (r.y + i), 6, r.y + i + 3);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(25, (int) label.getPreferredSize().getHeight());
}
};
rowheader.setBackground(Color.YELLOW);
rowheader.setOpaque(true);
JLabel columnheader = new JLabel() {
private static final long serialVersionUID = 1L;
Font f = new Font("Serif", Font.ITALIC | Font.BOLD, 10);
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle r = g.getClipBounds();
g.setFont(f);
g.setColor(Color.RED);
for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
g.drawLine(r.x + i, 0, r.x + i, 3);
g.drawString("" + (r.x + i), r.x + i - 10, 16);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension((int) label.getPreferredSize().getWidth(), 25);
}
};
columnheader.setBackground(Color.YELLOW);
columnheader.setOpaque(true);
jsp.setRowHeaderView(rowheader);
jsp.setColumnHeaderView(columnheader);
jsp.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
jsp.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
jsp.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
jsp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);
getContentPane().add(jsp, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ScrollPaneLayoutDemo();
}
});
}
}