[英]Java GUI: Display Area and 8 buttons
我想要一個顯示區域和8個按鈕。 每個按鈕將在顯示區域中顯示不同的文本。
當前,我只有顯示區域,但是當我嘗試添加A按鈕時,該按鈕與顯示區域重疊。 那么我怎么有一個顯示區域和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);
這通常稱為復合布局;)
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 );
JPanel buttonPane = new JPanel(); // FlowLayout by default...
buttonPane.add(new JButton("1")); // Add your buttons here...
buttonPane.add(new JButton("2"));
buttonPane.add(new JButton("3"));
buttonPane.add(new JButton("4"));
// My code
JFrame frame = new JFrame ();
JFrame btn = new JFrame();
frame.add ( middlePanel );
frame.add(buttonPane,BorderLayout.SOUTH);
frame.pack ();
frame.setLocationRelativeTo ( null );
//btn.getContentPane().add(BorderLayout.CENTER,panel);
btn.setVisible(true);
frame.setVisible ( true );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.