Java 程序以显示 JTextArea 中的内容
以下示例展示了在 JTextArea 中显示文本文件的内容——
示例
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo { private JFrame mainFrame; private JLabel statusLabel; private JPanel controlPanel; public SwingDemo() { prepareGUI(); } public static void main(String[] args) { SwingDemo demo = new SwingDemo(); demo.showTextAreaDemo(); } private void prepareGUI() { mainFrame = new JFrame("Java Swing"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { System.exit(0); } }); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showTextAreaDemo() { JLabel commentlabel= new JLabel("Text = ", JLabel.RIGHT); final JTextArea commentTextArea = new JTextArea("This is demo text!",5,20); JScrollPane scrollPane = new JScrollPane(commentTextArea); JButton showButton = new JButton("Show"); showButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { statusLabel.setText( commentTextArea.getText()); } }); controlPanel.add(commentlabel); controlPanel.add(scrollPane); controlPanel.add(showButton); mainFrame.setVisible(true); } }
输出
点击上方的“显示”按钮来展示文本——
广告