import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Application1 {
private boolean packFrame = false;
//Construct the application
public Application1() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}
}
class Frame1 extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JTextArea jTextArea1 = new JTextArea();
private JPanel jPanel1 = new JPanel();
private JButton jButton1 = new JButton();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jTextArea1.setText("jTextArea1");
jTextArea1.setLineWrap(true);
jTextArea1.setRows(2);
jTextArea1.setWrapStyleWord(true);
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton1.setText("jButton1");
contentPane.add(jTextArea1, BorderLayout.CENTER);
contentPane.add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jButton1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
当输入的内容太多时,我的JTextArea怎么不会自动出现滚动条呢?
7 个解决方案
#1
先加JScrollpanel,在把JTextArea 家道JScrollPanel 上
#2
同意.
#3
jScrollPane1.getViewport().add(jTextArea1, null);
#4
呵呵,同意....
#5
把你代码中的这一句:
contentPane.add(jTextArea1, BorderLayout.CENTER);
改为:
contentPane.add(new JScroollPanel(jTextArea1), BorderLayout.CENTER);
即可。
contentPane.add(jTextArea1, BorderLayout.CENTER);
改为:
contentPane.add(new JScroollPanel(jTextArea1), BorderLayout.CENTER);
即可。
#6
我同意henry_cn(学问:要学,要问!) 老兄的看法。
#7
JScroolPanel jScrool = new JScroolPanel(jTextArea1);
contentPane.add(jScrool, BorderLayout.CENTER);
contentPane.add(jScrool, BorderLayout.CENTER);
#1
先加JScrollpanel,在把JTextArea 家道JScrollPanel 上
#2
同意.
#3
jScrollPane1.getViewport().add(jTextArea1, null);
#4
呵呵,同意....
#5
把你代码中的这一句:
contentPane.add(jTextArea1, BorderLayout.CENTER);
改为:
contentPane.add(new JScroollPanel(jTextArea1), BorderLayout.CENTER);
即可。
contentPane.add(jTextArea1, BorderLayout.CENTER);
改为:
contentPane.add(new JScroollPanel(jTextArea1), BorderLayout.CENTER);
即可。
#6
我同意henry_cn(学问:要学,要问!) 老兄的看法。
#7
JScroolPanel jScrool = new JScroolPanel(jTextArea1);
contentPane.add(jScrool, BorderLayout.CENTER);
contentPane.add(jScrool, BorderLayout.CENTER);