Reputation: 6983
I;m trying to have a top button and a buttom button. But only the buttom button gets drawn. Here is the code
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.NORTH);
the complete function
private void initUI() {
/////////////////////////////////////////////////////////////////////////////
// set upui
setTitle("Simple example");
// Set size to match screen
mWidth=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-50;
mHeight=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()-50;
setSize( mWidth, mHeight);
setLocationRelativeTo(null);
// Set close operation to exit
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel1 =new JPanel();
JButton btn = new JButton("Top Button"); // Button is a Component
btn.addActionListener(this);
panel1.add(btn);
JPanel panel2 =new JPanel();
JButton btn2 = new JButton("Buttom Button"); // Button is a Component
panel2.add(btn2);
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.NORTH);
// Add the chart
// NOTE class DrawCompoment is defifin below where the drawing ooeration is overidden
DrawComponent test = new DrawComponent();
add(test,BorderLayout.CENTER);
///////////////////////////////////////////////////////////////////////////////
// sert up vscreen
vStartX=(double)10;
vStartY=10;
vWidth=mWidth-40;
vHeight=mHeight-80;
dx=vWidth/t.daySize;
dy=vHeight/t.dayBiggest;
// save fdata in spreads sheet
createSpreadSheet();
}
Upvotes: 0
Views: 2573
Reputation: 285405
You're adding panel1 and panel2 both to the same BorderLayout position, and only one can be added there. You need perhaps another JPanel to hold them both and then add that one to the BorderLayout.NORTH spot.
e.g.,
JPanel panel1 = new JPanel();
JButton btn = new JButton("Top Button"); // Button is a Component
btn.addActionListener(this);
panel1.add(btn);
JPanel panel2 = new JPanel();
JButton btn2 = new JButton("Buttom Button"); // Button is a Component
panel2.add(btn2);
// A JPanel to hold both panel1 and panel2
JPanel containerPanel = new JPanel(new GridLayout(2, 1));
containerPanel.add(panel1);
containerPanel.add(panel2);
// add only one component to the BorderLayout.NORTH position of the JFrame
add(containerPanel, BorderLayout.NORTH);
Upvotes: 4