「swing-JFrame」の編集履歴(バックアップ)一覧に戻る

swing-JFrame - (2008/10/29 (水) 18:31:02) のソース

*Swing JFrame
トップレベルコンテナ

JFrameサンプル1
  import javax.swing.*;        
  
  public class TestFrame {
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Test1 --- Kitty");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
        //Add the ubiquitous "Hello World" label.
        //JLabel label = new JLabel("Hello World");
        //frame.getContentPane().add(label);
        JButton bt1 = new JButton("Push me!");
        frame.getContentPane().add(bt1);
  
        //Display the window.
        frame.setBounds(0 , 0 , 400 , 200);
        //frame.pack();
        frame.setVisible(true);
    }
  
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
  }

1.frame.setDefaultCloseOperation()
ウィンドウ終了動作
-DO_NOTHING_ON_CLOSE :何もしない
-HIDE_ON_CLOSE :非表示
-DISPOSE_ON_CLOSE :破棄
-EXIT_ON_CLOSE :アプリ終了

2.setBounds()
ウィンドウサイズ

3.pack()
ウィンドウサイズを中に表示するコンポーネントにあわせて最適化(最小化)する。


JFrameサンプル2
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;        
  
  public class TestFrame {
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Test1 --- Kitty");
        frame.setBounds(0 , 0 , 400 , 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
        JLabel label = new JLabel("Hello World\n\n");
        JButton bt1 = new JButton("Push me!");
        JPanel contentPane = new JPanel(new BorderLayout());
        //contentPane.setBorder(someBorder);
        contentPane.add(label, BorderLayout.CENTER);
        contentPane.add(bt1, BorderLayout.PAGE_END);
   
        frame.setContentPane(contentPane);
   
        //Display the window.
        frame.setBounds(0 , 0 , 400 , 200);
        //frame.pack();
        frame.setVisible(true);
    }
  
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
  }

 
記事メニュー
人気記事ランキング
目安箱バナー