JScrollPane和JPanel [英] JScrollPane and JPanel
问题描述
因此,我正在制作一种文本编辑器,并且需要JScrollPane进行垂直导航. 但是我无法正常工作.
So, I'm making a kind of text editor, and I need a JScrollPane for vertical navigation. But I can't get it to work.
我已经阅读了Google搜索结果的前十页上的每本怪胎教程,但无法正常工作.
I have read every freaking tutorial on first ten pages of google results, and I can't get it to work.
假设我有JFrame(尺寸为1000x800).我想在其中放置一个JPanel(1000x2000),使其与JFrame水平对齐.我想在JPanel的右侧粘贴一个简单的滚动条,以便可以开始使用它的其余部分.
Lets say I have JFrame (size 1000x800). I want to put a JPanel (1000x2000) in it so that it horizontally alignes with the JFrame. I want to stick a simple scroll bar to the right side of the JPanel so I can get to the rest of it.
我减小了尺寸,我将JPanel添加到JScrollBar中,反之亦然,将其中之一添加到JFrame中,两者都没有,但是什么也没有.
I have reduced sizes, I have added JPanel to JScrollBar and vice versa, added one of them to JFrame, both, none, but nothing.
因此,在这一点上,我不介意几行完成的代码...
So, at this point, I wouldn't mind a couple of lines of finished code...
很好,这是代码...
Fine, here's the code...
mWindow = new JFrame(lang.getString("title"));
mWindow.setSize(1000, 800);
mWindow.setLocationRelativeTo(null);
mWindow.setResizable(false);
mWindow.setLayout(null);
mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mWindow.setVisible(true);
workspace = new JPanel();
workspace.setBounds(0,0, 1000, 1203);
workspace.setBackground(Color.RED);
scroll = new JScrollPane(workspace, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setBounds(0, 20, 600, 600);
//scroll.setLayout(null);
mWindow.getContentPane().add(scroll);
mWindow.repaint();
mWindow.validate();
这显示了JPanel的一部分(600X600,(JScrollPane大小)),并显示了滚动条,但不可滚动
That shows a part of JPanel (600X600, (JScrollPane size)), and shows scrollbars, but isn't scrollable
推荐答案
因此,我进行了这项非常快速的测试,对我来说效果很好...
So, I did this really quick test and it works fine for me...
public class TestPane extends JPanel {
public TestPane() {
setBorder(new LineBorder(Color.RED));
// This is for demonstration purposes only
// One should always rely on the layout manager
// to define this value
// Thank kleopatra
setPreferredSize(new Dimension(1000, 1000));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
FontMetrics fm = g.getFontMetrics();
Dimension size = getPreferredSize();
String text = "Pref: " + size.width + "x" + size.height;
g.drawString(text, 0, fm.getAscent());
size = getSize();
text = "Size: " + size.width + "x" + size.height;
g.drawString(text, 0, fm.getHeight() + fm.getAscent());
}
}
和测试框架
public class TestFrame {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JScrollPane scroll = new JScrollPane(new TestPane());
frame.add(scroll);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
哪个产生这个:
在旁注中,我不知道为什么人们坚持使用null布局,它们只会造成更多的麻烦和心痛,而他们不值得.花时间找到一些简单的布局管理器.由于很多原因,我讨厌VB,但是布局管理是我列表中的第一位,恕我直言
On a side note, I don't know why people insist on using null layouts, they just cause more trouble and heart ache then they're worth. Take the time to find some simple layout managers. I hate VB for a lot of reasons, but layout management is at the top of my list, IMHO
这篇关于JScrollPane和JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!