shownolayout.java

来自「此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码」· Java 代码 · 共 57 行

JAVA
57
字号
// ShowNoLayout.java: Place components without using a layout manager
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShowNoLayout extends JFrame
{
  private JLabel jlbl =
    new JLabel("Resize the Window and Study No Layout",
      JLabel.CENTER);;
  private JTextArea jta1 = new JTextArea("Text Area", 5, 10 );
  private JTextArea jta2 = new JTextArea("Text Area", 5, 10 );
  private JTextField jtf = new JTextField("TextField");
  private JPanel jp = new JPanel();
  private JButton jbt1 = new JButton("Cancel" );
  private JButton jbt2 = new JButton("Ok" );
  private GridBagLayout gbLayout;
  private GridBagConstraints gbConstraints;

  public static void main(String[] args)
  {
    ShowNoLayout frame = new ShowNoLayout();
    frame.setSize(400,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  public ShowNoLayout()
  {
    setTitle("Show No Layout");

    // Set background color for the panel
    jp.setBackground(Color.red);

    // Specify no layout manager
    getContentPane().setLayout(null);

    // Add components to frame
    getContentPane().add(jlbl);
    getContentPane().add(jp);
    getContentPane().add(jta1);
    getContentPane().add(jta2);
    getContentPane().add(jtf);
    getContentPane().add(jbt1);
    getContentPane().add(jbt2);

    // Put components in the right place
    jlbl.setBounds(0, 10, 400, 40);
    jta1.setBounds(0, 50, 100, 100);
    jp.setBounds(100, 50, 200, 100);
    jta2.setBounds(300, 50, 100, 50);
    jtf.setBounds(300, 100, 100, 50);
    jbt1.setBounds(100, 150, 100, 50);
    jbt2.setBounds(200, 150, 100, 50);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?