Java 为什么 JTextArea 可以滚动,但不能滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18859204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why might a JTextArea made scrollable, fail to be scrollable?
提问by Eric Lang
Does anyone see an issue with my code that might prevent JTextArea to become scrollable when you query data which expands it's bounds, it also seems that some words are getting cut in half, but the JScrollPane, which is supposedly supposed to fix the issue is failing at doing so.
有没有人看到我的代码存在一个问题,当您查询扩展其边界的数据时,该问题可能会阻止 JTextArea 变为可滚动,似乎有些单词被切成两半,但是应该可以解决该问题的 JScrollPane 失败了在这样做。
package guiprojj;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import javax.swing.*;
import com.eclipsesource.json.JsonObject;
import com.google.gson.JsonParser;
import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;
public class gui {
public static void main(String[] args)
{
JFrame maingui = new JFrame("Gui");
JButton enter = new JButton("Enter");
final JTextArea movieinfo = new JTextArea(5,20);
final JTextField movietext = new JTextField(16);
final JScrollPane scrolll = new JScrollPane(movieinfo);
JPanel pangui = new JPanel();
pangui.add(movietext);
pangui.add(enter);
scrolll.add(movieinfo);
pangui.add(movieinfo);
maingui.setResizable(false);
maingui.setVisible(true);
movieinfo.setLineWrap(true);
movieinfo.setEditable(false);
maingui.add(pangui);
scrolll.getPreferredSize();
//pangui.setPreferredSize(new Dimension(300, 150));
//pangui.add(scrolll, BorderLayout.CENTER);
//movieinfo.add(scrolll);
maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
maingui.pack();
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println(Test.getMovieInfo(movietext.getText()));
JsonParserFactory factory=JsonParserFactory.getInstance();
JSONParser parser=factory.newJsonParser();
Map jsonData=parser.parseJson(Test.getMovieInfo(movietext.getText()));
String Title = (String)jsonData.get("Title");
String Year = (String)jsonData.get("Year");
String Plot = (String)jsonData.get("Plot");
movieinfo.setText("Title: "+Title+"\nYear: "+ Year +"\nPlot: "+Plot);
}
});
}
}
采纳答案by camickr
final JTextArea movieinfo = new JTextArea(5,20);
final JScrollPane scrolll = new JScrollPane(movieinfo);
JPanel pangui = new JPanel();
//scrolll.add(movieinfo);
//pangui.add(movieinfo);
pangui.add(scroll);
A component can only have a single parent. You create the scrollpane with the text area which is good. But then you add the textarea to the panel, which is bad since the text area gets removed from the scrollpane).
一个组件只能有一个父级。您创建带有文本区域的滚动窗格,这很好。但是随后您将文本区域添加到面板中,这很糟糕,因为文本区域已从滚动窗格中删除)。
You need to add the scrollpane to the panel since this is the component that contains the text area.
您需要将滚动窗格添加到面板,因为这是包含文本区域的组件。
回答by JNL
I think you should use a ScrollPane.
我认为您应该使用ScrollPane。
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);