Java JPanel内部的JScrollPane内部的GridBagLayout不';不能正常滚动
我有一个Java JPanel内部的JScrollPane内部的GridBagLayout不';不能正常滚动,java,swing,Java,Swing,我有一个JPanel在JScrollPane在GridBagLayout 这是不正确的滚动。应该发生的是,每次 按下xxx按钮,滚动窗格内会添加一行新行。 实际情况是,如果按下xxx,比如说10次,只有 显示前七行,其余的无法滚动到。谁能 建议对下面的源代码进行更改,以使滚动正常进行 适当地?我花了几个小时在这上面,但没有成功,我尝试了所有的策略 通过网络 注: 文本在paintComponent中拆分,因为drawString会 不处理行尾字符 JPanel内部的JScrollPane内部的G
JPanel
在JScrollPane
在GridBagLayout
这是不正确的滚动。应该发生的是,每次
按下xxx按钮,滚动窗格内会添加一行新行。
实际情况是,如果按下xxx,比如说10次,只有
显示前七行,其余的无法滚动到。谁能
建议对下面的源代码进行更改,以使滚动正常进行
适当地?我花了几个小时在这上面,但没有成功,我尝试了所有的策略
通过网络
注:
paintComponent
中拆分,因为drawString
会
不处理行尾字符JPanel
内部的JScrollPane
内部的GridBagLayout
配置是一个更大系统的必要组成部分
一个有同样问题的软件,所以我把它保存在这里import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class question {
public static void main(String[] args) {
new DrawingGUI();
}
private static class ScrollingPane extends JPanel {
private static final long serialVersionUID = 1L;
String text;
public ScrollingPane() {
setPreferredSize(new Dimension(300, 100));
text = "";
}
public void SetText(String text_x) {text = text + System.lineSeparator() + text_x;}
public void paintComponent(Graphics g) {
super.paintComponent(g);
FontMetrics fm = g.getFontMetrics();
int y = -fm.getHeight();
for (String text : text.split("\n"))
g.drawString(text, 0, y += fm.getHeight());
}
}
static class DrawingGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel jp;
JScrollPane js;
ScrollingPane sp;
int LineNum;
DrawingGUI() {
LineNum = 0;
JFrame frame = new JFrame("xxx");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setSize(800,800);
frame.setVisible(true);
}
public void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button = new JButton("xxx");
button.setActionCommand("add_text");
button.addActionListener(this);
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
jp = new JPanel();
c.gridx = 0;
c.gridy = 1;
c.ipadx = 600;
c.ipady = 450;
pane.add(jp, c);
sp = new ScrollingPane();
js = new JScrollPane(sp);
js.getViewport().setPreferredSize(new Dimension(300,100));
c.gridx = 0;
c.gridy = 2;
c.ipady = 50;
pane.add(js, c);
}
public void actionPerformed(ActionEvent e) {
if ("add_text".equals(e.getActionCommand())) {
LineNum++;
sp.SetText("LineNum = " + LineNum);
sp.revalidate();
sp.repaint();
}
}
}
}
您的ScrollingPane JPanel无法使用以下行正确调整自身大小:
setPreferredSize(new Dimension(300, 100));
这将修复滚动窗格JPanel的大小。我发现您有几种可能的解决方案:
revalidate()
和repaint()
private static class ScrollingPane2 extends JPanel {
private static final long serialVersionUID = 1L;
private JTextArea textArea = new JTextArea(6, 20);
public ScrollingPane2() {
setLayout(new BorderLayout());
add(textArea, BorderLayout.CENTER);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(null);
}
public void SetText(String text_x) {
textArea.append(text_x + "\n");
}
}
private static class ScrollingPane3 extends JPanel {
private static final long serialVersionUID = 1L;
private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> jList = new JList<>(listModel);
public ScrollingPane3() {
setLayout(new BorderLayout());
add(jList, BorderLayout.CENTER);
jList.setBackground(null);
}
public void SetText(String text_x) {
listModel.addElement(text_x);
}
}
私有静态类ScrollingPane2扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私有JTextArea textArea=新JTextArea(6,20);
公共滚动窗格2(){
setLayout(新的BorderLayout());
添加(textArea,BorderLayout.CENTER);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(空);
}
公共void SetText(字符串text_x){
textArea.append(text_x+“\n”);
}
}
私有静态类ScrollingPane3扩展了JPanel{
私有静态最终长serialVersionUID=1L;
private DefaultListModel listModel=新的DefaultListModel();
私有JList JList=新JList(列表模型);
公共滚动窗格3(){
setLayout(新的BorderLayout());
添加(jList,BorderLayout.CENTER);
jList.setBackground(空);
}
公共void SetText(字符串text_x){
listModel.addElement(text_x);
}
}
您正在使ScrollingPane JPanel的功能短路,无法使用以下行正确调整自身大小:
setPreferredSize(new Dimension(300, 100));
这将修复滚动窗格JPanel的大小。我发现您有几种可能的解决方案:
revalidate()
和repaint()
private static class ScrollingPane2 extends JPanel {
private static final long serialVersionUID = 1L;
private JTextArea textArea = new JTextArea(6, 20);
public ScrollingPane2() {
setLayout(new BorderLayout());
add(textArea, BorderLayout.CENTER);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(null);
}
public void SetText(String text_x) {
textArea.append(text_x + "\n");
}
}
private static class ScrollingPane3 extends JPanel {
private static final long serialVersionUID = 1L;
private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> jList = new JList<>(listModel);
public ScrollingPane3() {
setLayout(new BorderLayout());
add(jList, BorderLayout.CENTER);
jList.setBackground(null);
}
public void SetText(String text_x) {
listModel.addElement(text_x);
}
}
私有静态类ScrollingPane2扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私有JTextArea textArea=新JTextArea(6,20);
公共滚动窗格2(){
setLayout(新的BorderLayout());
添加(textArea,BorderLayout.CENTER);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(空);
}
公共void SetText(字符串text_x){
textArea.append(text_x+“\n”);
}
}
私有静态类ScrollingPane3扩展了JPanel{
私有静态最终长serialVersionUID=1L;
private DefaultListModel listModel=新的DefaultListModel();
私有JList JList=新JList(列表模型);
公共滚动窗格3(){
setLayout(新的BorderLayout());
添加(jList,BorderLayout.CENTER);
jList.setBackground(空);
}
公共void SetText(字符串text_x){
listModel.addElement(text_x);
}
}
请参见编辑以回答…………您好?谢谢。滚动窗格2正是我所需要的。请参阅编辑以回答…………您好?谢谢。滚动窗格2正是我所需要的。