Java 滚动窗格和 JTextArea
Java Scrollpane and JTextArea
Java 中的滚动窗格再次出现问题。
我只想将 JTextArea 与 ScrollPane 结合起来。我像往常一样做了,但是这次没有用。
这是重要的代码部分(如果您需要更多,请告诉我):
rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER); //does it matter if I call this at the beginning or at the end ?
...
rightUpTextArea = new JTextArea();
rightUpScrollPane = new JScrollPane(rightUpTextArea);
...
rightWindowMaximumWidth.add(rightUpScrollPane, "Up"); //Jpanel
我用很多文本填充了 TextArea,但没有滚动窗格。
也许很明显,但我找不到错误。
编辑:
这是完整的代码(上面的代码在方法 setupRight() 和 setupRightOben() 中):
private void setupRightSide(){
//rightWindow
rightWindow = new JPanel(new BorderLayout(10,10));
rightWindowMaximumWidth = new JPanel();
rightWindowSplit = new BoxLayout(rightWindowMaximumWidth, BoxLayout.Y_AXIS);//"rightWindow" is the parent, not "windowContainer"!!!
rightUp = new JPanel();
rightUpTextArea = new JTextArea();
rightUpScrollPane = new JScrollPane(rightUpTextArea);
rightDownBoxPanel = new JPanel();
rightDownBox = new BoxLayout(rightDownBoxPanel, BoxLayout.X_AXIS);//"rightDownBoxPanel" is the parent, not "rightWindow"!!!
rightDownTextArea = new JTextArea();
rightDownTextAreaScrollPane = new JScrollPane(rightDownTextArea);
rightDownButtomSend = new JButton("Send");
//rightUpScrollPane.setAutoscrolls(true);
windowContainer.add(rightWindow, BorderLayout.CENTER);
//rechte Seite
rightWindow.setPreferredSize(new Dimension(250,250));
rightWindow.setSize(new Dimension(424,503)); // habe ich durch datenausgabe mit der Konsole herrausgefundem
rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER);
//container in "rechte Seite" mit maximaler width von 1000px
rightWindowMaximumWidth.setLayout(rightWindowSplit);
rightWindowMaximumWidth.setPreferredSize(new Dimension(rightWindow.getWidth()-10,1000));
rightWindowMaximumWidth.setMaximumSize(new Dimension(1000,Integer.MAX_VALUE));
rightWindowMaximumWidth.add(rightUpScrollPane, "Up");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
rightWindowMaximumWidth.add(rightDownBoxPanel, "Down");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
setupRechtsOben();
setupRechtsUnten();
}
private void setupRechtsOben(){
//obere Teil der rechten Seite
rightUp.setPreferredSize(new Dimension(420 , 400));
rightUp.setMaximumSize(new Dimension(420,400));
// rightUp.add(rightUpScrollPane);
//TextFeld rechts-oben
rightUpTextArea.setBorder( new CompoundBorder(new javax.swing.border.LineBorder(Color.BLACK), new EmptyBorder(15, 15, 15, 15)));
rightUpTextArea.setEditable(false);
rightUpTextArea.setLineWrap(true);
rightUpTextArea.setWrapStyleWord(true);
rightUpTextArea.setBackground(Color.lightGray);
rightUpTextArea.setPreferredSize(new Dimension(420,350));//-2 damit der rand zu sehen ist
rightUpTextArea.setMaximumSize(rightWindowMaximumWidth.getMaximumSize());
rightUpTextArea.setText("d\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\n"); //to fill it with text
}
private void setupRechtsUnten(){
//untere Teil der rechten Seite (vertikales BoyLayout)
rightDownBoxPanel.setLayout(rightDownBox);
rightDownBoxPanel.add(rightDownTextAreaScrollPane, "TextField");
rightDownBoxPanel.add(Box.createRigidArea(new Dimension(10,0)));
rightDownBoxPanel.add(rightDownButtomSend, "SendButton");
rightDownBoxPanel.setMaximumSize(new Dimension(1000,100));
rightDownBoxPanel.setPreferredSize(new Dimension(1000 ,100));
//TextEingabe im BoxLayout
rightDownTextArea.setBorder(new javax.swing.border.LineBorder(Color.BLACK));
//Damit es in der nächsten Zeile weitergeht, wenn diese voll ist
rightDownTextArea.setLineWrap(true);
rightDownTextArea.setWrapStyleWord(true);
//SendButton im BoxLayout
rightDownButtomSend.setSize(new Dimension(100, 50));
rightDownButtomSend.setPreferredSize(new Dimension(100, 50));
rightDownButtomSend.setMaximumSize(new Dimension(100, 50));
rightDownButtomSend.setFont(new Font("Arial", Font.BOLD, 14));
rightDownButtomSend.setAlignmentX(frame.CENTER_ALIGNMENT);
}
// Below Code is simple Demonstration about How to combine a JTextArea with a ScrollPane.
// I wish this code logic solve your problem.
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Tutorial extends JFrame {
public static void main(String[] args) {
new Tutorial();
}
public Tutorial() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(124, 84, 171, 106);
getContentPane().add(scrollPane);
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Monospaced", Font.BOLD, 15));
scrollPane.setViewportView(textArea);
setVisible(true);
}
}
问题(仍然)不在您发布的代码中,无论是原始代码还是额外代码。我创建了一个 class ,它将所有代码保持原样,为所有没有声明的代码创建实例变量,并实例化代码使用但未初始化的框架。它在文本区域周围创建一个 scrollPane 就好了。如果你需要的话,我把它留在这里供你研究;问题出在别处。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
public class PanelPlay
{
JFrame windowContainer;
JPanel rightWindow;
JPanel rightWindowMaximumWidth;
private BoxLayout rightWindowSplit;
private JPanel rightUp;
private JTextArea rightUpTextArea;
private JScrollPane rightUpScrollPane;
private JPanel rightDownBoxPanel;
private BoxLayout rightDownBox;
private JTextArea rightDownTextArea;
private JScrollPane rightDownTextAreaScrollPane;
private JButton rightDownButtomSend;
public static void main(String ... arguments)
{
PanelPlay pp = new PanelPlay();
pp.go();
}
public void go()
{
windowContainer = new JFrame();
setupRightSide();
windowContainer.pack();
windowContainer.setVisible(true);
}
private void setupRightSide(){
//rightWindow
rightWindow = new JPanel(new BorderLayout(10,10));
rightWindowMaximumWidth = new JPanel();
rightWindowSplit = new BoxLayout(rightWindowMaximumWidth, BoxLayout.Y_AXIS);//"rightWindow" is the parent, not "windowContainer"!!!
rightUp = new JPanel();
rightUpTextArea = new JTextArea();
rightUpScrollPane = new JScrollPane(rightUpTextArea);
rightDownBoxPanel = new JPanel();
rightDownBox = new BoxLayout(rightDownBoxPanel, BoxLayout.X_AXIS);//"rightDownBoxPanel" is the parent, not "rightWindow"!!!
rightDownTextArea = new JTextArea();
rightDownTextAreaScrollPane = new JScrollPane(rightDownTextArea);
rightDownButtomSend = new JButton("Send");
//rightUpScrollPane.setAutoscrolls(true);
windowContainer.add(rightWindow, BorderLayout.CENTER);
//rechte Seite
rightWindow.setPreferredSize(new Dimension(250,250));
rightWindow.setSize(new Dimension(424,503)); // habe ich durch datenausgabe mit der Konsole herrausgefundem
rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER);
//container in "rechte Seite" mit maximaler width von 1000px
rightWindowMaximumWidth.setLayout(rightWindowSplit);
rightWindowMaximumWidth.setPreferredSize(new Dimension(rightWindow.getWidth()-10,1000));
rightWindowMaximumWidth.setMaximumSize(new Dimension(1000,Integer.MAX_VALUE));
rightWindowMaximumWidth.add(rightUpScrollPane, "Up");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
rightWindowMaximumWidth.add(rightDownBoxPanel, "Down");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
setupRechtsOben();
setupRechtsUnten();
}
private void setupRechtsOben(){
//obere Teil der rechten Seite
rightUp.setPreferredSize(new Dimension(420 , 400));
rightUp.setMaximumSize(new Dimension(420,400));
// rightUp.add(rightUpScrollPane);
//TextFeld rechts-oben
rightUpTextArea.setBorder( new CompoundBorder(new javax.swing.border.LineBorder(Color.BLACK), new EmptyBorder(15, 15, 15, 15)));
rightUpTextArea.setEditable(false);
rightUpTextArea.setLineWrap(true);
rightUpTextArea.setWrapStyleWord(true);
rightUpTextArea.setBackground(Color.lightGray);
rightUpTextArea.setPreferredSize(new Dimension(420,350));//-2 damit der rand zu sehen ist
rightUpTextArea.setMaximumSize(rightWindowMaximumWidth.getMaximumSize());
rightUpTextArea.setText("d\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\n"); //to fill it with text
}
private void setupRechtsUnten(){
//untere Teil der rechten Seite (vertikales BoyLayout)
rightDownBoxPanel.setLayout(rightDownBox);
rightDownBoxPanel.add(rightDownTextAreaScrollPane, "TextField");
rightDownBoxPanel.add(Box.createRigidArea(new Dimension(10,0)));
rightDownBoxPanel.add(rightDownButtomSend, "SendButton");
rightDownBoxPanel.setMaximumSize(new Dimension(1000,100));
rightDownBoxPanel.setPreferredSize(new Dimension(1000 ,100));
//TextEingabe im BoxLayout
rightDownTextArea.setBorder(new javax.swing.border.LineBorder(Color.BLACK));
//Damit es in der nächsten Zeile weitergeht, wenn diese voll ist
rightDownTextArea.setLineWrap(true);
rightDownTextArea.setWrapStyleWord(true);
//SendButton im BoxLayout
rightDownButtomSend.setSize(new Dimension(100, 50));
rightDownButtomSend.setPreferredSize(new Dimension(100, 50));
rightDownButtomSend.setMaximumSize(new Dimension(100, 50));
rightDownButtomSend.setFont(new Font("Arial", Font.BOLD, 14));
rightDownButtomSend.setAlignmentX(JFrame.CENTER_ALIGNMENT);
}
}
与您的代码不同,您可以编译它并 运行 它,然后查看创建的 scrollPane。
Java 中的滚动窗格再次出现问题。 我只想将 JTextArea 与 ScrollPane 结合起来。我像往常一样做了,但是这次没有用。
这是重要的代码部分(如果您需要更多,请告诉我):
rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER); //does it matter if I call this at the beginning or at the end ?
...
rightUpTextArea = new JTextArea();
rightUpScrollPane = new JScrollPane(rightUpTextArea);
...
rightWindowMaximumWidth.add(rightUpScrollPane, "Up"); //Jpanel
我用很多文本填充了 TextArea,但没有滚动窗格。 也许很明显,但我找不到错误。
编辑: 这是完整的代码(上面的代码在方法 setupRight() 和 setupRightOben() 中):
private void setupRightSide(){
//rightWindow
rightWindow = new JPanel(new BorderLayout(10,10));
rightWindowMaximumWidth = new JPanel();
rightWindowSplit = new BoxLayout(rightWindowMaximumWidth, BoxLayout.Y_AXIS);//"rightWindow" is the parent, not "windowContainer"!!!
rightUp = new JPanel();
rightUpTextArea = new JTextArea();
rightUpScrollPane = new JScrollPane(rightUpTextArea);
rightDownBoxPanel = new JPanel();
rightDownBox = new BoxLayout(rightDownBoxPanel, BoxLayout.X_AXIS);//"rightDownBoxPanel" is the parent, not "rightWindow"!!!
rightDownTextArea = new JTextArea();
rightDownTextAreaScrollPane = new JScrollPane(rightDownTextArea);
rightDownButtomSend = new JButton("Send");
//rightUpScrollPane.setAutoscrolls(true);
windowContainer.add(rightWindow, BorderLayout.CENTER);
//rechte Seite
rightWindow.setPreferredSize(new Dimension(250,250));
rightWindow.setSize(new Dimension(424,503)); // habe ich durch datenausgabe mit der Konsole herrausgefundem
rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER);
//container in "rechte Seite" mit maximaler width von 1000px
rightWindowMaximumWidth.setLayout(rightWindowSplit);
rightWindowMaximumWidth.setPreferredSize(new Dimension(rightWindow.getWidth()-10,1000));
rightWindowMaximumWidth.setMaximumSize(new Dimension(1000,Integer.MAX_VALUE));
rightWindowMaximumWidth.add(rightUpScrollPane, "Up");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
rightWindowMaximumWidth.add(rightDownBoxPanel, "Down");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
setupRechtsOben();
setupRechtsUnten();
}
private void setupRechtsOben(){
//obere Teil der rechten Seite
rightUp.setPreferredSize(new Dimension(420 , 400));
rightUp.setMaximumSize(new Dimension(420,400));
// rightUp.add(rightUpScrollPane);
//TextFeld rechts-oben
rightUpTextArea.setBorder( new CompoundBorder(new javax.swing.border.LineBorder(Color.BLACK), new EmptyBorder(15, 15, 15, 15)));
rightUpTextArea.setEditable(false);
rightUpTextArea.setLineWrap(true);
rightUpTextArea.setWrapStyleWord(true);
rightUpTextArea.setBackground(Color.lightGray);
rightUpTextArea.setPreferredSize(new Dimension(420,350));//-2 damit der rand zu sehen ist
rightUpTextArea.setMaximumSize(rightWindowMaximumWidth.getMaximumSize());
rightUpTextArea.setText("d\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\n"); //to fill it with text
}
private void setupRechtsUnten(){
//untere Teil der rechten Seite (vertikales BoyLayout)
rightDownBoxPanel.setLayout(rightDownBox);
rightDownBoxPanel.add(rightDownTextAreaScrollPane, "TextField");
rightDownBoxPanel.add(Box.createRigidArea(new Dimension(10,0)));
rightDownBoxPanel.add(rightDownButtomSend, "SendButton");
rightDownBoxPanel.setMaximumSize(new Dimension(1000,100));
rightDownBoxPanel.setPreferredSize(new Dimension(1000 ,100));
//TextEingabe im BoxLayout
rightDownTextArea.setBorder(new javax.swing.border.LineBorder(Color.BLACK));
//Damit es in der nächsten Zeile weitergeht, wenn diese voll ist
rightDownTextArea.setLineWrap(true);
rightDownTextArea.setWrapStyleWord(true);
//SendButton im BoxLayout
rightDownButtomSend.setSize(new Dimension(100, 50));
rightDownButtomSend.setPreferredSize(new Dimension(100, 50));
rightDownButtomSend.setMaximumSize(new Dimension(100, 50));
rightDownButtomSend.setFont(new Font("Arial", Font.BOLD, 14));
rightDownButtomSend.setAlignmentX(frame.CENTER_ALIGNMENT);
}
// Below Code is simple Demonstration about How to combine a JTextArea with a ScrollPane.
// I wish this code logic solve your problem.
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Tutorial extends JFrame {
public static void main(String[] args) {
new Tutorial();
}
public Tutorial() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(124, 84, 171, 106);
getContentPane().add(scrollPane);
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Monospaced", Font.BOLD, 15));
scrollPane.setViewportView(textArea);
setVisible(true);
}
}
问题(仍然)不在您发布的代码中,无论是原始代码还是额外代码。我创建了一个 class ,它将所有代码保持原样,为所有没有声明的代码创建实例变量,并实例化代码使用但未初始化的框架。它在文本区域周围创建一个 scrollPane 就好了。如果你需要的话,我把它留在这里供你研究;问题出在别处。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
public class PanelPlay
{
JFrame windowContainer;
JPanel rightWindow;
JPanel rightWindowMaximumWidth;
private BoxLayout rightWindowSplit;
private JPanel rightUp;
private JTextArea rightUpTextArea;
private JScrollPane rightUpScrollPane;
private JPanel rightDownBoxPanel;
private BoxLayout rightDownBox;
private JTextArea rightDownTextArea;
private JScrollPane rightDownTextAreaScrollPane;
private JButton rightDownButtomSend;
public static void main(String ... arguments)
{
PanelPlay pp = new PanelPlay();
pp.go();
}
public void go()
{
windowContainer = new JFrame();
setupRightSide();
windowContainer.pack();
windowContainer.setVisible(true);
}
private void setupRightSide(){
//rightWindow
rightWindow = new JPanel(new BorderLayout(10,10));
rightWindowMaximumWidth = new JPanel();
rightWindowSplit = new BoxLayout(rightWindowMaximumWidth, BoxLayout.Y_AXIS);//"rightWindow" is the parent, not "windowContainer"!!!
rightUp = new JPanel();
rightUpTextArea = new JTextArea();
rightUpScrollPane = new JScrollPane(rightUpTextArea);
rightDownBoxPanel = new JPanel();
rightDownBox = new BoxLayout(rightDownBoxPanel, BoxLayout.X_AXIS);//"rightDownBoxPanel" is the parent, not "rightWindow"!!!
rightDownTextArea = new JTextArea();
rightDownTextAreaScrollPane = new JScrollPane(rightDownTextArea);
rightDownButtomSend = new JButton("Send");
//rightUpScrollPane.setAutoscrolls(true);
windowContainer.add(rightWindow, BorderLayout.CENTER);
//rechte Seite
rightWindow.setPreferredSize(new Dimension(250,250));
rightWindow.setSize(new Dimension(424,503)); // habe ich durch datenausgabe mit der Konsole herrausgefundem
rightWindow.add(rightWindowMaximumWidth, BorderLayout.CENTER);
//container in "rechte Seite" mit maximaler width von 1000px
rightWindowMaximumWidth.setLayout(rightWindowSplit);
rightWindowMaximumWidth.setPreferredSize(new Dimension(rightWindow.getWidth()-10,1000));
rightWindowMaximumWidth.setMaximumSize(new Dimension(1000,Integer.MAX_VALUE));
rightWindowMaximumWidth.add(rightUpScrollPane, "Up");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
rightWindowMaximumWidth.add(rightDownBoxPanel, "Down");
rightWindowMaximumWidth.add(Box.createRigidArea(new Dimension(0,10)));
setupRechtsOben();
setupRechtsUnten();
}
private void setupRechtsOben(){
//obere Teil der rechten Seite
rightUp.setPreferredSize(new Dimension(420 , 400));
rightUp.setMaximumSize(new Dimension(420,400));
// rightUp.add(rightUpScrollPane);
//TextFeld rechts-oben
rightUpTextArea.setBorder( new CompoundBorder(new javax.swing.border.LineBorder(Color.BLACK), new EmptyBorder(15, 15, 15, 15)));
rightUpTextArea.setEditable(false);
rightUpTextArea.setLineWrap(true);
rightUpTextArea.setWrapStyleWord(true);
rightUpTextArea.setBackground(Color.lightGray);
rightUpTextArea.setPreferredSize(new Dimension(420,350));//-2 damit der rand zu sehen ist
rightUpTextArea.setMaximumSize(rightWindowMaximumWidth.getMaximumSize());
rightUpTextArea.setText("d\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\nd\n"); //to fill it with text
}
private void setupRechtsUnten(){
//untere Teil der rechten Seite (vertikales BoyLayout)
rightDownBoxPanel.setLayout(rightDownBox);
rightDownBoxPanel.add(rightDownTextAreaScrollPane, "TextField");
rightDownBoxPanel.add(Box.createRigidArea(new Dimension(10,0)));
rightDownBoxPanel.add(rightDownButtomSend, "SendButton");
rightDownBoxPanel.setMaximumSize(new Dimension(1000,100));
rightDownBoxPanel.setPreferredSize(new Dimension(1000 ,100));
//TextEingabe im BoxLayout
rightDownTextArea.setBorder(new javax.swing.border.LineBorder(Color.BLACK));
//Damit es in der nächsten Zeile weitergeht, wenn diese voll ist
rightDownTextArea.setLineWrap(true);
rightDownTextArea.setWrapStyleWord(true);
//SendButton im BoxLayout
rightDownButtomSend.setSize(new Dimension(100, 50));
rightDownButtomSend.setPreferredSize(new Dimension(100, 50));
rightDownButtomSend.setMaximumSize(new Dimension(100, 50));
rightDownButtomSend.setFont(new Font("Arial", Font.BOLD, 14));
rightDownButtomSend.setAlignmentX(JFrame.CENTER_ALIGNMENT);
}
}
与您的代码不同,您可以编译它并 运行 它,然后查看创建的 scrollPane。