java jtextarea后添加,java-JScrollPane未添加到JTextArea

您的代码有问题:

>通过设置JTextArea的边界来限制其大小.无论何时使用setBounds,setSize或setPreferredSize进行此操作,都将使JTextArea的大小变刚性,因此,如果向其中添加大于此大小的文本,则JTextArea不会扩展.这样做通常会导致包含JScrollPane的对象在需要时显示滚动条,因为JTextArea在需要时不会扩展.

>您正在使用空布局.尽管null布局和setBounds()似乎是Swing新手喜欢创建复杂GUI的最简单和最佳方法,但您创建的Swing GUI越多,使用它们时将遇到的困难就越大.当GUI调整大小时,它们不会重新调整组件的大小;它们是需要增强或维护的皇家女巫;放置在滚动窗格中时,它们会完全失败;在所有平台或与原始分辨率不同的屏幕分辨率下查看时,它们看起来都令人作呕.

>您要将JTextArea添加到两个容器(GUI和JScrollPane),而这在Swing GUI中是不允许的.

代替:

>通过设置JTextArea的行和列属性来限制其显示的大小,将它们传递到JTextArea的两个int构造函数中最容易实现.

>使用嵌套的JPanels,每个JPanels具有自己的布局,以实现复杂,灵活而又有吸引力的GUI.

>仅将JTextArea添加到JScrollPane的视口,然后将JScrollPane添加到GUI.

例如,假设您希望在GUI中心的JScrollPane内有一个JTextArea,在顶部有一个按钮,在下面有一个JTextField和一个Submit按钮,例如一个典型的聊天窗口类型的应用程序,您可以使整体布局成为BorderLayout,添加一个使用GridLayout的JPanel到顶部带有按钮,使用Box的JPanel带有JTextField并提交按钮到底部,而JScrollPane则将JTextArea放在中心.它可能看起来像这样:

XWkvH.jpg

代码看起来像这样:

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变量设置并保存column和rows属性,另一个由JTextArea的首选大小设置,并将其变量称为prefSizeTextArea.

我们将创建一个方法setUpTextArea(…),将JTextArea放入JScrollPane中,再将其放入JPanel中,并具有一个将大量文本添加到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("

");

}

}

@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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以通过设置JTextArea的UI来实现为JTextArea添加背景图。 以下是一个简单的例子: ```java import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.plaf.basic.BasicTextAreaUI; public class JTextAreaBackground extends JFrame { private static final long serialVersionUID = 1L; public JTextAreaBackground() { setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea textArea = new JTextArea(); textArea.setUI(new BasicTextAreaUI() { // 绘制背景图片 @Override protected void paintBackground(Graphics g) { super.paintBackground(g); Image image = new ImageIcon("background.png").getImage(); g.drawImage(image, 0, 0, getWidth(), getHeight(), null); } }); JScrollPane scrollPane = new JScrollPane(textArea); getContentPane().add(scrollPane); setVisible(true); } public static void main(String[] args) { new JTextAreaBackground(); } } ``` 在上面的代码中,我们创建了一个JTextArea,并将其UI设置为BasicTextAreaUI的一个子类。在该子类中,我们重写了paintBackground方法,用于绘制背景图片。最后,我们将JTextArea添加JScrollPane中,并将JScrollPane添加到JFrame中。运行程序后,即可看到JTextArea的背景图片。注意,在本例中,背景图片必须放在与JTextAreaBackground.java相同的目录下。 ### 回答2: 要为JTextArea添加背景图,可以使用以下步骤: 首先,创建一个JFrame对象,并设置其大小、可见性和关闭操作。 然后,创建一个JTextArea对象,并使用setOpaque(false)方法使其透明。 接下来,创建一个JPanel对象,并重写其paintComponent()方法,在方法内部使用g.drawImage()方法绘制背景图。 然后,将JTextArea对象添加到JPanel对象中。 最后,将JPanel对象添加到JFrame对象中。 下面是一个示例代码: ```java import javax.swing.*; import java.awt.*; public class TestFrame extends JFrame { public TestFrame() { // 设置窗口大小 setSize(400, 300); // 设置窗口可见性 setVisible(true); // 设置窗口关闭操作 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建JTextArea对象 JTextArea textArea = new JTextArea(); // 设置JTextArea透明 textArea.setOpaque(false); // 创建JPanel对象 JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // 加载背景图 Image image = new ImageIcon("background.jpg").getImage(); // 绘制背景图 g.drawImage(image, 0, 0, getWidth(), getHeight(), this); } }; // 将JTextArea添加到JPanel中 panel.add(textArea); // 将JPanel添加到JFrame中 add(panel); } public static void main(String[] args) { // 创建TestFrame对象 new TestFrame(); } } ``` 在上述代码中,我们首先创建了一个名为TestFrame的类,继承自JFrame。在TestFrame的构造方法中,我们按照以上步骤进行操作,并使用“background.jpg”作为背景图。最后,我们在main方法中创建了TestFrame对象来展示窗口并实现效果。 ### 回答3: 在Java中为JTextArea添加背景图,可以通过以下步骤实现: 1. 导入必要的库: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; ``` 2. 创建JFrame对象和JTextArea对象: ```java JFrame frame = new JFrame(); JTextArea textArea = new JTextArea(); ``` 3. 创建JPanel对象,并将JTextArea添加到JPanel中: ```java JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(textArea); ``` 4. 创建一个继承自JComponent的类,并重写paintComponent方法,在该方法中绘制背景图: ```java class BackgroundPanel extends JComponent { private Image image; public BackgroundPanel(Image image) { this.image = image; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, null); } } ``` 5. 加载图片,并将JPanel对象设置为背景图片: ```java Image backgroundImage = new ImageIcon("background.jpg").getImage(); BackgroundPanel backgroundPanel = new BackgroundPanel(backgroundImage); backgroundPanel.add(panel); frame.setContentPane(backgroundPanel); ``` 6. 设置JFrame的属性并显示窗口: ```java frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setVisible(true); ``` 通过以上步骤,就可以为JTextArea添加背景图了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值