Java 为什么我的JText区域没有JScrollPanel?
我试图将JScrollPane添加到我的JTextArea,但它不起作用。有人能告诉我怎么了吗?我只想要水平的Java 为什么我的JText区域没有JScrollPanel?,java,swing,Java,Swing,我试图将JScrollPane添加到我的JTextArea,但它不起作用。有人能告诉我怎么了吗?我只想要水平的 import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class Program extends JFrame{ JButton button1, button2; JTextArea textArea1; JScrollPane
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Program extends JFrame{
JButton button1, button2;
JTextArea textArea1;
JScrollPane scroller;
public static String directory, nameOfFile, finalPath;
public static void main(String[] args){
new Program();
}
public Program(){
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("swinging");
JPanel thePanel = new JPanel();
thePanel.setLayout(null);
thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
button1 = new JButton("Wybierz plik:");
button2 = new JButton("Dodaj");
final JTextField text = new JTextField(24);
textArea1 = new JTextArea();
textArea1.setEditable(true);
textArea1.setLineWrap(true);
textArea1.setPreferredSize(new Dimension(490,200));
scroller = new JScrollPane(textArea1);
scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
thePanel.add(button1);
thePanel.add(text);
thePanel.add(button2);
thePanel.add(textArea1);
thePanel.add(scroller);
this.add(thePanel);
this.setVisible(true);
}
}
添加后,我看到的只是文本区域下方的一些小正方形。感谢您的反馈您的问题是由于您将JScrollPane和JTextArea都添加到
面板
JPanel中,因此您可以看到这两个面板:一个没有JScrollPanes的JTextArea,一个空JScrollPane
- 不要将textArea本身添加到JScrollPane和JPanel中,因为您只能将组件添加到一个容器中。相反,将其添加到一个组件,JScrollPane(实际上您正在将其添加到其视口视图中,但是如果您将其传递到JScrollPane的构造函数中,那么您正在执行此操作),然后将该JScrollPane添加到其他组件中
- 另外,从不设置文本组件的首选大小。您可以约束JTextArea,这样它就不会随着更多文本的添加而扩展,因此您永远不会看到滚动条,也不会看到超过此大小的文本。改为设置可见的列和行。e、 例如,
textArea1=新的JTextArea(行、列)代码>
thePanel.setLayout(null);
thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
我不确定您在这里想做什么,因为1)您只想设置一次容器的布局,2)一般来说,您希望避免使用null
布局
例如:
import java.awt.BorderLayout;
import javax.swing.*;
public class MyProgram extends JPanel {
private static final int T_FIELD_COLS = 20;
private static final int TXT_AREA_ROWS = 15;
private static final int TXT_AREA_COLS = 20;
private JButton button1 = new JButton("Button 1");
private JButton button2 = new JButton("Button 2");
private JTextField textField = new JTextField(T_FIELD_COLS);
private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS);
public MyProgram() {
// Create a JPanel to hold your top line of components
JPanel topPanel = new JPanel();
int gap = 3;
topPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
// set this JPanel's layout. Here I use BoxLayout.
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
topPanel.add(button1);
topPanel.add(Box.createHorizontalStrut(gap));
topPanel.add(textField);
topPanel.add(Box.createHorizontalStrut(gap));
topPanel.add(button2);
// so the JTextArea will wrap words
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
// add the JTextArea to the JScrollPane's viewport:
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// set the layout of the main JPanel.
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
private static void createAndShowGui() {
MyProgram mainPanel = new MyProgram();
JFrame frame = new JFrame("My Program");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack(); // don't set the JFrame's size, preferred size or bounds
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
// start your program on the event thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您的问题是由于您将JScrollPane和JTextArea都添加到
thePanel
JPanel中,因此您可以同时看到这两个:一个不带JScrollPanes的JTextArea和一个空JScrollPane
- 不要将textArea本身添加到JScrollPane和JPanel中,因为您只能将组件添加到一个容器中。相反,将其添加到一个组件,JScrollPane(实际上您正在将其添加到其视口视图中,但是如果您将其传递到JScrollPane的构造函数中,那么您正在执行此操作),然后将该JScrollPane添加到其他组件中
- 另外,从不设置文本组件的首选大小。您可以约束JTextArea,这样它就不会随着更多文本的添加而扩展,因此您永远不会看到滚动条,也不会看到超过此大小的文本。改为设置可见的列和行。e、 例如,
textArea1=新的JTextArea(行、列)代码>
thePanel.setLayout(null);
thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
我不确定您在这里想做什么,因为1)您只想设置一次容器的布局,2)一般来说,您希望避免使用null
布局
例如:
import java.awt.BorderLayout;
import javax.swing.*;
public class MyProgram extends JPanel {
private static final int T_FIELD_COLS = 20;
private static final int TXT_AREA_ROWS = 15;
private static final int TXT_AREA_COLS = 20;
private JButton button1 = new JButton("Button 1");
private JButton button2 = new JButton("Button 2");
private JTextField textField = new JTextField(T_FIELD_COLS);
private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS);
public MyProgram() {
// Create a JPanel to hold your top line of components
JPanel topPanel = new JPanel();
int gap = 3;
topPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
// set this JPanel's layout. Here I use BoxLayout.
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
topPanel.add(button1);
topPanel.add(Box.createHorizontalStrut(gap));
topPanel.add(textField);
topPanel.add(Box.createHorizontalStrut(gap));
topPanel.add(button2);
// so the JTextArea will wrap words
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
// add the JTextArea to the JScrollPane's viewport:
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// set the layout of the main JPanel.
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
private static void createAndShowGui() {
MyProgram mainPanel = new MyProgram();
JFrame frame = new JFrame("My Program");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack(); // don't set the JFrame's size, preferred size or bounds
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
// start your program on the event thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
试试这个:
试试这个:
textArea1.setPreferredSize(新维度(490200))代码>--永远不要这样做。认真地您可以约束JTextArea,这样它就不会随着更多文本的添加而扩展,因此您永远不会看到滚动条,也不会看到超过此大小的文本。改为设置可见的列和行。接下来,将textArea本身添加到JScrollPane和JPanel(??)。不。将它添加到一个组件JScrollPane,然后将该JScrollPane添加到其他组件。我已经回滚了您的问题代码。你在做的时候对答案进行了彻底的修改,这是在否定答案。请参阅我的答案中的示例代码。您的问题是由于您将JScrollPane和JTextArea都添加到thePanel
JPanel中,因此您可以看到这两个区域:一个没有JScrollPanes的JTextArea和一个空的JScrollPane。同样,根据我的回答,您只想将JTextArea添加到这个中。textArea1.setPreferredSize(新维度(490200))代码>--永远不要这样做。认真地您可以约束JTextArea,这样它就不会随着更多文本的添加而扩展,因此您永远不会看到滚动条,也不会看到超过此大小的文本。改为设置可见的列和行。接下来,将textArea本身添加到JScrollPane和JPanel(??)。不。将它添加到一个组件JScrollPane,然后将该JScrollPane添加到其他组件。我已经回滚了您的问题代码。你在做的时候对答案进行了彻底的修改,这是在否定答案。请参阅我的答案中的示例代码。您的问题是由于您将JScrollPane和JTextArea都添加到thePanel
JPanel中,因此您可以看到这两个区域:一个没有JScrollPanes的JTextArea和一个空的JScrollPane。同样,根据我的回答,您只想将JTextArea添加到这个中?将代码转储到未预料到的人身上并不像看起来那么有用,一些解释为什么你的方法可以帮助OP解决他们的问题不仅可以帮助他们避免将来犯同样的错误,而且还可以帮助其他可能有类似问题的人“尝试一下这个:”-为什么?将代码转储到未预料到的人身上并不像看上去那么有用,解释一下为什么您的方法可以帮助OP解决他们的问题,不仅可以帮助他们避免将来犯同样的错误,还可以帮助其他可能有类似问题的人