如何使用gridlayout将面板和按钮添加到java中的同一帧?

时间:2015-04-15 08:10:19

标签: java swing

我想使用gridlayout在同一帧中使用2个面板和三个按钮编写Java程序,第一个面板应包含2个标签和1个文本字段,第二个面板应包含2个按钮,两个面板正确完成但我不能不使用面板添加三个按钮。

public class ConverterGUI3 extends JFrame {

    private JLabel binaryLabel,resultLabel;
    private JTextField inputText;
    private JButton zero,one,toDecimal,toOctal,clear;
    private JPanel panel1,panel2;

    public  ConverterGUI3 () {
      setSize(400,200);
      setTitle("Converter");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      binaryLabel=new JLabel("Binary");
      inputText=new JTextField();
      resultLabel=new JLabel("Result");

      zero=new JButton("0"); 
      one=new JButton("1"); 

      toDecimal=new JButton("Convert To Decimal"); 
      toOctal=new JButton("Convert To Octal");
      clear=new JButton("Clear");

      Container cp=getContentPane();  
      cp.setLayout(new GridLayout());

      panel1=new JPanel();
      panel1.setLayout(new GridLayout(1,3));
      panel1.add(binaryLabel);
      panel1.add(inputText);
      panel1.add(resultLabel);
      cp.add(panel1, BorderLayout.NORTH);

      panel2=new JPanel();
      panel2.setLayout(new GridLayout(1,2));
      panel2.add(zero);
      panel2.add(one);
      cp.add(panel2, BorderLayout.CENTER);

      cp.add(toDecimal, BorderLayout.SOUTH);
      cp.add(toOctal, BorderLayout.SOUTH);
      cp.add(clear, BorderLayout.SOUTH);
  }
  public static void main(String[] args) {
      ConverterGUI3 frame = new ConverterGUI3();
      frame.setVisible(true);
  }
}

Converter

0 个答案:

没有答案