Codice PHP:
public MyFrame(String title)
{
super(title);
mainPanel = new JPanel(new GridBagLayout());
menuBar = new JMenuBar();
Close = new JMenu("Close");
menu2 = new JMenu("menu2");
close = new JMenuItem("Close");
close.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(-1);
}
});
Close.add(close);
menuBar.setBackground(Color.blue);
menuBar.add(
Close,
new GridBagConstraints(
0,0,2,1,
1.0,0.0,
GridBagConstraints.WEST,GridBagConstraints.NONE,
new Insets(0,0,0,0),
5,0));
menuBar.add(
menu2,
new GridBagConstraints(
1,0,2,1,
1.0,0.0,
GridBagConstraints.WEST,GridBagConstraints.NONE,
new Insets(0,0,0,0),
5,0));
inputPanel = new JPanel(new GridBagLayout());
address = new JTextField();
address.setSize(40,5);
ok = new JButton("OK");
//ok.setSize(10,5);
ok.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doConnection();
}
});
inputPanel.add(
address,
new GridBagConstraints(
0,0,3,1,
0.0,0.0,
GridBagConstraints.WEST,GridBagConstraints.NONE,
new Insets(0,0,0,0),
0,0));
inputPanel.add(
ok,
new GridBagConstraints(
1,0,1,1,
0.0,0.0,
GridBagConstraints.EAST,GridBagConstraints.NONE,
new Insets(0,0,0,0),
0,0));
outputPanel = new JPanel();
output = new JLabel();
outputPanel.add(output);
mainPanel.add(menuBar,
new GridBagConstraints(
0,0,4,1,
0.0,2.0,
GridBagConstraints.NORTHWEST,GridBagConstraints.NONE,
new Insets(0,0,0,0),
200,0));
mainPanel.add(inputPanel,
new GridBagConstraints(
0,1,4,2,
0.0,1.0,
GridBagConstraints.CENTER,GridBagConstraints.NONE,
new Insets(0,0,0,0),
0,0));
mainPanel.add(outputPanel,
new GridBagConstraints(
0,2,4,1,
0.0,1.0,
GridBagConstraints.SOUTH,GridBagConstraints.NONE,
new Insets(0,0,0,0),
0,0));
this.getContentPane().add(mainPanel);
this.setSize(400,200);
this.setLocation((int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2),
(int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2));
this.show();
}
Quello che non capisco è come organizzare i vari elementi, perchè durante le mie prove non sono riuscito a posizionarli dove e come vorrei io. Ad esempio eseguendo il codice postato: