Even though the code is scala, you can see the swing oriented approach. I just want to add a text area to a panel(core) and associate panel(core) with the frame. I want panel core and the text area to fill out 100% in the width and the height. Basically, I just want a text area shown. With this code, I can sort of see the textfield but is it. The width looks like it is only 1 pixel.
Possible Solution? should I find a way to add a layout to the frame? Which layout?
import java.awt.{ Insets, GridBagConstraints, Dimension }
import javax.swing.{ JPanel, JScrollPane, JTextArea }
import scala.swing.Swing._
import scala.swing.{ MainFrame, Panel, SimpleSwingApplication }
object LogAnaylyzerMain extends SimpleSwingApplication {
def maxWidth = 900
def maxHeight = 600
def initXPos = 320
def initYPos = 260
/**
* Core Panel Content.
*/
object coreContentPanel extends JPanel {
val outputTextArea = new JTextArea
val outputTextScrollPane = new JScrollPane(outputTextArea)
this.add(outputTextScrollPane)
}
class outputTextArea extends JTextArea {
this.setLineWrap(false)
this.setCaretPosition(0)
this.setEditable(true);
}
/**
* Main Frame, entry point.
*/
def top = new MainFrame {
peer.setLocation(initXPos, initYPos)
title = "JVM Log Analyzer"
contents = new Panel {
preferredSize = (maxWidth, maxHeight)
focusable = true
peer.add(c开发者_如何学运维oreContentPanel)
pack()
}
}
} // End of the Class //
The default layout of JPanel
(and Panel
) is FlowLayout
. Using GridLayout
or BorderLayout
center should let the JTextArea
fill the preferred size.
Addendum: Here's a (somewhat) comparable Java Swing example:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/** @see http://stackoverflow.com/questions/5421319 */
public class TextAreaTest extends JPanel {
private static final int maxWidth = 900;
private static final int maxHeight = 600;
private static final int initXPos = 320;
private static final int initYPos = 260;
private JTextArea ta = new JTextArea();
public TextAreaTest() {
this.setPreferredSize(new Dimension(900, 600));
this.setLayout(new GridLayout());
this.add(ta);
ta.append("Hello, world!");
}
private void display() {
JFrame f = new JFrame("TextAreaTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocation(initXPos, initYPos);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TextAreaTest().display();
}
});
}
}
精彩评论