gpt4 book ai didi

java - 使用 GridLayout、JPanel、BorderLayout

转载 作者:行者123 更新时间:2023-12-01 11:46:41 25 4
gpt4 key购买 nike

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 使用是否正确?

最佳答案

首先不扩展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);
}
}

关于java - 使用 GridLayout、JPanel、BorderLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29088814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com