GUI新手,我正在尝试创建一个简单的JFrame
,其中两个JTextArea
个实例位于彼此旁边,而JPanel
位于底部。
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
没有出现。这是将JTextArea
个对象添加到GridLayout
并且Container
正确使用的正确方法吗?
答案 0 :(得分:1)
从不延长JFrame
开始,这会让您感到困惑。基本上,您的示例代码有两个JFrame
实例,那么实际上哪一个实际显示在屏幕上?
您还必须生成NullPointerException
,因为decisionPanel
从未初始化。
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);
}
}
或者,从JPanel
延伸并将Demo
面板单独添加到JFrame
,这可能更为可取,具体取决于您要实现的目标......
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);
}
}