New to GUI, I am trying to create a simple JFrame
with two JTextArea
instances positioned right next to each other and a JPanel
at the bottom.
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Demo extends JFrame
{
private JPanel panel;
private JTextArea JTextArea1;
private JTextArea JTextArea2;
private DecisionPanel decisionPanel;
private GridLayout gridLayout;
private Container container;
public Demo()
{
super( "Demo" );
Container myContainer = new Container();
JTextArea1 = new JTextArea();
JTextArea2 = new JTextArea();
GridLayout gridLayout = new GridLayout( 1, 2 );
myContainer.setLayout( gridLayout );
myContainer.add( new JScrollPane( JTextArea1 ) );
myContainer.add( new JScrollPane( JTextArea2 ) );
JFrame f = new JFrame();
f.add( myContainer, BorderLayout.CENTER);
f.add( decisionPanel, BorderLayout.PAGE_END );
f.setSize( 400, 400 );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setVisible( true );
}
}
JFrame
does not appear. Is this the right way to add JTextArea
objects to GridLayout
and is Container
used correctly?
Start by not extending JFrame
, this is causing you confusion. Basically, your example code has two instances of JFrame
, so which one is actually been shown on the screen when?
You must also be generating a NullPointerException
as decisionPanel
is never initialised.
public class Demo { //extends JFrame {
private JPanel panel;
private JTextArea JTextArea1;
private JTextArea JTextArea2;
private DecisionPanel decisionPanel;
private GridLayout gridLayout;
private Container container;
public Demo() {
Container myContainer = new Container();
decisionPanel = new DecisionPanel();
JTextArea1 = new JTextArea();
JTextArea2 = new JTextArea();
GridLayout gridLayout = new GridLayout(1, 2);
myContainer.setLayout(gridLayout);
myContainer.add(new JScrollPane(JTextArea1));
myContainer.add(new JScrollPane(JTextArea2));
JFrame f = new JFrame("Demo");
f.add(myContainer, BorderLayout.CENTER);
f.add(decisionPanel, BorderLayout.PAGE_END);
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Or, extend from JPanel
and add the Demo
panel to the JFrame
independently, which might be more preferable depending on what you are trying to achieve...
public class Demo extends JPanel {
private JPanel panel;
private JTextArea JTextArea1;
private JTextArea JTextArea2;
private DecisionPanel decisionPanel;
private GridLayout gridLayout;
private Container container;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Demo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public Demo() {
setLayout(new BorderLayout());
Container myContainer = new Container();
decisionPanel = new DecisionPanel();
JTextArea1 = new JTextArea();
JTextArea2 = new JTextArea();
GridLayout gridLayout = new GridLayout(1, 2);
myContainer.setLayout(gridLayout);
myContainer.add(new JScrollPane(JTextArea1));
myContainer.add(new JScrollPane(JTextArea2));
add(myContainer, BorderLayout.CENTER);
add(decisionPanel, BorderLayout.PAGE_END);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.