Fieds_labo3

swing-JFrame

最終更新:

fieds_labo3

- view
管理者のみ編集可

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();
           }
       });
   }
 }


記事メニュー
ウィキ募集バナー