我看到了一些像这个问题的问题,但我无法解决这个问题。我无法在JScrollPane
上看到JTextArea
。任何人都可以指出我犯了哪些错误?感谢。
package experiement;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Experiment extends JFrame{
public Experiment(){
JTextArea tarea=new JTextArea();
tarea.setBounds(100,100,200,200);
JScrollPane pan= new JScrollPane();
pan.setPreferredSize(new Dimension(100,100));
pan=new JScrollPane(tarea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(pan);
add(tarea);
setLayout(null);
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[]aegs){
Experiment e=new Experiment();
}
}
答案 0 :(得分:2)
当您将JTextArea
组件与JSrcollPane
组件一起使用时,您应该将大小和位置设置为最后一个组件而不是第一个组件,并将创建的元素添加到JFrame
时您应该只添加JScrollPane
组件,因为它被视为JTextArea
组件的容器,请尝试以下代码:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main extends JFrame
{
JTextArea tarea;
JScrollPane pan;
public Main()
{
tarea = new JTextArea();
pan = new JScrollPane(tarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
pan.setBounds(100, 100, 200, 200);
add(pan);
setLayout(null);
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] aegs)
{
Main f = new Main();
}
}
答案 1 :(得分:2)
代码问题:
setBounds()
似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI越多,在使用它们时会遇到更严重的困难。当GUI调整大小时,它们不会调整组件的大小,它们是增强或维护的皇室女巫,当它们被放置在滚动窗格中时它们完全失败,当它们在所有平台上观看时或者与原始平台不同的屏幕分辨率时它们看起来很糟糕相反:
例如,假设您想在GUI中心的JScrollPane内部使用JTextArea,顶部有按钮,下面有一个JTextField和一个提交按钮,比如一个典型的聊天窗口类型应用程序,您可以使整个布局成为BorderLayout ,添加一个GridLayout-使用带有按钮的JPanel到顶部,一个BoxLayout使用带有JTextField的JPanel并将提交按钮添加到底部,以及将JTextArea保存到中心的JScrollPane。它看起来像这样:
代码看起来像这样:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
@SuppressWarnings("serial")
public class Experiment2 extends JPanel {
private static final int ROWS = 20;
private static final int COLUMNS = 50;
private static final int GAP = 3;
// create the JTextArea, setting its rows and columns properties
private JTextArea tarea = new JTextArea(ROWS, COLUMNS);
private JTextField textField = new JTextField(COLUMNS);
public Experiment2() {
// create the JScrollPane and pass in the JTextArea
JScrollPane scrollPane = new JScrollPane(tarea);
// let's create another JPanel to hold some buttons
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
buttonPanel.add(new JButton("Save"));
buttonPanel.add(new JButton("Load"));
buttonPanel.add(new JButton("Whatever"));
buttonPanel.add(new JButton("Exit"));
// create JPanel for the bottom with JTextField and a button
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
bottomPanel.add(textField);
bottomPanel.add(Box.createHorizontalStrut(GAP));
bottomPanel.add(new JButton("Submit"));
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
// use BorderLayout to add all together
setLayout(new BorderLayout(GAP, GAP));
add(scrollPane, BorderLayout.CENTER); // add scroll pane to the center
add(buttonPanel, BorderLayout.PAGE_START); // and the button panel to the top
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
Experiment2 mainPanel = new Experiment2();
JFrame frame = new JFrame("Experiment 2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
让我们试验并创建一个包含两个JTextAreas的GUI,一个是由colRowTextArea变量设置和保存列和行属性的GUI,另一个是JTextArea的首选大小,而不是猜测哪些有效,哪些不可用。设置,并将其变量称为prefSizeTextArea。
我们将创建一个方法setUpTextArea(...)
,我们将JTextArea放入JScrollPane,将其放入JPanel,并有一个按钮,将 lot 文本添加到JTextArea中,并查看添加文本时JTextArea的行为会发生什么。
这是代码并按下按钮,亲眼看看哪一个滚动:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class TwoTextAreas extends JPanel {
// our nonsense String
public static final String LoremIpsum = "Lorem ipsum dolor sit amet, "
+ "consectetur adipiscing elit, sed do eiusmod tempor incididunt "
+ "ut labore et dolore magna aliqua. Ut enim ad minim veniam, "
+ "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
+ "commodo consequat. Duis aute irure dolor in reprehenderit in "
+ "voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa "
+ "qui officia deserunt mollit anim id est laborum.";
private static final int ROWS = 30;
private static final int COLS = 40;
private static final Dimension TA_PREF_SIZE = new Dimension(440, 480);
private JTextArea colRowTextArea = new JTextArea(ROWS, COLS);
private JTextArea prefSizeTextArea = new JTextArea();
public TwoTextAreas() {
setLayout(new GridLayout(1, 0));
prefSizeTextArea.setPreferredSize(TA_PREF_SIZE);
add(setUpTextArea(colRowTextArea, "Set Columns & Rows"));
add(setUpTextArea(prefSizeTextArea, "Set Preferred Size"));
}
private JPanel setUpTextArea(JTextArea textArea, String title) {
// allow word wrapping
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new AppendTextAction(textArea)));
JPanel holderPanel = new JPanel(new BorderLayout());
holderPanel.setBorder(BorderFactory.createTitledBorder(title));
holderPanel.add(scrollPane);
holderPanel.add(buttonPanel, BorderLayout.PAGE_END);
return holderPanel;
}
private class AppendTextAction extends AbstractAction {
private JTextArea textArea;
private StringBuilder sb = new StringBuilder();
public AppendTextAction(JTextArea textArea) {
super("Append Text to TextArea");
this.textArea = textArea;
// create nonsense String
for (int i = 0; i < 100; i++) {
sb.append(LoremIpsum);
sb.append("\n");
}
}
@Override
public void actionPerformed(ActionEvent e) {
textArea.append(sb.toString());
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Two TextAreas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TwoTextAreas());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}