gpt4 book ai didi

Java 图形用户界面 : Display Area and 8 buttons

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

我想要一个显示区域和 8 个按钮。每个按钮都会在显示区域显示不同的文本。

目前我只有显示区域,但是当我尝试添加按钮时,按钮与显示区域重叠。那么我怎样才能有一个显示区域和8个按钮。

JPanel middlePanel = new JPanel ();
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );

// create the middle panel components

JTextArea display = new JTextArea ( 16, 58 );
display.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane ( display );
scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

//Add Textarea in to middle panel
middlePanel.add ( scroll );

// My code
JFrame frame = new JFrame ();
JFrame btn = new JFrame();
frame.add ( middlePanel );
frame.pack ();
frame.setLocationRelativeTo ( null );

JButton one = new JButton("1");
JPanel panel = new JPanel();
panel.add(one);
//btn.getContentPane().add(BorderLayout.CENTER,panel);
btn.setVisible(true);
frame.setVisible ( true );

最佳答案

使用两个容器,一个用于文本区域,一个用于按钮,每个容器都有自己的布局管理器...

JPanel middlePanel = new JPanel (new BorderLayout());
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );

JTextArea display = new JTextArea ( 16, 58 );
display.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane ( display );
scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

middlePanel.add ( scroll );

JPanel buttonPane = new JPanel(); // FlowLayout by default...
buttonPane.add(...); // Add your buttons here...

JFrame frame = new JFrame ();
frame.add ( middlePanel );
frame.add(buttonPane, BorderLayout.SOUTH);
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible(true);

这通常称为复合布局;)

关于Java 图形用户界面 : Display Area and 8 buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16929392/

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