gpt4 book ai didi

java - java中ScrollPanel不出现JTextArea调整大小而是

转载 作者:行者123 更新时间:2023-12-02 00:51:34 24 4
gpt4 key购买 nike

我的程序已经完成,但是测试一下,我发现滚动面板没有出现,它只是调整了 JTextArea 的大小。代码如下:

package javaapplication15;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.io.IOException;

import javax.swing.*;

public class Tekstprogram extends JFrame {

public Tekstprogram() {

setSize(400, 600);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
Container Indhold = getContentPane();
Indhold.setLayout(new FlowLayout());

JButton openButton = new JButton("Open");
JButton saveButton = new JButton("Save");

final JLabel statusbar =
new JLabel("Output of your selection will go here");

final JTextArea TekstOmråde = new JTextArea(29, 30);

JScrollPane scrollText = new JScrollPane(TekstOmråde);

openButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
int option = chooser.showOpenDialog(Tekstprogram.this);
if (option == JFileChooser.APPROVE_OPTION) {
File[] sf = chooser.getSelectedFiles();
String filelist = "nothing";
if (sf.length > 0) {
filelist = sf[0].getName();
}
for (int i = 1; i < sf.length; i++) {
filelist = filelist + ", " + sf[i].getName();
}

try {
String strLine;
File selectedFile = chooser.getSelectedFile();
FileInputStream in = new FileInputStream(selectedFile);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null) {
TekstOmråde.append(strLine + "\n");
}
} catch (Exception e) {
System.out.println("En fejl opstod ved" + e);
}

}
}
});

saveButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
int option = chooser.showSaveDialog(Tekstprogram.this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));

out.write(TekstOmråde.getText());
out.close();

} catch (IOException e) {
System.out.println("IOException fejl opstod :");
e.printStackTrace();
}

}

}
});

Indhold.add(openButton);
Indhold.add(saveButton);
Indhold.add(TekstOmråde);
Indhold.add(scrollText);
Indhold.add(statusbar);
}

public static void main(String args[]) {
Tekstprogram sfc = new Tekstprogram();
sfc.setVisible(true);
}
}

是否有办法使 JTextArea 静态化?

最佳答案

删除 Indhold.add(TekstOmråde);

自从你有了

JScrollPane scrollText = new JScrollPane(TekstOmråde);

您已经通过执行以下操作间接添加了 TexstOmråde

Indhold.add(scrollText);

关于java - java中ScrollPanel不出现JTextArea调整大小而是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2797845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com